1

在编程方面,我是个菜鸟。我们有这个关于登录配置文件帐户的项目。我刚开始做;我遇到了一个具体的问题。我想使用按钮关闭框架。

buttonenter.setText("Enter");
     buttonenter.addActionListener(new ActionListener (){
       public void actionPerformed (ActionEvent ae){

           }
    });

我尝试放置我的 frame.dispose();、set.Visible(false) 等,但我得到了一个错误。我不太明白。我真的很感激帮助!谢谢!

4

3 回答 3

4

这是您尝试执行的操作的简单示例。你收到什么错误?

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton btnClose = new JButton("CLOSE");
    btnClose.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.setVisible(false);
        }
    });
    frame.getContentPane().add(btnClose, BorderLayout.NORTH);
}
于 2012-10-05T16:32:37.243 回答
3

JButtons ActionListener你可以调用

  • JFrame#dispose();(终止当前 JVM)

  • JFrame#setVisible(false);(隐藏 JFrame)

  • System.exit(0);(终止当前 JVM)

和/或与(另一种标准方式是)

于 2012-10-05T16:35:47.583 回答
0

当您通过假可见关闭任何对象时,实际上您只是隐藏了该对象,而该对象仍保留在内存中。

你最好使用 frame.dispatchEvent(new windowEvent(frame,windowEvent.window_closeing)); 方法

于 2017-11-03T14:39:14.687 回答