2017-04-11 111 views
0

我试图从另一个类ABC的名为testView的类中显示一个窗口。窗口包含一个按钮。我想关闭该按钮上的窗口。我如何关闭它?关闭按钮上的容器单击

public class testView extends JFrame { 

    protected JButton closeButton = new JButton("Close"); 

    testView(){ 

    this.setSize(1000,700); 
    this.setTitle("Test"); 
    Container window = getContentPane(); 
    window.setLayout(new FlowLayout()); 
    this.setResizable(false); 

    window.add(closeButton); 
    } 
} 

public class ABC{ 
    public static void main(String[] args) { 

    testView View = new testView(); 

    View.setVisible(true); 
    } 
} 

从另一个类ABC显示窗口。如何关闭按钮单击窗口?

+0

只需使用Dispose()方法关闭它 –

回答

1

你可以做这样的事情:

button.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) 
    { 
     frameToClose.dispose(); 
    } 
}); 

该代码添加一个按钮动作监听器,那么它告诉框架时关闭该按钮具有一个在其行动的行动。希望这有助于:)

0

为了使窗口看不见你必须调用

java.awt.Window.setVisible(false) 

但随着dispose方法,你从内存中删除你的窗口。