import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener {// *****全局变量********************
JTextField jtf = new JTextField(21);// 显示区初始显示0,长度40
boolean flagjtf = true;// 是否是0
float result;//结果存放
int calnum = 1;// 次数
String caltext = "";// 存放要显示的字符
enum Suanfa {
jia, jian, cheng, chu
};
Suanfa suanfa = null;// 标记是哪种算法
Suanfa lastsuanfa = null;// 上一次的算法
JTextArea jta = new JTextArea("历史记录:\n", 9, 30);// *****************************************
public Calculator(){//*****************初始设置********************
Container dingceng=getContentPane();//定义顶层容器
setLayout(new BorderLayout());//新建管理器
JPanel pxs=new JPanel();
JPanel phistory=new JPanel();
JPanel pall=new JPanel();
GridBagLayout allgb=new GridBagLayout();
pall.setLayout(allgb);
GridBagConstraints call=new GridBagConstraints();
jtf.setHorizontalAlignment(JTextField.RIGHT);//右对齐
jtf.setText("0");
pxs.add(jtf);
phistory.add(jta);
JButton bnum0=new JButton("0");
JButton bnum1=new JButton("1");
JButton bnum2=new JButton("2");
JButton bnum3=new JButton("3");
JButton bnum4=new JButton("4");
JButton bnum5=new JButton("5");
JButton bnum6=new JButton("6");
JButton bnum7=new JButton("7");
JButton bnum8=new JButton("8");
JButton bnum9=new JButton("9");
JButton bdot=new JButton(".");
JButton bequ=new JButton("=");
JButton bjia=new JButton("+");
JButton bjian=new JButton("-");
JButton bcheng=new JButton("×");
JButton bchu=new JButton("÷");
JButton bql=new JButton("C");
JButton bce=new JButton("CE");
JButton btuige=new JButton("←");
JPanel panniu=new JPanel();
GridBagLayout lanniu=new GridBagLayout();//新建网格组布局
panniu.setLayout(lanniu);
GridBagConstraints cons=new GridBagConstraints();
cons.fill=GridBagConstraints.BOTH;//有剩余空间,空着
cons.weightx=0;
cons.weighty=0;//增量域=0
cons.gridwidth=1;
cons.gridheight=1;//大小
cons.insets=new Insets(2,2,2,2); /*位置*/
cons.gridx=0;
cons.gridy=0;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bnum7,cons);
cons.gridx=1;
cons.gridy=0;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bnum8, cons);
cons.gridx=2;
cons.gridy=0;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bnum9, cons);
cons.gridx=3;
cons.gridy=0;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bchu, cons);
cons.gridx=4;
cons.gridy=0;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(btuige, cons);
cons.gridx=0;
cons.gridy=1;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bnum4, cons);
cons.gridx=1;
cons.gridy=1;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bnum5, cons);
cons.gridx=2;
cons.gridy=1;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bnum6, cons);
cons.gridx=3;
cons.gridy=1;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bcheng, cons);
cons.gridx=4;
cons.gridy=1;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bql, cons);
cons.gridx=0;
cons.gridy=2;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bnum1, cons);
cons.gridx=1;
cons.gridy=2;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bnum2, cons);
cons.gridx=2;
cons.gridy=2;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bnum3, cons);
cons.gridx=3;
cons.gridy=2;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bjian, cons);
cons.gridx=4;
cons.gridy=2;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bce, cons);
cons.gridx=0;
cons.gridy=3;
cons.gridheight=1;
cons.gridwidth=2;
panniu.add(bnum0,cons);
cons.gridx=2;
cons.gridy=3;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bdot, cons);
cons.gridx=3;cons.gridy=3;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bjia, cons);
cons.gridx=4;
cons.gridy=3;
cons.gridheight=1;
cons.gridwidth=1;
panniu.add(bequ, cons);
call.gridx=0;
call.gridy=0;
call.gridheight=1;
call.gridwidth=1;
pall.add(pxs,call);
call.gridx=0;call.gridy=1;
call.gridheight=1;
call.gridwidth=1;
pall.add(panniu,call);
call.gridx=1;call.gridy=0;
call.gridheight=2;
call.gridwidth=1;
pall.add(phistory,call);
dingceng.add(pall);//窗体设置
setSize(600,190);
setTitle("计算器");
setVisible(true);
setResizable(false);//*******************************************
//**************监听器***********************
bnum0.addActionListener(this);
bnum1.addActionListener(this);
bnum2.addActionListener(this);
bnum3.addActionListener(this);
bnum4.addActionListener(this);
bnum5.addActionListener(this);
bnum6.addActionListener(this);
bnum7.addActionListener(this);
bnum8.addActionListener(this);
bnum9.addActionListener(this);
bql.addActionListener(this);
btuige.addActionListener(this);
bce.addActionListener(this);
bequ.addActionListener(this);
bjia.addActionListener(this);
bjian.addActionListener(this);
bcheng.addActionListener(this);
bchu.addActionListener(this);
bdot.addActionListener(this);
jtf.addActionListener(this);//*******************************************
}// **************按钮事件***********************
public void actionPerformed(ActionEvent e){
String gotbutton=e.getActionCommand(); /*按数字*/
if(gotbutton=="0") {
if(flagjtf) {
jtf.setText("0");
} else{
String currenttext1=jtf.getText();
jtf.setText(currenttext1+"0");
flagjtf=false;
}
jtf.requestFocus();
} else if(gotbutton=="1"){
if(flagjtf) {
jtf.setText("1");
flagjtf=false;
} else{
String currenttext1=jtf.getText();
jtf.setText(currenttext1+"1");
flagjtf=false;
}
jtf.requestFocus();
} else if(gotbutton=="2") {
if(flagjtf) {
jtf.setText("2");
flagjtf=false;
} else{
String currenttext1=jtf.getText();
jtf.setText(currenttext1+"2");
flagjtf=false;
}
jtf.requestFocus();
} else if(gotbutton=="3") {
if(flagjtf) {
jtf.setText("3");
flagjtf=false;
} else{
String currenttext1=jtf.getText();
jtf.setText(currenttext1+"3");
flagjtf=false;
}
jtf.requestFocus();
} else if(gotbutton=="4") {
if(flagjtf) {
jtf.setText("4");
flagjtf=false;
} else{
String currenttext1=jtf.getText();
jtf.setText(currenttext1+"4");
flagjtf=false;
}
jtf.requestFocus();
} else if(gotbutton=="5") {
if(flagjtf) {
jtf.setText("5");
flagjtf=false;
} else{
String currenttext1=jtf.getText();
jtf.setText(currenttext1+"5");
flagjtf=false;
}
jtf.requestFocus();
} else if(gotbutton=="6") {
if(flagjtf) {
jtf.setText("6");
flagjtf=false;
} else{
String currenttext1=jtf.getText();
jtf.setText(currenttext1+"6");
flagjtf=false;
}
jtf.requestFocus();
} else if(gotbutton=="7") {
if(flagjtf) {
jtf.setText("7");flagjtf=false;
} else{
String currenttext1=jtf.getText();
jtf.setText(currenttext1+"7");
flagjtf=false;
}
jtf.requestFocus();
} else if(gotbutton=="8") {
if(flagjtf) {
jtf.setText("8");
flagjtf=false;
} else{
String currenttext1=jtf.getText();
jtf.setText(currenttext1+"8");
flagjtf=false;
}
jtf.requestFocus();
} else if(gotbutton=="9") {
if(flagjtf) {
jtf.setText("9");
flagjtf=false;
}else{
String currenttext1=jtf.getText();
jtf.setText(currenttext1+"9");
flagjtf=false;
}
jtf.requestFocus();
}//小数点
else if(gotbutton==".") {
if(flagjtf) {
jtf.setText("0.");
flagjtf=false;
} else{
String currenttext1=jtf.getText();
jtf.setText(currenttext1+".");
flagjtf=false;
}
jtf.requestFocus();
}/*清零*/
else if(gotbutton=="C") {
jtf.setText("0");
flagjtf=true;
jtf.requestFocus();
}/*CE*/
else if(gotbutton=="CE") {
jtf.setText("0");
flagjtf=true;
result=0;
caltext="";
lastsuanfa=null;
suanfa=null;
jtf.requestFocus();
calnum=1;}/*退格*/
else if(gotbutton=="←") {
if(flagjtf) {
} else{
String currenttext1=jtf.getText();
int jtfnum= currenttext1.length() ;
if (currenttext1.length()==1) {
jtf.setText("0");
flagjtf=true;
} else{
jtf.setText(currenttext1.substring(0,jtfnum-1));
}
}
jtf.requestFocus();
}/*加法*/
else if(gotbutton=="+") {
if(calnum==1){
calnum=2;
lastsuanfa=Suanfa.jia;
suanfa=Suanfa.jia; //计算
result=Float.valueOf(jtf.getText()).floatValue(); //显示
caltext=jtf.getText()+"+";
jtf.setText("0");
flagjtf=true;//重置
jtf.requestFocus();
} else{
lastsuanfa=suanfa;
suanfa=Suanfa.jia;
calnum=2; //计算
suan(Float.valueOf(jtf.getText()).floatValue()); //显示
caltext=caltext+jtf.getText()+"+";
jtf.setText("0");flagjtf=true;//重置
}
}/*减法*/
else if(gotbutton=="-") {
if(calnum==1){
calnum=2;
lastsuanfa=Suanfa.jian;
suanfa=Suanfa.jian; //计算
suan(Float.valueOf(jtf.getText()).floatValue()); //显示
caltext=caltext+jtf.getText()+"-";
jtf.setText("0");
flagjtf=true;//重置
}
} /*乘法*/
else if(gotbutton=="×") {
if(calnum==1){
calnum=2;
lastsuanfa=Suanfa.cheng;
suanfa=Suanfa.cheng; //计算
result=Float.valueOf(jtf.getText()).floatValue(); //显示
caltext=jtf.getText()+"×";
jtf.setText("0");
flagjtf=true;//重置
jtf.requestFocus();
} else{
lastsuanfa=suanfa;
suanfa=Suanfa.cheng;
calnum=2; //计算
suan(Float.valueOf(jtf.getText()).floatValue()); //显示
caltext=caltext+jtf.getText()+"×";
jtf.setText("0");
flagjtf=true;//重置
}
} /*除法*/
else if(gotbutton=="÷") {
if(calnum==1){
calnum=2;
lastsuanfa=Suanfa.chu;
suanfa=Suanfa.chu; //计算
result=Float.valueOf(jtf.getText()).floatValue(); //显示
caltext=jtf.getText()+"÷";
jtf.setText("0");
flagjtf=true;//重置
jtf.requestFocus();
} else{
lastsuanfa=suanfa;
suanfa=Suanfa.chu;
calnum=2; //计算
suan(Float.valueOf(jtf.getText()).floatValue()); //显示
caltext=caltext+jtf.getText()+"÷";
jtf.setText("0");
flagjtf=true;//重置
} } /*====*/
else if(gotbutton=="=") {
lastsuanfa=suanfa;
suan(Float.valueOf(jtf.getText()).floatValue()); //显示
caltext=caltext+jtf.getText()+"="+result;
jta.append(caltext+"\n");
jtf.setText(String.valueOf(result));
flagjtf=true;//重置
calnum=1;
result=0;
caltext="";
jtf.requestFocus();
}
}// *******************************************
//*****************主函数********************
public static void main(String[] args) {
Calculator mycal = new Calculator();
mycal.setVisible(true);
mycal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}// *******************************************
//**************计算*****************************
public void suan(float x) {
if(lastsuanfa==Suanfa.jia)
result+=x;
else if(lastsuanfa==Suanfa.jian)
result-=x;
else if(lastsuanfa==Suanfa.cheng)
result*=x;
else if(lastsuanfa==Suanfa.chu)
result/=x;
}// *******************************************
}
运行效果: