[英]How to open a new Jframe and close the firstt Jframe from the new Jframe by clicking button?
By click 1 JButton("1- open 2nd Frame") It Opens the 2nd-frame, but when I try to close the 2nd-frame and 1st-Frame at the same time by JButton("2- close current Frame and 1st Frame"), it just closes the 2nd-frame not the 1st one. 通过单击1 JButton(“ 1- open 2nd Frame”),它将打开第二帧,但是当我尝试通过JButton(“ 2-关闭当前帧和第一帧)同时关闭第二帧和第一帧时”),它只是关闭第二帧而不是第一帧。 Please advice how to close two Frames at the same time.
请建议如何同时关闭两个框架。 Not using "System.exit(0);"
不使用“ System.exit(0);”
The First JFrame code : 第一个JFrame代码:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionEvent;
public class Frame2Frame {
public JFrame frmFrame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame2Frame window = new Frame2Frame();
window.frmFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Frame2Frame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmFrame = new JFrame();
frmFrame.setTitle("1st Frame");
frmFrame.setBounds(100, 100, 333, 174);
frmFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmFrame.getContentPane().setLayout(null);
JButton btnToFrame = new JButton("1- open 2nd Frame");
btnToFrame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Frame1Frame F1F = new Frame1Frame<Object>();
F1F.setVisible(true);
new Frame2Frame().setVisible(false);
dispose();
}
});
btnToFrame.setBounds(83, 52, 147, 23);
frmFrame.getContentPane().add(btnToFrame);
}
protected static void addWindowListener(WindowAdapter windowAdapter) {
// TODO Auto-generated method stub
}
public static void setVisible(boolean b) {
// TODO Auto-generated method stub
}
public void dispose() {
// TODO Auto-generated method stub
}
}
The Second JFrame code : 第二个JFrame代码:
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Frame1Frame<frmFrame> extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
public JFrame frmFrame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1Frame frame = new Frame1Frame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Frame1Frame() {
setTitle("2nd Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 389, 145);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("2- close current Frame and 1st Frame");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
}
});
btnNewButton.setBounds(46, 35, 260, 23);
contentPane.add(btnNewButton);
}
}
Thanks for your reply in advance. 感谢您的提前答复。
Have Frame1Frame import Frame2Frame and than do frmFrame.dispose();
让Frame1Frame导入Frame2Frame,然后执行
frmFrame.dispose();
This should be called right before 这应该在之前
`dispose();`
How to close a JFrame and open another one when a button is clicked. 单击按钮时如何关闭JFrame并打开另一个JFrame。
Once you are inside your action listener do the following: 一旦进入动作侦听器,请执行以下操作:
1) Make the other frame disappear: if you want to permanently dispose of it and not access it after do: 1)使另一个框架消失:如果要永久丢弃它,并且在操作后不访问它:
F1F.setVisible(false);
dispose();
new Frame2Frame().setVisible(true);
This should permanently dispose of the first frame and make the second visible. 这应该永久丢弃第一个框架,并使第二个框架可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.