【问题标题】:How to close a GUI on JButton click [closed]如何关闭 JButton 上的 GUI 单击 [关闭]
【发布时间】:2013-02-22 16:02:16
【问题描述】:

我编写了一些具有 GUI 的代码,该 GUI 有一个输入按钮,单击该按钮时会打开我创建的工具,但我还希望 jbutton 做的是关闭和打开第一个 GUI我创建的工具,我尝试更改 setVisible(true/false); 语句,但它们只是隐藏了 GUI,它没有运行。

总而言之,我希望我的JButton 有两个功能,一个是关闭当前的 GUI,一个是打开我创建的工具。

我认为这与使enterButton关闭GUI的编码有关:

public void actionPerformed(ActionEvent e){ 
    if(e.getSource() == enterButton){
        // coding to make the GUI exit???
    }
}

【问题讨论】:

  • 为什么使用 2 个 GUI 而不是 CardLayout 或类似的?有关详细信息,请参阅The Use of Multiple JFrames, Good/Bad Practice?
  • 我是初学者,我从未听说过卡片布局@AndrewThompson
  • 你现在有,所以去搜索引擎吧!

标签: java swing jbutton exit actionevent


【解决方案1】:

你可以像这样使用System.exit(0)

public void actionPerformed(ActionEvent e){ 
    if(e.getSource() == enterButton){

        //coding to make the GUI exit???

        System.exit(0); 
    }
}

或使用dispose()

public void actionPerformed(ActionEvent e){ 
    if(e.getSource() == enterButton){
        //coding to make the GUI exit???

        this.dispose();
    }
}

【讨论】:

  • 第一个建议只有在第二个 GUI 以单独的 Process 启动时才有效。如果第一帧设置为DISPOSE_ON_CLOSE,则第二个建议可能有效。
  • 是的,第一个建议关闭了整个事情,包括第二个 GUI
猜你喜欢
  • 2013-09-26
  • 1970-01-01
  • 2013-06-27
  • 1970-01-01
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多