我很抱歉,如果这是一个的n00b问题,但我已经花路太长,这一次我创建的窗口侦听器,窗口事件,以及其他一切,我怎么指定调用什么方法?这是我的代码:
private static void mw() {
Frame frm = new Frame("Hello Java");
WindowEvent we = new WindowEvent(frm, WindowEvent.WINDOW_CLOSED);
WindowListener wl = null;
wl.windowClosed(we);
frm.addWindowListener(wl);
TextField tf = new TextField(80);
frm.add(tf);
frm.pack();
frm.setVisible(true);
}
我试图获取一个URL,然后下载它,我进行了其他所有工作,我只是试图关闭窗口。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FrameByeBye {
// The method we wish to call on exit.
public static void showDialog(Component c) {
JOptionPane.showMessageDialog(c, "Bye Bye!");
}
public static void main(String[] args) {
// creating/udpating Swing GUIs must be done on the EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame f = new JFrame("Say Bye Bye!");
// Swing's default behavior for JFrames is to hide them.
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
showDialog(f);
System.exit(0);
}
} );
f.setSize(300,200);
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}
此外Runtime.addShutdownHook(Thread)
,在关机之前,还应考虑对执行至关重要的任何操作。
这是该代码的AWT版本。
import java.awt.*;
import java.awt.event.*;
class FrameByeBye {
// The method we wish to call on exit.
public static void showMessage() {
System.out.println("Bye Bye!");
}
public static void main(String[] args) {
Frame f = new Frame("Say Bye Bye!");
f.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
showMessage();
System.exit(0);
}
} );
f.setSize(300,200);
f.setLocationByPlatform(true);
f.setVisible(true);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句