Java Swing - .dispose() 方法未关闭 JFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22469574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Swing - .dispose() method not closing JFrame
提问by justinSYDE
I'm writing a simple game that is based on two JFrames. The user is only displayed on JFrame at a time. To get to the other JFrame, the user clicks a button. The button in the new JFrame will have an overriden method that should navigate the user back to the old JFrame.
我正在编写一个基于两个 JFrame 的简单游戏。用户一次只显示在 JFrame 上。要访问另一个 JFrame,用户单击一个按钮。新 JFrame 中的按钮将具有一个重写方法,该方法应该将用户导航回旧 JFrame。
I've found success doing this however the .dispose() method doesn't seem to close the new frame when the user tries to navigate back to the old frame.
我发现这样做很成功,但是当用户尝试导航回旧框架时, .dispose() 方法似乎没有关闭新框架。
Here's a snippet of my code (original JFrame class):
这是我的代码片段(原始 JFrame 类):
public class TicTacToe extends JFrame implements ActionListener{
....
public class gameModeListener implements ActionListener
{
@Override public void actionPerformed(ActionEvent e)
{
TicTacToeSingle singlePlayer = new TicTacToeSingle();
singlePlayer.setVisible(true);
dispose();
}
}
}
And from the other JFrame class:
从另一个 JFrame 类:
public class TicTacToeSingle extends TicTacToe{
private int i;
private int j;
JButton boardArray[][];
Random random_generator = new Random();
int randomI;
int randomJ;
public TicTacToeSingle()
{
super("Single Player");
gameMode.setText("Two Player"); //gameMode is the button that has it's actionlistener method overriden. It navigates the user to and back from JFrame to JFrame
gameMode.addActionListener(new gameModeListener());
....
}
....
public class gameModeListener implements ActionListener
{
@Override public void actionPerformed(ActionEvent e)
{
TicTacToe twoPlayer = new TicTacToe("Two Player");
twoPlayer.setVisible(true);
dispose();
}
}
Your help is greatly appreciated :)
非常感谢您的帮助:)
回答by Krishna
Well when u have used Object of the class and u are calling just Dispose();
then how compiler came to know that for this particular class object should be dispose on that call.?
so for giving reference u need to use this try this:
那么当你使用了类的 Object 并且你正在调用时,Dispose();
编译器是如何知道对于这个特定的类对象应该在那个调用上处理的。?所以为了提供参考,你需要使用这个试试这个:
TicTacToe twoPlayer = new TicTacToe("Two Player");
twoPlayer.setVisible(true);
twoPlayer.dispose();
if still there is prob let me know.
如果还有问题,请告诉我。