我在awt中创建一个samll应用程序,当我关闭窗口时,关闭按钮不起作用,但是我已经添加了关闭按钮的功能,之后关闭按钮不起作用...
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");
}
}
为什么关闭按钮不起作用?
答案 0 :(得分:13)
最好使用方法public void dispose()
Why should you have to dispose() a java.awt.Window that goes out of scope?
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
}
);
答案 1 :(得分:9)
你可以这样做:
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
答案 2 :(得分:3)
尝试这样做:
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);
}
}