私はテキストベースのアドベンチャーゲームを書いていますが、問題が発生しました。私はそれをターミナルからJFrameのGUIに移植しています。進める前にユーザーの入力を待つ必要があります。私は、falseとして始まるbuttonPressedと呼ばれるブール変数があるシステムをセットアップします。送信テキストボタンが押されると、テキストボタンがtrueに変わります。プログラムを続行する前に、ボタンがtrueになるのを待っているwhileループがあり、ユーザーが入力を送信するまでプログラムを本質的に一時停止します。問題は、これはwhileループ内にsystem.out.println行が書き込まれている場合にのみ機能します。それ以外の場合は、ボタンをクリックしても処理が継続しません。どうしたの?あなたが変更するには、あなたのブールのためbusy waitingあるので、あなたは、何かをするためにあなたのGUIスレッドの時間を与えていないように見えますJavaプログラムがwhileループ中に突き当たりました
public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
moves += 1;
movesLabel.setText("Moves: " + moves);
buttonPressed = true;
try{
input = (String)(textfield.getText());
textfield.setText("");
}catch(Exception ex){
textfield.setText("Error");
}
}
}
public static void main (String args[]) {
Main gui = new Main();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setSize(650, 350);
gui.setTitle("Sprinkles Text Adventure");
setInventory();
textArea.setText(textDisplay("Welcome Adventurer!"));
textArea.setText(textDisplay("What is thy name: "));
while(!buttonPressed){
// this only works when I have system.out.println("something") written in
}
buttonPressed = false;
textArea.setText(textDisplay(input));
if ("alice".equals(input)||"Alice".equals(input)){
textArea.setText(textDisplay("Stop toking the magical pipe, "));
textArea.setText(textDisplay("you aren't alice and this isn't wonderland"));
textArea.setText(textDisplay("Now down the rabbit hole you go!"));
}
else if ("l3ikezI".equals(input)||"l3ikezi".equals(input)){
System.out.println("Magic and Technology flows through your veins");
System.out.println("Welcome back, Master");
for(int j = 0; j<14; j++){
Chest[j]=1;
}
}
else{
System.out.println("Enter " + input + " and good luck!\n\n");
}
あなたは 'のbuttonPressed定義しているあなたのコンポーネントの再利用性を高めます'? – Nick
はい、上にプライベート静的なブール値でそれを初期化するbuttonPressed = false;それは私がコードの1行を持っているときに動作するので変です。 –
次に、JVMが空の無限ループをスキップすると思います。多分あなたはThread.sleep(15)を使うことができますか? – Demurgos