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");
}
}
如何修复“关闭”按钮?
I am creating a small application using AWT. When I try to close the window, the "close" button doesn't work.
Here's my code:
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");
}
}
How can I fix the "close" button?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好使用方法
public void dispose()
为什么必须 dispose() 超出范围的 java.awt.Window?
It's better to use the method
public void dispose()
Why should you have to dispose() a java.awt.Window that goes out of scope?
你可以这样做:
You could do it like this:
尝试这样做:
Try doing it like this: