Button添加事件监听器问题

cyhgohappy 2007-08-15 04:20:28
//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

大家帮帮忙了呀
...全文
344 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxwdsky 2007-08-16
  • 打赏
  • 举报
回复
我改了一下,可以了,你运行以下,看看。
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) {
}
}
qizhe6783 2007-08-15
  • 打赏
  • 举报
回复
你试试在运行里写notepad.exe看看是否能运行,我把你的代码粘下来好用啊

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧