Java实现简易计算器 发表于 2015-07-13 | 分类于 Java | | 阅读次数 简述 使用JDK 1.8; 使用JFrame实现窗口,JFrame拥有比Applet更好的界面(个人认为); 已经实现基本的+、-、*、/四则运算,支持小数点,正负数,未实现除0判断; 程序截图 源代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Calculator extends JFrame { private Container container; //定义布局方式为网格包布局 private GridBagLayout layout; private GridBagConstraints constraints; //计算结果显示区 private JTextField displayField; //保存+, -, *, /, =命令 private String lastCommand; //保存计算结果 private double result; //判断是否为数字的开始 private boolean start; public Calculator() { super("计算器"); setBounds(300, 200, 100, 100); container = getContentPane(); layout = new GridBagLayout(); container.setLayout(layout); constraints = new GridBagConstraints(); start = true; result = 0; //默认执行的操作 lastCommand = "="; //定义计算区域 displayField = new JTextField(20); //计算区域的数字靠右显示 displayField.setHorizontalAlignment(JTextField.RIGHT); //定义计算区域的背景颜色 displayField.setBackground(Color.white); //定义计算区域的前景颜色 displayField.setForeground(Color.blue); displayField.setFont(new Font("Arial", 24, 24)); //定义计算区域的X,Y轴 constraints.gridx = 0; constraints.gridy = 0; //定义计算区域的宽度和高度 constraints.gridwidth = 4; constraints.gridheight = 1; //定义控件填充满剩余空间 constraints.fill = GridBagConstraints.BOTH; //定义计算区域的X,Y轴单位度量值 constraints.weightx = 50; constraints.weighty = 50; layout.setConstraints(displayField, constraints); //将计算区域加入到窗口中 container.add(displayField); //定义两个监视器 ActionListener insert = new InsertAction(); ActionListener command = new CommandAction(); //通过定义好的方法添加按钮 addButton("Bck", 0, 1, 1, 1, insert); addButton("CE", 1, 1, 1, 1, insert); addButton("CLEAR", 2, 1, 2, 1, insert); addButton("7", 0, 2, 1, 1, insert); addButton("8", 1, 2, 1, 1, insert); addButton("9", 2, 2, 1, 1, insert); addButton("/", 3, 2, 1, 1, command); addButton("4", 0, 3, 1, 1, insert); addButton("5", 1, 3, 1, 1, insert); addButton("6", 2, 3, 1, 1, insert); addButton("*", 3, 3, 1, 1, command); addButton("1", 0, 4, 1, 1, insert); addButton("2", 1, 4, 1, 1, insert); addButton("3", 2, 4, 1, 1, insert); addButton("-", 3, 4, 1, 1, command); addButton("0", 0, 5, 1, 1, insert); //只显示"-"号, "+"不显示 addButton("+/-", 1, 5, 1, 1, insert); addButton(".", 2, 5, 1, 1, insert); addButton("+", 3, 5, 1, 1, command); addButton("Powered By GYSHGX868", 0, 6, 3, 1, insert); addButton("=", 3, 6, 1, 1, command); //设置窗口的大小 setSize(300, 300); //设置窗口可见 setVisible(true); } private void addButton(String label, int row, int column, int with, int height, ActionListener listener) { // 定义添加按钮的方法 JButton button = new JButton(label); constraints.gridx = row; constraints.gridy = column; constraints.gridwidth = with; constraints.gridheight = height; constraints.fill = GridBagConstraints.BOTH; button.addActionListener(listener); layout.setConstraints(button, constraints); container.add(button); //设置按钮间的间距 constraints.insets = new Insets(1, 1, 1, 1); //设置字体 button.setFont(new Font("Arial", 12, 18)); button.setForeground(Color.MAGENTA); button.setBorderPainted(true); button.setContentAreaFilled(true); button.setBounds(2, 2, 2, 2); button.contains(5, 5); } private class InsertAction implements ActionListener { //定义Insert事件的方法 public void actionPerformed(ActionEvent event) { String input = event.getActionCommand(); if (start) { displayField.setText(""); //默认计算区域为空 start = false; //如果开始输入的是"+/-",以负数显示 if (input.equals("+/-")) displayField.setText(displayField.getText() + "-"); } if (!input.equals("+/-")) { if (input.equals("Bck")) { String str = displayField.getText(); if (str.length() > 0) { //如果单击退格,实现退格的功能 displayField.setText(str.substring(0, str.length() - 1)); } } else if (input.equals("CE") || input.equals("CLEAR")) { displayField.setText("0"); start = true; //实现清零和复位的功能 } else if (input.equals(".")) { if (displayField.getText().trim().indexOf(".") == -1) { //输入小数点时,判断是否已经输入过小数点 displayField.setText(displayField.getText() + input); } } else displayField.setText(displayField.getText() + input); } } } private class CommandAction implements ActionListener { public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); if (start) { lastCommand = command; } else { calculate(Double.parseDouble(displayField.getText())); lastCommand = command; start = true; //记忆输入的操作符号 } } } public void calculate(double x) { if (lastCommand.equals("+")) result += x; else if (lastCommand.equals("-")) result -= x; else if (lastCommand.equals("*")) result *= x; else if (lastCommand.equals("/")) result /= x; else if (lastCommand.equals("=")) result = x; //实现计算的功能 //显示计算的结果 displayField.setText("" + result); } public static void main(String[] args) { //初始化计算器 Calculator calculator = new Calculator(); calculator.setResizable(false); calculator.setForeground(Color.MAGENTA); //关闭窗口,退出程序 calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}