[英]Why does my AWT Java Window not close, when I push X to the close window?
Hi I created this java code for AWT Window嗨,我为 AWT Window 创建了这个 java 代码
package labelExample;
import java.awt.*;
import java.awt.event.*;
public class labelExample extends Frame implements ActionListener{
Frame f;
TextField tf; Label l; Button b;
labelExample(){
Frame f=new Frame("Label Example");
tf=new TextField("www.google.de");
tf.setBounds(50,50, 150,20);
l=new Label();
l.setBounds(50,100, 250,20);
b=new Button("Find IP");
b.setBounds(50,150,60,30);
b.addActionListener(this);
add(b);add(tf);add(l);
setSize(400,400);
setLayout(null);
setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
f.dispose(); // use dispose method
}
}
);
}
public void actionPerformed(ActionEvent e) {
try{
String host=tf.getText();
String ip=java.net.InetAddress.getByName(host).getHostAddress();
l.setText("IP of "+host+" is: "+ip);
}catch(Exception ex){System.out.println(ex);}
}
public static void main(String[] args) {
new labelExample();
}
}
When I try to close the window by pushing the X button, nothing happens.当我尝试通过按下 X 按钮关闭窗口时,没有任何反应。 The windows stays open.
窗户保持打开状态。 I made these changes my example code: public class labelExample extends Frame implements ActionListener{
我对我的示例代码进行了这些更改: public class labelExample extends Frame implements ActionListener{
I added new awt-frame f with name the "Label Example".我添加了名为“标签示例”的新 awt-frame f。
frame f;框架 f; frame f=new Frame("Label Example");
frame f=new Frame("标签示例"); And added this code to close the frame
并添加了此代码以关闭框架
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
f.dispose(); // use dispose method
}
}
);
How do have change my code to fix this problem ?如何更改我的代码来解决这个问题?
I believe that in labelExample(){ ... }
you should add a line setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
我相信在
labelExample(){ ... }
你应该添加一行setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
to close the window.关闭窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.