这是我开发的代码。这是主程序,它保存和执行我的游戏的每个外部JFrame。chooseGender是一个外部程序,它只是一个JFrame及其组件。
我的目标是当chooseGender执行时,它有两个选项按钮(男性,女性)当用户选择一个时,actionListener会将框架设置为setVisible(false),然后让WindowClosing事件打开下一个JFrame (chooseRace)。这将发生在更多的帧中,但这2个是为了学习目的。我很感谢你提前提供的帮助。:)
所以我的问题是,我该如何在这个程序中向chooseGender添加一个WindowListener,这样我就可以关闭它并打开下一个呢?
package javagame;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class Main implements WindowListener {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new chooseGender().setVisible(true);
}
});
EventQueue.invokeLater(new Runnable() {
public void run() {
new chooseRace().setVisible(false);
}
});
}发布于 2010-06-22 01:06:42
实现这一点的一种简单方法可能是使用模态JDialogs。
代码将类似于以下代码:
main {
new chooseGender().setVisible(true);
new chooseRace().setVisible(true);
new chooseAge...
}您可能希望实现一个类似于以下内容的WidowListener:
public class OpenNewWindowWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e){
// in here open the next window.
}
}并将该窗口侦听器添加到正确的帧中:
// In the constructor for the JFrame
addWindowListener(new OpenNewWindowListener());而且,这些类中的每一个都会使用extend JDialog,并且在它们的构造函数中使用setModal(true)。
https://stackoverflow.com/questions/3086723
复制相似问题