吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2867|回复: 11
收起左侧

[Java 转载] 很久之前学习的时候 写的一个计算器

  [复制链接]
lpc0828 发表于 2020-10-22 15:14
现在看起来 还是很有感觉
[Java] 纯文本查看 复制代码
import java.awt.BorderLayout;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JiSuanQi {
	private JFrame frame;
	private Container con;
	private JPanel panel1, panel2, panel3, panel4;
	private JButton[] butt;
	private JButton but2, but3, but4, but5, but6, but8, but9, but10, but11, but12;
	private JTextField ll;

	// 构造器
	public JiSuanQi() {
		frame = new JFrame("SVIP专用计算器");
		frame.setSize(500, 600);
		frame.setLocation(600, 250);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		con = frame.getContentPane();
		init();
		frame.setVisible(true);
	}

	//模块以及计算方法
	@SuppressWarnings("unused")
	void init() {

		// 上面的模块
		frame.setLayout(new BorderLayout());

		panel1 = new JPanel();
		con.add(panel1, BorderLayout.NORTH);

		ll = new JTextField(30);

		ll.setFont(new Font("宋体", Font.PLAIN, 30));
		panel1.add(ll);

		// 中间的模块
		// 九键
		butt = new JButton[9];
		panel2 = new JPanel();
		panel2.setLayout(new GridLayout(3, 3));
		con.add(panel2, BorderLayout.CENTER);

		for (int i = 0; i < butt.length; i++) {
			butt[i] = new JButton((i + 1) + "");
			Font f = new Font("宋体", Font.BOLD, 40);
			butt[i].setFont(f);
			panel2.add(butt[i]);

		}
		// 1到9 每个键位设置
		butt[0].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + 1 + "");
			}
		});
		butt[1].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + 2 + "");
			}
		});
		butt[2].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + 3 + "");
			}
		});
		butt[3].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + 4 + "");
			}
		});
		butt[4].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + 5 + "");
			}
		});
		butt[5].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + 6 + "");
			}
		});
		butt[6].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + 7 + "");
			}
		});
		butt[7].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + 8 + "");
			}
		});
		butt[8].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + 9 + "");
			}
		});

		// 右边的模块
		panel3 = new JPanel();
		panel3.setLayout(new GridLayout(3, 1));
		con.add(panel3, BorderLayout.EAST);

		but2 = new JButton("*");
		Font f = new Font("宋体", Font.BOLD, 40);
		but2.setFont(f);
		panel3.add(but2);
		but2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + "*");
			}
		});

		but3 = new JButton("/");

		Font q = new Font("宋体", Font.BOLD, 40);
		but3.setFont(f);
		panel3.add(but3);
		but3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + "/");

			}
		});

		but4 = new JButton("C");
		Font w = new Font("宋体", Font.BOLD, 40);
		but4.setFont(f);
		panel3.add(but4);
		but4.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText("");

			}
		});
		panel3.setPreferredSize(new Dimension(120, 0));

		// 下边的模块
		panel4 = new JPanel();
		panel4.setLayout(new GridLayout(2, 4));
		con.add(panel4, BorderLayout.SOUTH);

		but5 = new JButton("0");
		// 根据指定字体名称、样式和磅值大小,创建一个新 Font。
		Font e = new Font("宋体", Font.BOLD, 40);
		but5.setFont(f);
		panel4.add(but5);

		but5.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + "0");
			}
		});

		but6 = new JButton("+");
		Font r = new Font("宋体", Font.BOLD, 40);
		but6.setFont(f);
		panel4.add(but6);
		but6.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + "+");
			}
		});

		but8 = new JButton("-");
		Font t = new Font("宋体", Font.BOLD, 40);
		but8.setFont(f);
		panel4.add(but8);
		but8.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + "-");
			}
		});

		// substring(0,length-1)删除字符串最后一位
		but12 = new JButton("b");
		Font y = new Font("宋体", Font.BOLD, 40);
		but12.setFont(f);
		panel4.add(but12);
		but12.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					ll.setText(ll.getText().substring(0, ll.getText().length() - 1));
				} catch (Exception a) {
					ll.setText("");
				}
			}
		});

		but9 = new JButton(".");
		Font a = new Font("宋体", Font.BOLD, 40);
		but9.setFont(f);
		panel4.add(but9);
		but9.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + ".");
			}
		});

		but10 = new JButton("(");
		Font s = new Font("宋体", Font.BOLD, 40);
		but10.setFont(f);
		panel4.add(but10);
		but10.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + "(");
			}
		});

		but11 = new JButton(")");
		Font d = new Font("宋体", Font.BOLD, 40);
		but11.setFont(f);
		panel4.add(but11);
		but11.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ll.setText(ll.getText() + ")");
			}
		});
		// =计算数据结果以及处理异常
		but8 = new JButton("=");
		Font z = new Font("宋体", Font.BOLD, 40);
		but8.setFont(f);
		panel4.add(but8);
		but8.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				ScriptEngineManager manager = new ScriptEngineManager();
				ScriptEngine engine = manager.getEngineByExtension("js");
				Bindings bindings = engine.createBindings();
				String expression = (ll.getText());
				bindings.put("expression", expression);
				Integer value = 1;
				Double v = 1.0;
				try {
					int a = (int) engine.eval("eval(expression)", bindings);
					ll.setText("" + a);
				} catch (Exception e1) {
					try {
						Double v1 = (Double) engine.eval("eval(expression)", bindings);
						ll.setText("" + v1);
					} catch (Exception e11) {
						ll.setText("输入有误!请重新输入!");

					}

				}

			}
		});

		panel4.setPreferredSize(new Dimension(0, 200));

	}

	public static void main(String[] args) {
		new JiSuanQi();
	}

}


计算器.png



免费评分

参与人数 1热心值 +1 收起 理由
默君 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

eepp 发表于 2020-10-22 15:30
不会是当初学JAVA的大作业吧?哈哈
 楼主| lpc0828 发表于 2020-10-22 15:39
赵纯良 发表于 2020-10-22 15:42
Dream_Peng 发表于 2020-10-22 16:36
java 很少拿来写窗口
QingYi. 发表于 2020-10-22 18:10
这种用c#做好点吧
wanshiz 发表于 2020-10-23 12:45
可以看出当年的风采
靓仔小黄 发表于 2020-11-8 03:06
eepp 发表于 2020-10-22 15:30
不会是当初学JAVA的大作业吧?哈哈

哈哈,之前刚被布置过
Yamz 发表于 2020-11-8 08:30
学习一下…
尼古拉斯·刘能 发表于 2020-11-11 09:22
哈哈,以前也写过,只是没这个复杂
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-6 03:41

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表