描述:一个简单的计算器小程序 可以实现+、-、*、/运算。具有清零和退格的功能。
主要是熟悉一下javaGUI编程以及内部类的相关知识
计算器面板类实现如下:
package calculator;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CalculatorPanel extends JPanel {
private JLabel display;
private JPanel buttonPanel;
private JPanel toolPanel;
private JPanel panel;
private double result;
private String lastCommand;
private boolean cls;
public CalculatorPanel(){
setLayout(new BorderLayout());
result = 0; //结果
lastCommand = "="; //上一个运算符
cls = true; //清屏标识符
//add display
display = new JLabel("0");
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
//add the button to a panel
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
addButton(panel,"7", insert);
addButto