我是一个相当基本的程序员,被指派制作一个 GUI 程序,而没有任何创建 GUI 的经验。使用 NetBeans,我设法设计了我认为 GUI 应该是什么样子,以及一些按钮在按下时应该做什么,但是主程序在继续之前不会等待用户输入。我的问题是,如何让这个程序等待输入?
public class UnoMain {
public static void main(String args[]) {
UnoGUI form = new UnoGUI(); // GUI class instance
// NetBeans allowed me to design some dialog boxes alongside the main JFrame, so
form.gameSetupDialog.setVisible(true); // This is how I'm trying to use a dialog box
/* Right around here is the first part of the problem.
* I don't know how to make the program wait for the dialog to complete.
* It should wait for a submission by a button named playerCountButton.
* After the dialog is complete it's supposed to hide too but it doesn't do that either. */
Uno Game = new Uno(form.Players); // Game instance is started
form.setVisible(true); // Main GUI made visible
boolean beingPlayed = true; // Variable dictating if player still wishes to play.
form.playerCountLabel.setText("Players: " + Game.Players.size()); // A GUI label reflects the number of players input by the user in the dialog box.
while (beingPlayed) {
if (!Game.getCompleted()) // While the game runs, two general functions are repeatedly called.
{
Player activePlayer = Game.Players.get(Game.getWhoseTurn());
// There are CPU players, which do their thing automatically...
Game.Turn(activePlayer);
// And human players which require input before continuing.
/* Second part of the problem:
* if activePlayer's strategy == manual/human
* wait for GUI input from either a button named
* playButton or a button named passButton */
Game.advanceTurn();
// GUI updating code //
}
}
}
}
我花了大约三天时间试图弄清楚如何集成我的代码和 GUI,所以如果有人能告诉我如何让这件事发挥作用,我将不胜感激。如果您需要任何其他信息来帮助我,请询问。
编辑:基本上,教授让我们用 GUI 制作 Uno 游戏。可以有计算机和人类玩家,其数量由用户在游戏开始时确定。我一开始基于控制台编写了整个游戏的代码,以使游戏的核心能够正常工作,并从那以后尝试设计一个 GUI;目前,此 GUI 仅在游戏运行时显示有关游戏的信息,但我不确定如何允许代码等待并接收来自 GUI 的输入,而无需程序提前充电。我调查了其他 StackOverflow 问题,例如this、this、this或this,但我无法理解如何将答案应用于我自己的代码。如果可能的话,我想要一个类似于链接中答案的答案(我可以检查和/或使用的代码答案)。如果我听起来要求很高或没有受过教育且令人困惑,我深表歉意;我已经在这个项目上努力工作了几个星期,现在明天就到期了,我一直在强调,因为在我弄清楚之前我无法前进。
TL;DR - 如何让我的主程序等待并监听按钮单击事件?我应该使用模式对话框,还是有其他方法可以做到这一点?无论哪种情况,都需要更改哪些代码才能做到这一点?