单击JButton时打开一个新的JFrame

时间:2016-03-23 10:37:52

标签: java swing jframe

我从DebtForm.java按下btnAddNewDebt按钮时尝试打开IOUApplication.java。按下按钮后,IOUApplication.java窗口应关闭,DebtForm.java窗口应打开。

我按下DebtForm.java按钮后设法打开btnAddNewDebt,但我无法关闭IOUApplication.java窗口。

我尝试过使用以下内容:

public void close(){
    WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOS­ING);
    Toolkit.getDefaultToolkit().getSystemEve­ntQueue().postEvent(winClosingEvent);
}

然而,我不确定在何处放置代码,或者是否有其他替代方法可以关闭窗口。

以下是IOUApplication.java的上下文片段:

public void initialize() {

    frame = new JFrame();
    frame.getContentPane().setBackground(Color.DARK_GRAY);
    frame.setBounds(100, 100, 450, 132);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnAddNewDebt = new JButton("Add new debt");
    btnAddNewDebt.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            DebtForm frame = new DebtForm();
            frame.setVisible(true);
        }
    });

    btnAddNewDebt.setBounds(81, 18, 117, 29);
    frame.getContentPane().add(btnAddNewDebt);      

    JButton btnPersonalDebt = new JButton("Personal Debt");
    btnPersonalDebt.setBounds(266, 18, 117, 29);
    frame.getContentPane().add(btnPersonalDebt);

    JLabel lblWrittenAndCoded = new JLabel("Written and coded by Samuel Kahessay");
    lblWrittenAndCoded.setForeground(Color.WHITE);
    lblWrittenAndCoded.setBounds(108, 88, 252, 16);
    frame.getContentPane().add(lblWrittenAndCoded);
}

1 个答案:

答案 0 :(得分:0)

为了关闭JFrame,您需要做的就是调用frame.dispose()。这个方法将完全摆脱内存中的JFrame。如果你想稍后重新打开窗口,你需要做的就是frame.setVisible(false),然后当你想重新打开它时,然后是frame.setVisible(true)。您可能必须将JFrame设置为全局变量才能执行此操作。假设你为DebtForm()编写的代码可以创建它自己的JFrame,这就是它的样子:

public void initialize() {

/* Your same code */
frame = new JFrame();
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setBounds(100, 100, 450, 132);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JButton btnAddNewDebt = new JButton("Add new debt");
btnAddNewDebt.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        DebtForm debtForm = new DebtForm();
        debtForm.setVisible(true);
        /* THE ONLY NEW LINE OF CODE */
        frame.setVisible(false); //This will make the first window disapear.
        /* ------------------------- */
    }
});

btnAddNewDebt.setBounds(81, 18, 117, 29);
frame.getContentPane().add(btnAddNewDebt);      

JButton btnPersonalDebt = new JButton("Personal Debt");
btnPersonalDebt.setBounds(266, 18, 117, 29);
frame.getContentPane().add(btnPersonalDebt);

JLabel lblWrittenAndCoded = new JLabel("Written and coded by Samuel Kahessay");
lblWrittenAndCoded.setForeground(Color.WHITE);
lblWrittenAndCoded.setBounds(108, 88, 252, 16);
frame.getContentPane().add(lblWrittenAndCoded);
}

稍后,为了使原始窗口重新出现,在initialize()方法之外,您需要声明“frame”,如下所示:

public static JFrame frame = new JFrame();

现在在你的DebtForm.java中,你可以再次使框架可见:

IOUApplication.frame.setVisible(true);

我希望这会有所帮助。感谢您的阅读!