黑马程序员技术交流社区
标题:
awt 窗体事件,以下代码为什么关闭不了,求大神回复
[打印本页]
作者:
陈妙俊
时间:
2014-5-7 16:51
标题:
awt 窗体事件,以下代码为什么关闭不了,求大神回复
本帖最后由 陈妙俊 于 2014-5-7 17:15 编辑
import java.awt.event.*;
import java.awt.*;
public class AwtDemo01 {
public static void main(String[] args) {
Frame f=new Frame("my awt");
f.setSize(500,400);//设置大小
f.setLocation(300, 200); //设置本地位置
f.setLayout(new FlowLayout());
Button b=new Button("我是一个按钮"); //添加一个按钮
f.add(b);
//此处是一个事件监听机制,为什么我的没办法关闭,求解释
f.addWindowListener(new WindowAdapter(){
public void WindowClosd(WindowEvent e){
System.exit(0);
}
});
f.setVisible(true);
}
}
作者:
曹冬明
时间:
2014-5-7 16:57
你方法都写错了,是windowClosing吧
作者:
Up↑Lee↗
时间:
2014-5-7 17:02
import java.awt.event.*;
import java.awt.*;
public class AwtDemo01 {
public static void main(String[] args) {
Frame f=new Frame("my awt");
f.setSize(500,400);//设置大小
f.setLocation(300, 200); //设置本地位置
f.setLayout(new FlowLayout());
Button b=new Button("我是一个按钮"); //添加一个按钮
f.add(b);
//此处是一个事件监听机制,为什么我的没办法关闭,求解释
f.addWindowListener(new WindowAdapter(){
// public void WindowClosd(WindowEvent e){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setVisible(true);
}
}
复制代码
区分这两个方法:
windowClosing
void
windowClosing
(
WindowEvent
e)用户试图从窗口的系统菜单中关闭窗口时调用。
windowClosed
void
windowClosed
(
WindowEvent
e)因对窗口调用 dispose 而将其关闭时调用。
作者:
陈妙俊
时间:
2014-5-7 17:14
Up↑Lee↗ 发表于 2014-5-7 17:02
区分这两个方法:windowClosingvoid windowClosing(WindowEvent e)用户试图从窗口的系统菜单 ...
谢谢你们的回复,问题已经解决了,WindowClosing(WindowEvent e)这里是我写错了方法的"W"是小写才可以
作者:
gentleman
时间:
2014-5-7 17:52
import java.awt.event.*;
import java.awt.*;
public class AwtDemo01 {
public static void main(String[] args) {
Frame f=new Frame("my awt");
f.setSize(500,400);//设置大小
f.setLocation(300, 200); //设置本地位置
f.setLayout(new FlowLayout());
Button b=new Button("我是一个按钮"); //添加一个按钮
f.add(b);
//此处是一个事件监听机制,为什么我的没办法关闭,求解释
f.addWindowListener(new WindowAdapter(){
public void WindowClosd(WindowEvent e){//*********这里是复写的方法,名称必须一致应该是WindowClosing
System.exit(0);
}
});
f.setVisible(true);
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2