package haha;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.plaf.synth.SynthStyle;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.omg.CORBA.SystemException;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
public class Gui {
public static void main(String[] args){
new MyGuiFrame().load();;
}
}
//不在需要把组件放到构造方法中
class MyGuiFrame extends Frame{
TextField num1,num2,num3;
public void load() {
setVisible(true);
setLocation(200,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Label adLabel = new Label("+");
Button but1 = new Button("=");
setLayout(new FlowLayout());
add(num1);
add(adLabel);
add(num2);
add(but1);
add(num3);
//监听器
but1.addActionListener(new MyActionListener());
pack();
}
//使用内部类不在需要获取
private class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
//获取输入框中的数值
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//吧结果放进第三个框
num3.setText(""+(n1+n2));
//前面两个置空
num1.setText("");
num2.setText("");
}
}
}
简易计算器 内部类
最新推荐文章于 2020-07-30 14:36:10 发布