簡體   English   中英

關閉打開的框架而不關閉當前框架

[英]closing an opened frame without closing the current

我想在打開另一個 Frame 時關閉我的homeScreen Frame

public class MainClass {

    public static void main(String[] args) {
        JFrame homeScreen = new JFrame("HomeScreen");
        homeScreen.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        EsPanel2 panelloPrimo = new EsPanel2();
        homeScreen.add(panelloPrimo);
        homeScreen.setBounds(0, 0, 690, 470);
        homeScreen.setLocationRelativeTo(null);
        homeScreen.setVisible(true);
    }
}

Espanel2 是一個 class,它擴展了JPanel並有一個按鈕,單擊它時,按鈕的ActionListener會打開一個全新的 Frame。

單擊它時有一個按鈕,它通過按鈕的 ActionListener 打開一個全新的框架

您可以在ActionListener中引用帶有邏輯的開放框架。 就像是:

// close existing frame

JButton button = (JButton)event.getSource();
Window window = SwingUtilities.windowForComponent(button);
window.dispose();

// create and show new frame

JFrame frame = new JFrame(...);
...
frame.setVisible( true );

暫無
暫無

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

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