【问题标题】:Closing JFrame with Button用按钮关闭 JFrame
【发布时间】: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


【解决方案1】:

因为您是在 Runnable 中创建框架,所以它的范围仅限于可运行对象的范围。尝试在 runnable 之外声明变量,然后在 runnable 中初始化它,如下所示:

private JPanel contentPane;
private LibrarianMenu frame;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame = new LibrarianMenu();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

然后在不声明 LibrarianMenu 的新实例的情况下将可见设置为 false:

btnLogout.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
           frame.setVisible(false);
     }
});

【讨论】:

  • @nix 如果这解决了您的问题,请将答案标记为正确。谢谢:)
【解决方案2】:

发生这种情况是因为每次按下按钮时都会创建该框架的新实例。这是您的代码更新:

static LibrarianMenu frame ;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame = new LibrarianMenu();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

注销按钮事件应该是这样的:

btnLogout.addActionListener(
        new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
            }
        });

【讨论】:

  • @nix static 是那些“看起来”不错的解决方案之一,但它会让你成为一个黑暗的盟友,打败你,偷走你的钱包,一切都是为了,最好避免
猜你喜欢
  • 1970-01-01
  • 2011-01-22
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 2022-01-18
  • 2011-02-05
  • 1970-01-01
相关资源
最近更新 更多