//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大家帮帮忙了呀
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大家帮帮忙了呀
解决方案 »
- 如何用AWT显示网页?
- java 中 Object 作为默认父类 为什么 出现错误 incompatible operand types Person and Object
- 菜鸟请教 i-- 和 i-1 输出结果不相同是为什么,
- 实现一个类似于五子棋的胜负判定算法,,高分,在线等。。。。
- 抽象类中可不可以有private变量?可不可以有private方法?
- 关于Jtable的问题
- ArrayList的用法,元素删除不了
- 如何把一个父类对象转换成一个子类对象,而一开始这个对象就是父类的对象!呵呵,这个问题有点菜!
- user.home是如何在得到的?(可能比较少见)
- 求高手指点
- 动态创建控件问题!!!!!!!!!!!!!!!!!!!!!!!!!(急)
- 时间格式化问题。。。。急
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) {
}
}