我有一个包含两个不同游戏的纸牌程序。为了访问游戏,我有一个菜单栏,其中的游戏菜单包含地毯纸牌和时钟纸牌。
默认情况下,程序在地毯纸牌中启动,我想选择游戏 -> 时钟纸牌,然后弹出一个带有时钟纸牌的新窗口,并关闭旧的地毯纸牌窗口。
目前我的程序将打开时钟窗口,但不会关闭旧的地毯窗口。
public void clockGame() {
JButton[] buttons = {
new JButton("Close"), new JButton("New Game")
};
final JDialog dialog = new JDialog(GUI, "Click a button");
final JOptionPane optionPane = new JOptionPane("Would you like to start a new game of Clock Solitaire?", JOptionPane.INFORMATION_MESSAGE, JOptionPane.INFORMATION_MESSAGE, null, buttons, buttons[1]);
buttons[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
});
buttons[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// What can I put here to close the Carpet Window
new ClockSolitaire();
dialog.setVisible(false);
}
});
dialog.setContentPane(optionPane);
dialog.pack();
dialog.setLocationRelativeTo(GUI);
dialog.setVisible(true);
}
我可以向第二个 actionPerformed
添加什么,以便关闭旧窗口?
最佳答案
让您的类扩展 JFrame,迫使您创建和显示 JFrame,而通常需要更大的灵 active ,您就会陷入困境。更好的解决方案:不要交换 JFrame。而是创建 JPanel 并显示它们,然后使用 CardLayout 在一个 JFrame 中交换它们。 。
关于java - 仅关闭一个 Jframe 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22974731/