【发布时间】:2016-04-20 09:41:30
【问题描述】:
我目前正在使用 Eclipse 的拖放功能,我有一个默认带有 JFrame 的应用程序窗口,并且能够setVisible(false);,但我的其他框架/面板/窗口是我使用 JPanel 创建的并扩展了 JFrame。
由于extend 我无法setVisible(false or true); 它对窗口没有任何影响它仍然是真实的。
我的代码:
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LibrarianMenu frame = new LibrarianMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public LibrarianMenu() {
setTitle("Librarian");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 385, 230);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
.
.
. so on
在这里我正在尝试执行我的按钮:
btnLogout.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
LibrarianMenu frame = new LibrarianMenu();
frame.setVisible(false);
}
});
有什么解决办法吗?
【问题讨论】:
-
请发布一个可编译的示例,因为您的代码中甚至没有类定义。无论哪种方式,您都需要在您调用
setVisible(true)的同一引用上调用setVisible(false),而不是在引用新创建对象的引用上。 -
不要在 onClick 中创建 LibrarianMenu,将其创建为主类中的字段,然后使用 setVisible 为 false。单击按钮时,您正在创建另一个 LibrarianMenu,因此第一个仍然存在。
-
SwingUtilities.windowForComponent(btnLogout).setVisible(false)?
标签: java eclipse jframe jpanel