import 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;
private char[] c;
public Calculator(){
super("Calculator");
container=getContentPane();
layout=new GridBagLayout();
container.setLayout(layout);
constraints=new GridBagConstraints();
start=true;
result=0;
constraints.fill=GridBagConstraints.BOTH;
displayfield=new JTextField(20);
displayfield.setHorizontalAlignment(JTextField.RIGHT);
displayfield.setForeground(Color.BLUE);
constraints.gridx=0;
constraints.gridy=0;
constraints.gridwidth=4;
constraints.gridheight=2;
constraints.fill=GridBagConstraints.BOTH;
constraints.weightx=50;
constraints.weighty=50;
layout.setConstraints(displayfield,constraints);
container.add(displayfield);
ActionListener insert = new InsertAction(); //定义两个监视器
ActionListener command = new CommandAction();
KeyListener keyinsert =new InsertKey();
addButton("Back",0,2,1,1,insert,keyinsert); //通过定义好的方法添加按钮
addButton("CE",1,2,1,1,insert,keyinsert);
addButton("CLEAR",2,2,2,1,insert,keyinsert);
addButton("7",0,3,1,1,insert,keyinsert);
addButton("8",1,3,1,1,insert,keyinsert);
addButton("9",2,3,1,1,insert,keyinsert);
addButton("+",3,3,1,1,insert,keyinsert);
addButton("4",0,4,1,1,insert,keyinsert);
addButton("5",1,4,1,1,insert,keyinsert);
addButton("6",2,4,1,1,insert,keyinsert);
addButton("-",3,4,1,1,insert,keyinsert);
addButton("1",0,5,1,1,insert,keyinsert);
addButton("2",1,5,1,1,insert,keyinsert);
addButton("3",2,5,1,1,insert,keyinsert);
addButton("*",3,5,1,1,insert,keyinsert);
addButton("+/-",0,6,1,1,insert,keyinsert);
addButton("0",1,6,1,1,insert,keyinsert);//只显示"-"号,"+"没有实用价值
addButton(".",2,6,1,1,insert,keyinsert);
addButton("/",3,6,1,1,insert,keyinsert);
addButton("(",0,7,1,1,insert,keyinsert);
addButton(")",1,7,1,1,insert,keyinsert);
addButton("=",2,7,2,1,command,keyinsert);
setSize(300,300); //设置窗口的大小
setVisible(true); //设置窗口可见
}
private void addButton(String label,int row,int column,int with,int height,ActionListener listener ,KeyListener key ) {
//定义添加按钮的方法
JButton button=new JButton(label);
constraints.gridx=row;
constraints.gridy=column;
constraints.gridwidth=with;
constraints.gridheight=height;
constraints.fill=GridBagConstraints.BOTH;
button.addActionListener(listener);
button.addKeyListener(key);
layout.setConstraints(button,constraints);
container.add(button);
constraints.insets=new Insets(1,1,1,1);//设置按钮间的间距
button.setForeground(Color.MAGENTA);
button.setBorderPainted(true);
button.setContentAreaFilled(true);
button.setBounds(2,2,2,2);
button.contains(5,5);
//button.setEnabled(false);
}
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("Back")) {
if(displayfield.getText().length()>0) {
displayfield.setText(displayfield.getText().substring(0, displayfield.getText().length()-1));
}//如果单击退格,实现退格的功能
}
else if(input.equals("CE")||input.equals("CLEAR")) {
displayfield.setText("");
start=true;
}
else
displayfield.setText(displayfield.getText()+input);
}
}
}
private class InsertKey implements KeyListener {
//定义Insert事件的方法
public void keyPressed(KeyEvent event) {
if (start) {
displayfield.setText(""); //默认计算区域为空
start=false;
}
if(event.getKeyCode()==KeyEvent.VK_BACK_SPACE){
if(displayfield.getText().l