Obviously, a function continuously running within the actionPerformed method will effectively disable the rest of my GUI.
这是一个有效的观察,表明您在使用Swing时已经理解了基本规则.
你的游戏最有可能是事件驱动的(如果我错了,请纠正我)所以按钮执行的操作应该只是将程序设置为新状态,等待进一步的事件.这不应该是耗时的,并且通常由EDT直接完成.
当然,如果你想做一个奇特的开始 – 新游戏动画,那需要在一个单独的线程中执行,在这种情况下你只需要在actionPerformed方法中启动动画线程(我建议使用SwingWorker) ,然后返回.
在代码中,我想它看起来像这样:
go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Remove the menu components
someJPanel.removeAll();
// Show the game table
someJPanel.add(new GamePanel());
someJPanel.revalidate();
someJPanel.repaint();
// done. Wait for further user actions.
}
});