I have tried to display a window from a class called testView from another class ABC. Window contains a button. I want to close the window on that button click. How can i close it?
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);
}
}
Window is displayed from another class ABC.How can i close the window on button click?
You can do something like this:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
frameToClose.dispose();
}
});
This code is adding a button action listener, then it tells the frame to close when that button has an action that is acted upon it. Hope this helps :)
To make window invisible you have to call
java.awt.Window.setVisible(false)
But with dispose
method you remove your window from memory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.