今天老师让做一个计算器,简单的布局自己可以完成,但是里面的判断代码不会 ,就去百度了一下,自己也把不懂的地方搞懂之后加了注释,分享给大家
import java.awt.*;
import java.awt.event.*; import javax.swing.*; /** * A simple calculator program. * <p>I saw this program in a QQ group, and help a friend correct it.</p> * * @author Singyuen Yip * @version 1.00 12/29/2009 * @see JFrame * @see ActionListener */ public class JCalculator extends JFrame implements ActionListener { /** * SerialVersionUID Java的序列化机制 */ private static final long serialVersionUID = -169068472193786457L; /** * This class help close the Window. * @author Singyuen Yip * */ private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent we) { System.exit(0); } } int i; // Strings for Digit & Operator buttons. 数字 和操作按钮 private final String[] str = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", ".", "0", "=", "+" }; // Build buttons.按钮数组 JButton[] buttons = new JButton[str.length]; // For cancel or reset. 取消 或者 重置清零按钮 JButton reset = new JButton("CE"); // Build the text field to show the result. 文本框display JTextField display = new JTextField("0"); /** * Constructor without parameters.构造函数没有参数 */ public JCalculator() { super("Calculator"); // Add a panel. 画板1 JPanel panel1 = new JPanel(new GridLayout(4, 4)); // panel1.setLayout(new GridLayout(4,4));画板1上网格布局 //将按钮添加到画板1上 for (i = 0; i < str.length; i++) { buttons[i] = new JButton(str[i]); panel1.add(buttons[i]); } //画板2 边界布局 JPanel panel2 = new JPanel(new BorderLayout()); // panel2.setLayout(new BorderLayout()); //画板2上添加文本框display 按钮reset panel2.add("Center", display); panel2.add("East", reset); // JPanel panel3 = new Panel(); 画板3 /*获取内容面板,因为JFrame不能直接添加组件, * 需要用getContentPane()函数获取内容面板, * 再在内容面板上进行添加组件*/ getContentPane().setLayout(new BorderLayout()); getContentPane().add("North", panel2); getContentPane().add("Center", panel1); // Add action listener for each digit & operator button. for (i = 0; i < str.length; i++) buttons[i].addActionListener(this); // Add listener for "reset" button. reset.addActionListener(this); // Add listener for "display" button. display.addActionListener(this); // The "close" button "X". addWindowListener(new WindowCloser()); // Initialize the window size. setSize(800, 800); // Show the window. // show(); Using show() while JDK version is below 1.5. setVisible(true); // Fit the certain size.适合一定的大小 /*关于pack()方法 在 Frame 类中有一个从类 java.awt.Window 继承的方法 pack() show() 同样也继承自 java.awt.Window public void pack() 调整此窗口的大小,以适合其子组件的首选大小和布局。 如果该窗口和/或其所有者仍不可显示,则两者在计算首 选大小之前变得可显示。在计算首选大小之后,将会验证 该 Window*/ pack(); } public void actionPerformed(ActionEvent e) { Object target = e.getSource();//target=输入的object String label = e.getActionCommand();//返回与此动作相关的命令字符串。 if (target == reset) handleReset();//后面有handleaReset方法的定义 else if ("0123456789.".indexOf(label) > 0) handleNumber(label);//后面有handleaNumber方法的定义 else handleOperator(label);//后面有handleaOperator方法的定义 } // Is the first digit pressed? boolean isFirstDigit = true; /** * Number handling. * @param key the key of the button. */ public void handleNumber(String key) { if (isFirstDigit) display.setText(key); else if ((key.equals(".")) && (display.getText().indexOf(".") < 0)) display.setText(display.getText() + "."); else if (!key.equals(".")) display.setText(display.getText() + key); isFirstDigit = false; } /** * Reset the calculator. */ public void handleReset() { display.setText("0"); isFirstDigit = true; operator = "="; } double number = 0.0; String operator = "="; /** * Handling the operation. * @param key pressed operator's key. */ public void handleOperator(String key) { if (operator.equals("+")) number += Double.valueOf(display.getText()); else if (operator.equals("-")) number -= Double.valueOf(display.getText()); else if (operator.equals("*")) number *= Double.valueOf(display.getText()); else if (operator.equals("/")) number /= Double.valueOf(display.getText()); else if (operator.equals("=")) number = Double.valueOf(display.getText()); display.setText(String.valueOf(number)); operator = key; isFirstDigit = true; } public static void main(String[] args) { new JCalculator(); } }