簡體   English   中英

當我將 X 推到關閉窗口時,為什么我的 AWT Java 窗口沒有關閉?

[英]Why does my AWT Java Window not close, when I push X to the close 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();  
    }  
    }

當我嘗試通過按下 X 按鈕關閉窗口時,沒有任何反應。 窗戶保持打開狀態。 我對我的示例代碼進行了這些更改: public class labelExample extends Frame implements ActionListener{
我添加了名為“標簽示例”的新 awt-frame f。
框架 f; frame f=new Frame("標簽示例"); 並添加了此代碼以關閉框架

f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
         f.dispose(); // use dispose method 
         }
         }
         );

如何更改我的代碼來解決這個問題?

我相信在labelExample(){ ... }你應該添加一行setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 關閉窗口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM