如何通过单击按钮打开一个新的Jframe并从新的Jframe关闭firstt Jframe?

时间:2017-10-17 08:38:07

标签: java swing jbutton action frame

单击1 JButton(“1- open 2nd Frame”)它打开第2帧,但是当我尝试通过JButton同时关闭第2帧和第1帧时(“2- close current Frame”和第一帧“),它只关闭第二帧而不是第一帧。 请建议如何同时关闭两个帧。不使用“System.exit(0);”

第一个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  
}
}

第二个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);
}
}

感谢您的回复。

2 个答案:

答案 0 :(得分:0)

让Frame1Frame导入Frame2Frame而不是 SELECT * FROM table WHERE MATCH(SearchCompany) AGAINST ('+"All Bar One"' IN BOOLEAN MODE);

这应该在

之前调用
frmFrame.dispose();

答案 1 :(得分:0)

如何在单击按钮时关闭JFrame并打开另一个JFrame。

进入行动听众后,请执行以下操作:

1)让另一帧消失: 如果你想永久处理它而不是在执行之后访问它:

        F1F.setVisible(false);
        dispose();
        new Frame2Frame().setVisible(true);

这应该永久处理第一帧并使第二帧可见。