java - 单击 IntelliJ 按钮后,如何将 JFrame 的可见性设置为 False?
问题描述
UI2 是我使用 IntelliJ 的 Swing UI Designer --> GUI 表单建模的 JFrame。我在此框架中添加了一个按钮,单击该按钮将打开另一个框架。我想在单击按钮之后和打开另一个框架之前将当前框架的可见性设置为 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);
}}
解决方案
一种解决方案是向您的班级添加一个字段。
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。
推荐阅读
- php - 获取刚刚添加的 term_id (WordPress)
- python - 网页抓取时找不到带有 JSON 信息的请求 URL
- sharepoint - SPFX - 扩展 - 无法将库文档转换为 pdf
- python-3.x - 如何在机器人框架报告中附加屏幕截图
- c - Redis 服务器及其 fork() - 何时调用 vm_enough_memory?
- bison - 解决 Yacc/Bison 语法中的 shift/reduce 冲突
- android - 如何触发框架布局的onsizechanged
- xamarin - 在矩形 xaml 中显示标题、文本内容
- aws-lambda - DyanamoDB 与 AWS Lambda 函数 Order by Desc Order with scan
- node.js - 连接到 Mongodb 的可变路由/url 参数