我正在制作这个益智游戏,它使用 Scanner(System.in) 从键盘接收一封信,它会改变拼图状态。
现在,我正在为游戏制作一个 GUI。我有一个带有 JLabels 的 JPanel 显示拼图,另一个带有 2 个 JButton 的 JLabel,一个是已实现的退出按钮,另一个必须是重播按钮。这个重播按钮必须模拟整个游戏的重新开始。我的问题是如何在按下重放按钮后立即停止主循环?
当我单击按钮时,是否可以使用任何系统调用以在主循环上产生中断?
这是主要的逻辑循环:
public static void main(String args[]){
Cliente i=new Cliente();
int labSize;
labSize=i.func1();
boolean b1;
b1=i.func2();
boolean b2;
b2=i.func3();
int nDarts;
nDarts=i.func4();
int nDrags;
nDrags=i.func5();
Game g=new Game(labSize,nDarts,nDrags);
g.boolInit(b1, b2);
g.initGame();
i.setTab(g.getLab(), g.getLabSize());
do{
g.updateGame();
i.setTab(g.getLab(), g.getLabSize());
i.showTab();
g.moveHeroObj(i);
g.firingDartsLogic(i);
g.weaponCaught();
g.shieldCaught();
g.dartLogic();
g.flamesLogic();
if(!g.didGameEnd()){
g.dragonKillLogic();
g.dragonLogic();
}
g.updateHeroSymbol();
g.updateDragonSymbol();
if(g.finishedGame())
g.changeEndBool();
}while(!g.didGameEnd());
g.updateGame();
i.setTab(g.getLab(), g.getLabSize());
i.showTab();
i.exitFunc();
return;
}
这是图形用户界面:
public GameGUI(int n) {
labSize=n;
mainFrame = new JFrame("Maze Game");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
mainFrame.setVisible(false);
choicesPanel=new JPanel();
choicesPanel.setLayout(new GridLayout(0, 1));
labPanel=new JPanel(new GridLayout(labSize,labSize));
replayButton=new JButton("Replay");
replayButton.setAlignmentY(Component.TOP_ALIGNMENT);
replayButton.setAlignmentX(Component.LEFT_ALIGNMENT);
replayButton.addActionListener(new ReplayHandler());
exitButton=new JButton("Exit");
exitButton.setAlignmentY(Component.TOP_ALIGNMENT);
exitButton.setAlignmentX(Component.LEFT_ALIGNMENT);
exitButton.addActionListener(new ExitHandler());
choicesPanel.add(replayButton);
choicesPanel.add(exitButton);
mainFrame.getContentPane().add(choicesPanel);
mainFrame.getContentPane().add(labPanel);
}
private class ExitHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String[] holder={"Yes","Cancel"};
if(JOptionPane.showOptionDialog(null, "Are you sure you want to exit?", "", 0, 0, null, holder, 0)==0){
System.exit(0);
}
}
}
private class ReplayHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String[] holder={"Yes","No"};
if(JOptionPane.showOptionDialog(null, "Start a new game?", "", 0, 0, null, holder, 0)==0){
}
}
}