Al ver que mucha gente pregunta cómo cerrar solo la ventana actual en java swing, escribí este artículo.
La interfaz principal tiene principalmente dos JButtons, uno es para invocar otro JFame a través de eventos de botón, y el otro es para cerrar el formulario actual.
1. El método setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) no se puede utilizar para cerrar el formulario actual, puede utilizar
setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
2. Exit () no se puede usar a través del evento JButton, que cerrará todos los formularios de todo el programa, puede usar dispose (); esto solo cerrará el formulario actual
La implementación específica es la siguiente:
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() {
// 普通按钮控件
JFrame jf = new JFrame("main");
Toolkit tk = this.getToolkit();// 得到窗口工具条
int width = 650;
int height = 500;
Dimension dm = tk.getScreenSize();
jf.setSize(300, 200);// 设置程序的大小
jf.setLocation((int) (dm.getWidth() - width) / 2,
(int) (dm.getHeight() - height) / 2);// 显示在屏幕中央
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setVisible(true);
JPanel contentPane = new JPanel();
jf.setContentPane(contentPane);
// 创建两个按钮,并且将按钮添加到内容面板中
JButton another = new JButton("另起页面");
JButton close = new JButton("关闭");
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();
}
}
Las representaciones son las siguientes:
Solo cierre el formulario de salida sin involucrar al formulario principal.
El código exit.java es el siguiente
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() {
// 普通按钮控件
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);
// 创建两个按钮并添加到内容面板
JButton close1 = new JButton("关闭");
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();
}
}
De esta manera, al hacer clic en el botón cerrar, solo se cierra el formulario de salida actual, mientras que el formulario principal aún existe.
Conocimiento complementario: la diferencia entre el cierre de formularios de JFrame y Frame en java
El método setDefaultCloseOperation () se usa en JFrame, por ejemplo:
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
En Frame, se utiliza un detector de eventos de la siguiente manera:
win.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});