UI2是我使用IntelliJ的Swing UI Designer --> GUI建模的JFrame。我已经在这个框架中添加了一个按钮,当它被单击时,它将打开另一个框架。我希望在单击按钮之后打开另一个框架之前,将当前框架的可见性设置为false。并且我不能访问按钮的action listener方法中的当前帧。有什么建议吗?如果问题不清楚,很抱歉。
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);
}}发布于 2020-05-07 18:11:59
一种解决方案是只在类中添加一个字段。
public class UI2 {
JPanel rootPanel;
JPanel northPanel;
JPanel westPanel;
JPanel southPanel;
JButton button1;
JLabel header;
JTextField textField1;
JFrame showingFrame;
...
}然后在你的main方法中。将set content窗格更改为。
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。
https://stackoverflow.com/questions/61653904
复制相似问题