java监听器
程序员文章站
2022-05-01 12:27:27
...
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
class Max extends Frame implements ActionListener,WindowListener{
TextField tf1 = new TextField();
TextField tf2 = new TextField();
TextField tf3 = new TextField();
TextField result = new TextField();
Button bt1 = new Button("计算");
Max(){
super("求三个数的最大值");
add(tf1,BorderLayout.WEST);
add(tf2,BorderLayout.NORTH);
add(tf3,BorderLayout.EAST);
add(result,BorderLayout.CENTER);
add(bt1,BorderLayout.SOUTH);
this.setSize(400,300);
this.setVisible(true);
bt1.addActionListener(this);
this.addWindowListener(this);
}
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(tf1.getText());
int b = Integer.parseInt(tf2.getText());
int c = Integer.parseInt(tf3.getText());
int max;
if (a> b){
max = a;
}else{
max = b;
}
if(c>max){
max = c;
}
result.setText(new Integer(max).toString());
}
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
e.getWindow().setVisible(false);
e.getWindow().dispose();
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
public class test {
/**
* @param args
*/
public static void main(String[] args) {
new Max();
}
}
上一篇: Linux(十一)权限管理