To close JFrame in the Swing application, you can use window events.
In this article, we will learn to close the JFrame programmatically on the click event and non-click event.
Let's first start with an example to create a JFrame that contains a label and a button.
This JFrame close when the user clicks on the close icon on the right upper corner of the window. For this purpose, we used the setDefaultCloseOperation() method and passed the JFrame.EXIT_ON_CLOSE constant.
This is a basic example where we created a JFrame that closes by clicking on the cross icon.
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JExercise {
public static void main(String[] args) {
JFrame jframe = new JFrame("JFrame");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("Hello World!");
JButton button = new JButton();
button.setText("Click");
panel.add(label);
panel.add(button);
jframe.add(panel);
jframe.setSize(300, 200);
jframe.setLocationRelativeTo(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
}
Output:
This is the example of the auto close of JFrame, for this, we used the dispachedEvent() method with the window close instruction. The JFrame window will auto-close just after the run. You can call this method with any event such as button click.
import java.awt.FlowLayout;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JExercise {
public static void main(String[] args) {
JFrame jframe = new JFrame("JFrame");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("Hello World!");
JButton button = new JButton();
button.setText("Click");
panel.add(label);
panel.add(button);
jframe.add(panel);
jframe.setSize(300, 200);
jframe.setLocationRelativeTo(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
jframe.dispatchEvent(new WindowEvent(jframe, WindowEvent.WINDOW_CLOSING));
}
}
This is another approach, here, we called the setVisible() and dispose() method on the button click.
First, we passed the false value to setVisible() method and then disposed the object by using the dispose() method. See the code here.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JExercise {
public static void main(String[] args) {
JFrame jframe = new JFrame("JFrame");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("Hello World!");
JButton button = new JButton();
button.setText("Click");
panel.add(label);
panel.add(button);
jframe.add(panel);
jframe.setSize(300, 200);
jframe.setLocationRelativeTo(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jframe.setVisible(false);
jframe.dispose();
}
});
}
}
Hope this will help you. :)
Happy Coding!
Also Read: How To Add Image To JPanel in Swing?