首页 > 代码库 > JAVA之GUI编程ACTION事件

JAVA之GUI编程ACTION事件

package GUI;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class StudyAction {
private Frame f;
private Button but;

StudyAction(){
    init();
}

private void init() {
    f=new Frame("Window ------by Von");
    f.setBounds(300, 100, 600, 500);
    f.setLayout(new FlowLayout());
   
    but=new Button("Click me to close");
    f.add(but);
    dealwithEvent();
    f.setVisible(true);
}

private void dealwithEvent() {
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e){  //区分大小写,WindowClosing则不可
            System.exit(0);
        }
    });
   
    but.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
           
        }
    });
}
public static void main(String[] args) {
    new StudyAction();
}
}