2013-02-22 21 views
1

我已经编写了一些GUI编程,它有一个输入按钮,点击后可以打开我创建的工具,但我也希望jbutton执行的操作是关闭第一个GUI关闭以及打开我创建的工具,我试图更改setVisible(true/false);声明,但它们只是隐藏了GUI,它不运行。所以总结一下,我想我的JButton有两个功能,一个关闭当前的GUI,一个打开我创建的工具。如何关闭JButton上的图形用户界面点击

我认为这是与这个编码,使enterButton关闭GUI:

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

为什么使用2个GUI而不是'CardLayout'或类似的?有关更多详细信息,请参见[使用多个JFrames,好/坏实践?](http://stackoverflow.com/a/9554657/418556)。 – 2013-02-22 16:09:28

+0

我是一个初学者,我从来没有听说过卡片布局@AndrewThompson – 2013-02-22 16:21:56

+1

你现在已经有了,所以找到一个搜索引擎并且为它付出! – 2013-02-22 16:23:12

回答

2

您可以使用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(); 
    } 
} 
+1

第一个建议只有在第二个GUI在单独的'Process'中启动时才有效。如果第一帧设置了“DISPOSE_ON_CLOSE”,则第二个建议可能会起作用。 – 2013-02-22 16:11:47

+0

是的,第一个建议关闭了包括第二个GUI在内的整个事情 – 2013-02-22 16:21:00