Button添加事件监听器问题
//ButtonListener.java
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.event.ActionEvent;
public class ButtonListener implements ActionListener {
/**
* Method actionPerformed
*
*
* @param e
*
*/
public void actionPerformed(ActionEvent e) {
// TODO: Add your code here
try{
System.out.print("happy!");
Runtime.getRuntime().exec("notepad.exe");
}catch(Exception er){
System.out.println(er.getMessage());
}
}
}
//MyWindowListener.java
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.Window;
public class MyWindowListener implements WindowListener {
/**
* Method windowOpened
*
*
* @param e
*
*/
public void windowOpened(WindowEvent e) {
// TODO: Add your code here
}
/**
* Method windowClosing
*
*
* @param e
*
*/
public void windowClosing(WindowEvent e) {
// TODO: Add your code here
e.getWindow().setVisible(false);
((Window)e.getComponent()).dispose();
System.exit(0);
}
/**
* Method windowClosed
*
*
* @param e
*
*/
public void windowClosed(WindowEvent e) {
// TODO: Add your code here
}
/**
* Method windowIconified
*
*
* @param e
*
*/
public void windowIconified(WindowEvent e) {
// TODO: Add your code here
}
/**
* Method windowDeiconified
*
*
* @param e
*
*/
public void windowDeiconified(WindowEvent e) {
// TODO: Add your code here
}
/**
* Method windowActivated
*
*
* @param e
*
*/
public void windowActivated(WindowEvent e) {
// TODO: Add your code here
}
/**
* Method windowDeactivated
*
*
* @param e
*
*/
public void windowDeactivated(WindowEvent e) {
// TODO: Add your code here
}
/**
* Method actionPerformed
*
*
* @param e
*
*/
}
//Test.java
import java.awt.*;
public class Test {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: Add your code here
Frame f=new Frame("Test");
Button b=new Button("OK");
b.addActionListener(new ButtonListener());
f.add(b);
f.setSize(300,300);
f.setVisible(true);
f.addWindowListener(new MyWindowListener());
}
}
-----
理论上我点击按钮的话,记事本会跳出来才对啊,不过为什么完全没反映呢
而窗口监听器设置关闭到是OK
大家帮帮忙了呀