//ButtonListener.javaimport 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.javaimport 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.javaimport 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大家帮帮忙了呀

解决方案 »

  1.   

    你试试在运行里写notepad.exe看看是否能运行,我把你的代码粘下来好用啊
      

  2.   

    我改了一下,可以了,你运行以下,看看。
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    public class Test {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());
    }
    }
    class ButtonListener implements ActionListener {
    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());
    }
    }
    }
    class MyWindowListener implements WindowListener {public void windowOpened(WindowEvent e) {
    // TODO: Add your code here
    }public void windowClosing(WindowEvent e) {
    // TODO: Add your code here
    e.getWindow().setVisible(false);
    ((Window)e.getComponent()).dispose();
    System.exit(0);
    }public void windowClosed(WindowEvent e) {
    // TODO: Add your code here
    }public void windowIconified(WindowEvent e) {
    // TODO: Add your code here
    }    
        public void windowDeiconified(WindowEvent e) {
        }    public void windowActivated(WindowEvent e) {
        }    public void windowDeactivated(WindowEvent e) {
        }
    }