[英]How do I set JFrame's visibility to False after button click on IntelliJ?
UI2 是一個 JFrame,我使用 IntelliJ 的 Swing UI Designer --> GUI 表單建模。 我在此框架中添加了一個按鈕,單擊該按鈕將打開另一個框架。 我想在單擊按鈕之后和打開另一個框架之前將當前框架的可見性設置為 false。 而且我無法在按鈕的動作偵聽器方法中訪問當前幀。 有小費嗎? 抱歉,如果問題不清楚。
public class UI2 {
JPanel rootPanel;
JPanel northPanel;
JPanel westPanel;
JPanel southPanel;
JButton button1;
JLabel header;
JTextField textField1;
public UI2() {
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFrame frame2 = new JFrame();
frame2.setContentPane(new next_f().Panel1);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.pack();
frame2.setVisible(true);
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("UI");
frame.setContentPane(new UI2().rootPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}}
一種解決方案是在 class 中添加一個字段。
public class UI2 {
JPanel rootPanel;
JPanel northPanel;
JPanel westPanel;
JPanel southPanel;
JButton button1;
JLabel header;
JTextField textField1;
JFrame showingFrame;
...
}
然后在你的主要方法中。 將設置內容窗格更改為。
public static void main(String[] args) {
JFrame frame = new JFrame("UI");
UI2 ui2 = new UI2();
ui2.showingFrame = frame;
frame.setContentPane(ui2.rootPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
現在 UI2 中的所有內容都可以訪問showingFrame。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.