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:
- Jtextfield cannot be displayed normally when added to JPanel
- java.lang.IllegalArgumentException: adding a window to a container
- Multipartfiletofileutils (multipartfile to file)
- Monitoring session to determine whether the user is online or not
- Solution to javax.naming.noinitialcontextexception error
- Extracting JDBC tool class: JDBC utils
- Java uses single thread pool to realize multi thread sequential execution (non alternating, non synchronous)
- Abstract method and static method of java interface
- Three ways of thread sequence alternate execution in Java lock free programming
- Ternary operator in Java?: error: not a statement
- The thread implementation of timer in Java
- JAVA: How to Convert PDF from Color to Grayscale
- Java Error: No enclosing instance of type Main is accessible. Must qualify the allocation with an encl
- JAVA: Random access file is always garbled
- [Solved] Java.lang.IllegalStateException: getReader() has already been called for this request
- Explicit and implicit conversion of Java data type
- [JVM] stack overflow error stackoverflowerror [How to Solve]
- Springboot controls the startup of rabbitmq through configuration files
- How to Solve JVM Common Errors: outofmemoryerror & stackoverflowerror
- Java callback function implementation case