修改一下啊 太多了
import java.awt.*;
import java.lang.Object;
import java.awt.event.*;
import java.lang.String;
import javax.swing.*;
import java.awt.TextComponent;
public class test extends Frame{
//声明16个Button
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14,btn15,btn16;
//声明1个文本框,用于接收数字和操作
TextField numText;
//声明一个面板专门用于存放按钮
Panel btnPanel;
//构造函数初始话按钮
public test(String title){
super(title);
btn1=new Button("0");
btn2=new Button("1");
btn3=new Button("2");
btn4=new Button("3");
btn5=new Button("4");
btn6=new Button("5");
btn7=new Button("6");
btn8=new Button("7");
btn9=new Button("8");
btn10=new Button("9");
btn11=new Button("+");
btn12=new Button("-");
btn13=new Button("*");
btn14=new Button("/");
btn15=new Button("=");
btn16=new Button("清空");
//将文本的初始值设置成0
numText=new TextField("");
//实例化放按钮的Panel;
btnPanel=new Panel();
//将布局模式设置成流式布局
btnPanel.setLayout(new GridLayout(4,4,5,3));
//为每个按钮添加事件
btn1.addActionListener(new btnAction());
btn2.addActionListener(new btnAction());
btn3.addActionListener(new btnAction());
btn4.addActionListener(new btnAction());
btn5.addActionListener(new btnAction());
btn6.addActionListener(new btnAction());
btn7.addActionListener(new btnAction());
btn8.addActionListener(new btnAction());
btn9.addActionListener(new btnAction());
btn10.addActionListener(new btnAction());
btn11.addActionListener(new btnAction());
btn12.addActionListener(new btnAction());
btn13.addActionListener(new btnAction());
btn14.addActionListener(new btnAction());
btn15.addActionListener(new btnAction());
btn16.addActionListener(new btnAction());
//将按钮添加到Panel
btnPanel.add(btn1);
btnPanel.add(btn2);
btnPanel.add(btn3);
btnPanel.add(btn4);
btnPanel.add(btn5);
btnPanel.add(btn6);
btnPanel.add(btn7);
btnPanel.add(btn8);
btnPanel.add(btn9);
btnPanel.add(btn10);
btnPanel.add(btn11);
btnPanel.add(btn12);
btnPanel.add(btn13);
btnPanel.add(btn14);
btnPanel.add(btn15);
btnPanel.add(btn16);
//为文本框添加事件
numText.addTextListener(new TextListener() {
public void textValueChanged(TextEvent e) {
if(numText.getText().indexOf("0",0)!=-1){
numText.getText().replace("0","");
}
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//将文本框放在北部
add(numText,BorderLayout.NORTH);
//将按钮放于中部
add(btnPanel,BorderLayout.CENTER);
//将文本设置成不可操作
numText.setEditable(false);
}
//没有任何操作的值
int num=0;
//numStr是文本框的值
String numStr=null;
Boolean chinage=false;
//单击按钮的方法
public int check(){
numStr = numText.getText();
//判断文本是否已经有了+,-,*,/
if (numStr.indexOf("+") == 0) {
numStr = numStr.replace('+', '0');
num = num + Integer.parseInt(numStr);
return num;
}else
if (numStr.indexOf("-") != -1) {
numStr = numStr.replace("-", "0");
num = num - Integer.parseInt(numStr);
return num;
}
else
if(numStr.indexOf("*")!=-1){
numStr=numStr.replace('*','0');
num = num * Integer.parseInt(numStr);
return num;
}
else
if(numStr.indexOf("/")!=-1){
numStr=numStr.replace('/','0');
try{
num = num / Integer.parseInt(numStr);
return num;
}catch(ArithmeticException e){
JOptionPane.showMessageDialog(null,"除数不能为空!","消息!",1);
return num;
}
}
else
return num=Integer.parseInt(numStr);
}
//创建事件
//这里面的东西都很简单主要就是改变文本的值和判断操作
private class btnAction implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getActionCommand()=="0"){
if(!numText.getText().equals("0"))
numText.setText(numText.getText()+0);
}
if(event.getActionCommand()=="1")
numText.setText(numText.getText()+1);
if(event.getActionCommand()=="2")
numText.setText(numText.getText()+2);
if(event.getActionCommand()=="3")
numText.setText(numText.getText()+3);
if(event.getActionCommand()=="4")
numText.setText(numText.getText()+4);
if(event.getActionCommand()=="5")
numText.setText(numText.getText()+5);
if(event.getActionCommand()=="6")
numText.setText(numText.getText()+6);
if(event.getActionCommand()=="7")
numText.setText(numText.getText()+7);
if(event.getActionCommand()=="8")
numText.setText(numText.getText()+8);
if(event.getActionCommand()=="9")
numText.setText(numText.getText()+9);
if(event.getActionCommand()=="清空"){
numText.setText("");
num=0;
}
if(event.getActionCommand()=="+"){
//调用上面的check();方法
check();
numText.setText("+");
}
if(event.getActionCommand()=="-"){
check();
numText.setText("-");
}
if(event.getActionCommand()=="*"){
check();
numText.setText("*");
}
if(event.getActionCommand()=="/"){
if(numStr=="/0"){
JOptionPane.showMessageDialog(null,"除数不能为空!","消息!",1);
}
check();
numText.setText("/");
}
if(event.getActionCommand()=="="){
check();
numText.setText(""+num);
}}}
public static void main(String[] args){
test test=new test("计数器");
test.setSize(200,180);
//设置界面的位置
test.setLocation(500,300);
//让界面可现
test.setVisible(true);
}}