You see many people asking how to close only the current form in Java Swing, so write this.
The main interface consists of two JButtons, one to invoke another JFame with a button event, and the other to close the current form.
1. The method setDefaultCloseOperation(jframe. EXIT_ON_CLOSE) cannot be used to close the current form, but setDefaultCloseOperation(jframe. DISPOSE_ON_CLOSE) can be used;
2. Exit() cannot be used through the JButton event, which will cause the entire form of the program to close, dispose () can be used; This closes only the current formSpecific implementation is as follows:
NewFrame.java
The main interface consists of two JButtons, one to invoke another JFame with a button event, and the other to close the current form.
1. The method setDefaultCloseOperation(jframe. EXIT_ON_CLOSE) cannot be used to close the current form, but setDefaultCloseOperation(jframe. DISPOSE_ON_CLOSE) can be used;
2. Exit() cannot be used through the JButton event, which will cause the entire form of the program to close, dispose () can be used; This closes only the current formSpecific implementation is as follows:
NewFrame.java
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class NewFrame extends JFrame {
/**
* called another JFrame
* close this JFrame
* write by Jimmy.li
* time:2016/4/6 22:55
*/
private static final long serialVersionUID = 1L;
public NewFrame() {
// Normal button control
JFrame jf = new JFrame("main");
Toolkit tk = this.getToolkit();// Getting the Window Toolbar
int width = 650;
int height = 500;
Dimension dm = tk.getScreenSize();
jf.setSize(300, 200);// Set the size of the program
jf.setLocation((int) (dm.getWidth() - width)/2,
(int) (dm.getHeight() - height)/2);// Appears in the center of the screen
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setVisible(true);
JPanel contentPane = new JPanel();
jf.setContentPane(contentPane);
// Create two buttons and add them to the content panel.
JButton another = new JButton("Start Another Page");
JButton close = new JButton("close");
contentPane.add(another);
contentPane.add(close);
another.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new exit();
}
});
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
}
public static void main(String[] args)
{
new NewFrame();
}
}
The renderings are as follows:
Close only exit’s form, not the parent form.
The exit.java code is as follows
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* called another JFrame close this JFrame write by Jimmy.li time:2016/4/6 22:55
*/
public class exit {
private static final int WIDTH = 300;
private static final int HEIGHT = 200;
public exit() {
// Normal button control
final JFrame jf = new JFrame("exit");
jf.setSize(WIDTH, HEIGHT);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setVisible(true);
JPanel contentPane = new JPanel();
jf.setContentPane(contentPane);
// Create two buttons and add them to the content panel.
JButton close1 = new JButton("Closed");
contentPane.add(close1);
close1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// System.exit(0);
jf.dispose();
}
});
}
public static void main(String[] args)
{
new exit();
}
}
By clicking the close button, only the current exit form is closed and the parent form still exists.
Good luck!
Read More:
- How to get the current time in java time string
- Eclipse: How to Close XML validation (xml file Error)
- How to Converte Java objects to jsonnode in Jackson (Four Methods)
- How to Solve java server error (java application Run Normally)
- How to convert a Java string into a number (stringtonumber)
- JAVA 8: How to Convert List to Map
- JAVA: How to Convert PDF from Color to Grayscale
- How to Solve Error:java.io.InvalidClassException
- JAVA: How to Use Minio to upload pictures
- Java uses class array to report error Exception in thread “main” java.lang.NullPointerException solution
- Java: How to Find the Minimum Value from a Random Array by Bubble Sort Method
- AlmaLinux 9.1: How to Install java11
- How to Solve Flynk Task Java verifyerror 209 error
- How to Solve ERROR: Java 1.7 or later is required to run Apache Drill
- How to Solve Java Runtime (class file version 53.0)ERROR
- How to Solve IntelliJ IDEA Error: Cannot determine path to ‘tools.jar‘ library for 17 (C:\Program Files\Java\jd…
- How to Solve Java Error \ufeff
- JAVA: How to Read JSON Format Data (Web Game Development)
- JAVA: How to Use Multipartfile to upload Files
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException