我是一個相當基礎的程序員,他被分配到一個GUI程序,沒有任何創建GUI的經驗。使用NetBeans,我設法設計了我覺得GUI應該看起來像什麼,以及按下按鈕時應該做些什麼,但是主程序在繼續之前不會等待用戶的輸入。我的問題是,我如何讓這個程序等待輸入?如何讓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的輸入,而無需預先對程序進行計費。我調查了其他StackOverflow問題,如this,this,this或this,但我無法理解如何將答案應用於我自己的代碼。如果可能的話,我想要一個類似於鏈接中答案的答案(我可以檢查和/或使用代碼的答案)。我很抱歉,如果我聽起來要求苛刻或沒有受過教育和困惑;我一直在努力工作這個項目幾個星期,現在是明天到期的,而且我一直在強調,因爲在我弄清楚之前我無法前進。
TL; DR - 如何讓我的主程序等待並收聽按鈕單擊事件?我應該使用模式對話框,還是有其他方法來做到這一點?無論哪種情況,需要更改哪些代碼才能實現?
你是什麼意思*「等待輸入?」* – MadProgrammer 2013-03-10 20:08:48
我希望程序停止並等待用戶按下來自GUI的特定按鈕前進(我想要等待的區域被指示與塊評論)。就像現在一樣,代碼在while循環中運行並拋出錯誤,因爲用戶沒有輸入他們需要的內容,我不知道如何修復它。 – 2013-03-10 20:25:59
從技術上講,你不能。您在事件驅動的環境中工作,這意味着您必須對任何可能發生的事件作出響應,你可以使用像模態對話框這樣的對話框,直到對話框關閉,這個對話框纔會被阻止,但這可能不是你真正想要的 – MadProgrammer 2013-03-10 20:32:30