JAVA 编写计算器 要代码最简单的

2024-12-15 09:15:14
推荐回答(1个)
回答1:

//声明需要插入的包
import java.awt.*;
import java.lang.Object;
        import java.lang.String;
import javax.swing.*;
import java.awt.event.*;
import java.awt.TextComponent;
//声明一个主类jisuanqi,继续窗口类Frame
public class jisuanqi extends Frame{
//声明16个Button
Button anjian1, anjian2, anjian3, anjian4, anjian5, anjian6, anjian7, anjian8, anjian9, anjian10, anjian11, anjian12, anjian13, anjian14,anjian15,anjian16;
//声明文本框,用于接收数字和计算
TextField numText;
//声明面板容器,用于存放各种按钮
Panel anjianPanel;
//构造方法初始化按钮
jisuanqi(String title){
super(title);
anjian1=new Button("0");
anjian2=new Button("1");
anjian3=new Button("2");
anjian4=new Button("3");
anjian5=new Button("4");
anjian6=new Button("5");
anjian7=new Button("6");
anjian8=new Button("7");
anjian9=new Button("8");
anjian10=new Button("9");
anjian11=new Button("+");
anjian12=new Button("-");
anjian13=new Button("*");
anjian14=new Button("/");
anjian15=new Button("=");
anjian16=new Button("清空");
//初始化文本框,将文本的值设置为“0”
numText=new TextField("0");
//实例化面板;
anjianPanel=new Panel();
//设置按钮
anjianPanel.setLayout(new FlowLayout());
//实例化按钮
anjian1.addActionListener(new anjianAction());
anjian2.addActionListener(new anjianAction());
anjian3.addActionListener(new anjianAction());
anjian4.addActionListener(new anjianAction());
anjian5.addActionListener(new anjianAction());
anjian6.addActionListener(new anjianAction());
anjian7.addActionListener(new anjianAction());
anjian8.addActionListener(new anjianAction());
anjian9.addActionListener(new anjianAction());
anjian10.addActionListener(new anjianAction());
anjian11.addActionListener(new anjianAction());
anjian12.addActionListener(new anjianAction());
anjian13.addActionListener(new anjianAction());
anjian14.addActionListener(new anjianAction());
anjian15.addActionListener(new anjianAction());
anjian16.addActionListener(new anjianAction());
//将各个按钮添加到面板容器中
anjianPanel.add(anjian1);
anjianPanel.add(anjian2);
anjianPanel.add(anjian3);
anjianPanel.add(anjian4);
anjianPanel.add(anjian5);
anjianPanel.add(anjian6);
anjianPanel.add(anjian7);
anjianPanel.add(anjian8);
anjianPanel.add(anjian9);
anjianPanel.add(anjian10);
anjianPanel.add(anjian11);
anjianPanel.add(anjian12);
anjianPanel.add(anjian13);
anjianPanel.add(anjian14);
anjianPanel.add(anjian15);
anjianPanel.add(anjian16);
//为文本框添加事件
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(anjianPanel,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 anjianAction 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("0");
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){
jisuanqi jisuanqi=new jisuanqi("计数器");
jisuanqi.setSize(300,280);
//设置窗口大小
jisuanqi.setLocation(550,350);
//设置界面属性
jisuanqi.setVisible(true);
}}

如有疑问请继续追问,我将竭尽全力为您解答