import
javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Calculate extends Frame{ TextField num1,num2,num3; public Calculate() { //三个文字域 num1 = new TextField(10); num2 = new TextField(10); num3 = new TextField(20); //一个按钮 Button button = new Button("="); button.addActionListener(new CalculateListener()); //一个标签 Label label = new Label("+"); setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } class CalculateListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { int i1 = Integer.parseInt( num1.getText()); int i2 = Integer.parseInt( num2.getText()); num1.setText(""); num2.setText(""); num3.setText(String.valueOf(i1+i2)); } } public static void main(String[] args) { new Calculate(); } }

 

 

一个简易的加法计算器

利用到了事件监听器

监听按钮的变换从而使按钮有所作为。

在这个方法中完整的实现了真正的Java的面向对象。

而不是面向过程

利用方法与属性 构建了这个加法计算器

其中利用了内部类:最大的好处就是们可以畅通无阻的访问外部的属性和方法!

 

相关文章:

  • 2021-11-20
  • 2022-01-12
  • 2021-10-30
  • 2022-12-23
  • 2021-12-24
  • 2021-06-02
  • 2022-12-23
  • 2021-12-02
猜你喜欢
  • 2021-08-15
  • 2021-11-27
  • 2021-06-08
  • 2021-12-08
  • 2022-02-09
  • 2022-01-17
  • 2021-11-28
相关资源
相似解决方案