制作一个简易计算器 发表于 2018-04-20 | 分类于 java | 热度 ℃ | 字数统计 577 字 | 阅读时长 3 分钟 使用java的AWT包实现一个简易的计算器。 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179import java.awt.*;import java.awt.event.*;public class Calculator { private Frame frame; private Label label; private Panel panel; private Button button0,button1,button2,button3,button4,button5,button6,button7,button8, button9,buttonequ,buttondiv,buttondot,buttonmul,buttonsub,buttonadd; private ButtonHandler listener; public Calculator() { frame = new Frame("My Calculator"); label = new Label("0", Label.RIGHT); panel = new Panel(new GridLayout(4, 4)); button0 = new Button("0"); button1 = new Button("1"); button2 = new Button("2"); button3 = new Button("3"); button4 = new Button("4"); button5 = new Button("5"); button6 = new Button("6"); button7 = new Button("7"); button8 = new Button("8"); button9 = new Button("9"); buttonequ = new Button("="); buttondiv = new Button("/"); buttondot = new Button("."); buttonmul = new Button("*"); buttonsub = new Button("-"); buttonadd = new Button("+"); panel.add(button7); panel.add(button8); panel.add(button9); panel.add(buttonadd); panel.add(button4); panel.add(button5); panel.add(button6); panel.add(buttonsub); panel.add(button1); panel.add(button2); panel.add(button3); panel.add(buttonmul); panel.add(button0); panel.add(buttondot); panel.add(buttonequ); panel.add(buttondiv); frame.add(label, BorderLayout.NORTH); frame.add(panel, BorderLayout.CENTER); frame.pack(); frame.setSize(300,300); frame.setVisible(true); listener = new ButtonHandler(); //对按钮添加监听 button0.addActionListener(listener); button1.addActionListener(listener); button2.addActionListener(listener); button3.addActionListener(listener); button4.addActionListener(listener); button5.addActionListener(listener); button6.addActionListener(listener); button7.addActionListener(listener); button8.addActionListener(listener); button9.addActionListener(listener); buttonadd.addActionListener(listener); buttonsub.addActionListener(listener); buttonmul.addActionListener(listener); buttondiv.addActionListener(listener); buttondot.addActionListener(listener); buttonequ.addActionListener(listener); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } class ButtonHandler implements ActionListener { int flag = 0; //存储运算符 boolean equHappened = true; double arg1 = 0, arg2 = 0, result = 0; //@Override public void actionPerformed(ActionEvent e) { if(e.getSource() == buttonequ) { equHappened = true; if(flag==0) { result = arg1 + arg2; } else if(flag==1) { result = arg1 - arg2; } else if(flag==2) { result = arg1 * arg2; } else { result = arg1 / arg2; } label.setText(result+""); } else { if(equHappened == true) { label.setText(""); equHappened = false; } if(e.getSource() == buttondot) { label.setText("0."); } else if(e.getSource() == button0) { label.setText(label.getText()+"0"); arg2 = Double.parseDouble(label.getText()); } else if(e.getSource() == button1) { label.setText(label.getText()+"1"); arg2 = Double.parseDouble(label.getText()); } else if(e.getSource() == button2) { label.setText(label.getText()+"2"); arg2 = Double.parseDouble(label.getText()); } else if(e.getSource() == button3) { label.setText(label.getText()+"3"); arg2 = Double.parseDouble(label.getText()); } else if(e.getSource() == button4) { label.setText(label.getText()+"4"); arg2 = Double.parseDouble(label.getText()); } else if(e.getSource() == button5) { label.setText(label.getText()+"5"); arg2 = Double.parseDouble(label.getText()); } else if(e.getSource() == button6) { label.setText(label.getText()+"6"); arg2 = Double.parseDouble(label.getText()); } else if(e.getSource() == button7) { label.setText(label.getText()+"7"); arg2 = Double.parseDouble(label.getText()); } else if(e.getSource() == button8) { label.setText(label.getText()+"8"); arg2 = Double.parseDouble(label.getText()); } else if(e.getSource() == button9) { label.setText(label.getText()+"9"); arg2 = Double.parseDouble(label.getText()); } else if(e.getSource() == buttonadd) { label.setText(""); arg1 = arg2; flag = 0; } else if(e.getSource() == buttonsub) { label.setText(""); arg1 = arg2; flag = 1; } else if(e.getSource() == buttonmul) { label.setText(""); arg1 = arg2; flag = 2; } else if(e.getSource() == buttondiv) { label.setText(""); arg1 = arg2; flag = 3; } } } } public static void main(String[] args) { Calculator cal = new Calculator(); }} 并不难,只用到了AWT,一个小时做完,感兴趣的可以用swing改写一下。 donate the author Donate WeChat Pay Alipay