A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. public class CalculatorFrame extends Frame implements ActionListener
  4. {
  5. private TextField text1,text2,text3;
  6. private Button button_1,button_2,button_3,button_4,button_5,button_plus,button_subtract,button_multiply,button_divide,button_cancel;
  7. private Label label1,label2,label3;
  8. public CalculatorFrame()
  9. {
  10. super("CalculatorFrame");

  11. this.setSize(320,120);
  12. this.setBackground(Color.lightGray);
  13. this.setLocation(300,240);
  14. this.setLayout(new FlowLayout(FlowLayout.LEFT));
  15. label1=new Label("shu1:");
  16. add(label1);
  17. text1 = new TextField(20);
  18. text1.addActionListener(this);
  19. this.add(text1);
  20. label1=new Label("shu2:");
  21. add(label2);
  22. text2 = new TextField(20);
  23. text2.addActionListener(this);
  24. this.add(text2);
  25. label3=new Label("=");
  26. add(label3);
  27. text3 = new TextField(20);
  28. text3.addActionListener(this);
  29. this.add(text3);
  30. button_1 = new Button("1");
  31. button_2 = new Button("2");
  32. button_3 = new Button("3");
  33. button_4 = new Button("4");
  34. button_5 = new Button("5");
  35. button_plus = new Button("+");
  36. button_subtract=new Button("-");
  37. button_multiply=new Button("*");
  38. button_divide=new Button("/");
  39. button_cancel = new Button("C");
  40. this.add(button_1);
  41. this.add(button_2);
  42. this.add(button_3);
  43. this.add(button_4);
  44. this.add(button_5);
  45. this.add(button_plus);
  46. this.add(button_subtract);
  47. this.add(button_multiply);
  48. this.add(button_divide);
  49. this.add(button_cancel);
  50. button_1.addActionListener(this);
  51. button_2.addActionListener(this);
  52. button_3.addActionListener(this);
  53. button_4.addActionListener(this);
  54. button_5.addActionListener(this);
  55. button_plus.addActionListener(this);
  56. button_subtract.addActionListener(this);
  57. button_multiply.addActionListener(this);
  58. button_divide.addActionListener(this);
  59. button_cancel.addActionListener(this);
  60. this.setVisible(true);

  61. this.addWindowListener(new WinClose());

  62. }
  63. public void actionPerformed(ActionEvent e)
  64. { if (e.getSource()==button_plus)
  65. {text3.setText(Float.valueOf(Float.valueOf(text1.getText())+Float.valueOf(text2.getText())).toString());}
  66. if (e.getSource()==button_subtract)
  67. {text3.setText(Float.valueOf(Float.valueOf(text1.getText())-Float.valueOf(text2.getText())).toString()); }
  68. if (e.getSource()==button_multiply)
  69. {text3.setText(Float.valueOf(Float.valueOf(text1.getText())*Float.valueOf(text2.getText())).toString());}
  70. if (e.getSource()==button_divide)
  71. {text3.setText(Float.valueOf(Float.valueOf(text1.getText())/Float.valueOf(text2.getText())).toString());}
  72. else
  73. {text1.setText("");
  74. text2.setText("");
  75. text3.setText("");}
  76. }
  77. public static void main(String arg[])
  78. {
  79. new CalculatorFrame();
  80. }
  81. }

  82. class WinClose implements WindowListener
  83. {
  84. public void windowClosing(WindowEvent e)
  85. {
  86. System.exit(0);
  87. }
  88. public void windowOpened(WindowEvent e) { }
  89. public void windowActivated(WindowEvent e) { }
  90. public void windowDeactivated(WindowEvent e) { }
  91. public void windowClosed(WindowEvent e) { }
  92. public void windowIconified(WindowEvent e) { }
  93. public void windowDeiconified(WindowEvent e) { }
  94. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马