- Java code
import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class WindowColseing extends Frame { Frame window; public static void main(String[] args) { WindowColseing c = new WindowColseing(); c.go(); } public void go() { window = new Frame("关闭窗口事件"); window.setBackground(Color.RED); window.setSize(300, 300); window.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub dispose(); } }); window.setVisible(true); }}
刚学JAVA事件, 不知是那里错了,就是不能关闭,高手赐教帮小弟看看那里错了..
------解决方案--------------------------------------------------------
package ycq.udplistener;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class NewJFrame extends Frame {
// Frame window;
public static void main(String[] args) {
NewJFrame c = new NewJFrame();
c.go();
}
public void go() {
// window = new Frame("关闭窗口事件");
setBackground(Color.RED);
setSize(300, 300);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
dispose();
}
});
setVisible(true);
}
}