[英]How to close the window in AWT?
我正在使用AWT創建一個小應用程序。 當我嘗試關閉窗口時,“關閉”按鈕不起作用。
這是我的代碼:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
class ButtonDemo1 implements ActionListener {
Button b1;
TextField tf;
Frame f;
ButtonDemo1(String s) {
f = new Frame(s);
b1 = new Button("OK");
tf = new TextField(10);
f.setSize(200, 250);
f.setVisible(true);
b1.addActionListener(this);
f.add(tf);
f.add(b1);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
f.setLayout(new FlowLayout());
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
tf.setText("Press Ok");
}
}
public static void main(String args[]) {
new ButtonDemo1("First");
}
}
如何修復“關閉”按鈕?
最好使用方法public void dispose()
你為什么要處理一個超出范圍的java.awt.Window?
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
}
);
你可以這樣做:
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
嘗試這樣做:
class ExampleClass implements ActionListener, WindowListener
{
...
f.addWindowListener(this);
...
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.