簡體   English   中英

單擊JButton時打開一個新的JFrame

[英]Opening a new JFrame when JButton is clicked

我試圖打開DebtForm.javabtnAddNewDebt按鈕被按下,從IOUApplication.java 按下按鈕后, IOUApplication.java窗口應關閉, DebtForm.java窗口應打開。

按下btnAddNewDebt按鈕時,我已經設法打開DebtForm.java ,但無法關閉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);
}

為了關閉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);

我希望這有幫助。 感謝您的閱讀!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM