2016-10-08 9 views
0

私はJavaFxを初めて使用しており、私のハングマンゲームのコードを作成しようとしています。私はレキシコンを保持しているファイルがあるかどうかを確認する必要がある時点です。ユーザーにその場所の入力を要求しない場合。私は、これを行うには、ユーザーに場所を入力してボタンを押すよう求めるポップアップウィンドウでこれをしようとしています。このボタンは、textbox.getText()が保存され、読み込まれる原因となります。 。私は意図的にこのポップアップを実行するために場所を間違って指定しましたが、それはしばらくの間ループしてしまい、アプリケーションを終了させて​​しまいました。私は新しいシーンとしてそれを実行していますが、次に何をするかについています。ここに私のController.javaがありますJavaFxはwhileループでスタックしました

FYI:これは大学の宿題ですが、私は登録されていません。私は障害があり、私の友人はクラスを通過しています。私は彼の仕事で遊ぶことで忙しくしています。コードを覚えるのが大好きで、これは私を最新のものにしてくれるので、新しいことを学ぶことができます。

package sample; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.VBox; 
import javafx.stage.Modality; 
import javafx.stage.Stage; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 


public class Controller { 

    public Label welcomeLabel, guessesLettersLabel, GuessesRemainingLabel, currentStateLAbel, endGameMessageLabel; 
    public Button newGameButton, exitButton, enterTextButton; 
    public TextField enterTextBox; 
    public boolean ynAnswer; 
    public String textAnswer; 
    Stage window; 
    File inputFile; 


    public void newGame() throws FileNotFoundException { 

     Scanner input = new Scanner(System.in); 

     inputFile = new File("src\\sample\\HangmanLexcon.txt"); 
     while(!inputFile.exists()) { 
      window = new Stage(); 
      window.initModality(Modality.APPLICATION_MODAL); 
      window.setTitle("File was not found."); 
      window.setMinWidth(250); 
      window.setMaxHeight(100); 

      //form 
      TextField fileInput = new TextField(); 
      Button inputButton = new Button("Upload File"); 
      inputButton.setOnAction(e -> { 
       inputFile = new File(fileInput.getText()); 
       window.close(); 
      }); 


      //Layout 
      VBox layout = new VBox(10); 
      layout.setPadding(new Insets(20, 20, 20, 20)); 
      layout.getChildren().addAll(fileInput, inputButton); 

      //Set scene and show in window. 
      Scene scene = new Scene(layout, 300, 100); 
      window.setScene(scene); 
      window.show(); 

     } 

     Hangman newGame = new Hangman(inputFile.toString()); 
     welcomeLabel.setText("A word has been selected. Good luck, and may the odds ever be in your favor."); 

     guessesLettersLabel.setText("Guessed Letters: " + newGame.getGuessedLetters()); 
     GuessesRemainingLabel.setText("Guesses remaining: " + newGame.getIncorrectGuessesRemaining()); 
     currentStateLAbel.setText("Current State: " + newGame.getCurrentState()); 

    } 

    public void enterTextButton(){ 
     System.out.println("You pressed enter..."); 
    } 

    public void enterText(){ 
     enterTextButton(); 
    } 

    /** 
    * If the player closed to exit the game or hit the X button this method will ask if they are 
    * sure they wish to exit before quiting the game. 
    */ 
    public void exitGame(){ 
     Boolean answer = desplayYesNoMessgae("Exit", "Are you sure you want to exit?"); 
     if(answer) 
      System.exit(0); 
    } 

    public boolean desplayYesNoMessgae(String title, String message){ 
     Stage window = new Stage(); 
     window.initModality(Modality.APPLICATION_MODAL); 
     window.setTitle(title); 
     window.setMinWidth(250); 
     Label label1 = new Label(); 
     label1.setText(message); 

     //Create two buttons, yes and no 
     Button yesButton = new Button("Yes"); 
     Button noButton = new Button("No"); 

     //If yes ic clucked, set answer to true. 
     yesButton.setOnAction(e -> { 
      ynAnswer = true; 
      window.close(); 
     }); 

     //if no is clicked, set answer to false. 
     noButton.setOnAction(e -> { 
      ynAnswer = false; 
      window.close(); 
     }); 

     VBox layout = new VBox(10); 
     layout.getChildren().addAll(label1, yesButton, noButton); 
     layout.setAlignment(Pos.CENTER); 
     Scene scene = new Scene(layout); 
     window.setScene(scene); 
     window.showAndWait(); 

     return ynAnswer; 
    } 

    public String desplayTextMessgae(String title, String message){ 
     Stage window = new Stage(); 
     window.initModality(Modality.APPLICATION_MODAL); 
     window.setTitle(title); 
     window.setMinWidth(250); 
     Label label1 = new Label(); 
     label1.setText(message); 

     //form 
     TextField nameInput = new TextField(); 
     Button loadBbutton = new Button("Upload"); 
     loadBbutton.setOnAction(e -> { 
      textAnswer = nameInput.getText(); 
      window.close(); 
     }); 


     //Layout 
     VBox layout = new VBox(10); 
     layout.setPadding(new Insets(20, 20, 20, 20)); 
     layout.getChildren().addAll(nameInput, loadBbutton); 

     //Set scene and show in window. 
     Scene scene = new Scene(layout, 300, 100); 
     window.setScene(scene); 
     window.show(); 

     return textAnswer; 
    } 

} 

答えて

2

GUIプログラミングでは、ループベースのアプローチを検討する前に、常にイベント駆動アプローチを検討してください。 whileループは、コントロールを繰り返し作成し、リスナーの1つ(ボタン)にリスナーを登録し、コントロールを新しいウィンドウに配置し、ウィンドウを表示します。ループは、次の反復に進む前にボタンが押されるのを待つことはありません。だからあなたは多くのウィンドウで終わる...

有効なファイルが入力されるまでステージが表示されたらブロックするようにします。ステージでは、ウィンドウを表示してウィンドウが閉じられるまで実行をブロックする方法を定義します(showAndWait)。 (これは本質的にのFXアプリケーションスレッドで呼び出す必要があるブロッキング方法です。コード実行がブロックされている間UIが応答し続けるようにするためにいくつかの魔法があります)。あなたは可能性があり

inputButton.setOnAction(e -> { 
    inputFile = new File(fileInput.getText()); 
    if (inputFile.exists()) { 
     window.close(); 
    } 
}); 

と:ファイルが存在する場合にのみ、ウィンドウを閉じることで、これは少しよりよい作ることができる

while(!inputFile.exists()) { 
    window = new Stage(); 
    window.initModality(Modality.APPLICATION_MODAL); 
    window.setTitle("File was not found."); 
    window.setMinWidth(250); 
    window.setMaxHeight(100); 

    //form 
    TextField fileInput = new TextField(); 
    Button inputButton = new Button("Upload File"); 
    inputButton.setOnAction(e -> { 
     inputFile = new File(fileInput.getText()); 
     window.close(); 
    }); 


    //Layout 
    VBox layout = new VBox(10); 
    layout.setPadding(new Insets(20, 20, 20, 20)); 
    layout.getChildren().addAll(fileInput, inputButton); 

    //Set scene and show in window. 
    Scene scene = new Scene(layout, 300, 100); 
    window.setScene(scene); 
    window.showAndWait(); 

} 

を:これは、ウィンドウが閉じられるまで次の反復に進んでループを防止しますファイルが存在しない限り、ボタンを無効にすることでより良いものにしてください:

TextField fileInput = new TextField(); 
Button inputButton = new Button("Upload File"); 
inputButton.setDisable(true); 
fileInput.textProperty().addListener((obs, oldText, newText) -> 
    inputButton.setDisable(! new File(newText).exists())); 
inputButton.setOnAction(e -> { 
    inputFile = new File(fileInput.getText()); 
    window.close(); 
}); 

または結合を持つ:

TextField fileInput = new TextField(); 
Button inputButton = new Button("Upload File"); 
inputButton.disableProperty().bind(Bindings.createBooleanBinding(
    () -> ! new File(fileInput.getText()).exists(), 
    fileInput.textProperty())); 
inputButton.setOnAction(e -> { 
    inputFile = new File(fileInput.getText()); 
    window.close(); 
}); 

最後に、ファイルが有効でない限り、無効にOKボタンを押して、あなたは窓が装飾のない作ることができます(いや、決定的に、閉じるボタンを最小化、最大化、または)。有効なファイルが入力されない限り、ウィンドウを閉じることは不可能であり、ループを完全に取り除くことができます。

if (! inputFile.exists()) { 
    window = new Stage(); 
    window.initModality(Modality.APPLICATION_MODAL); 
    window.initStyle(StageStyle.UNDECORATED); 
    window.setTitle("File was not found."); 
    window.setMinWidth(250); 
    window.setMaxHeight(100); 

    //form 
    TextField fileInput = new TextField(); 
    Button inputButton = new Button("Upload File"); 
    inputButton.disableProperty().bind(Bindings.createBooleanBinding(
     () -> ! new File(fileInput.getText()).exists(), 
     fileInput.textProperty())); 
    inputButton.setOnAction(e -> { 
     inputFile = new File(fileInput.getText()); 
     window.close(); 
    }); 


    //Layout 
    VBox layout = new VBox(10); 
    layout.setPadding(new Insets(20, 20, 20, 20)); 
    layout.getChildren().addAll(fileInput, inputButton); 

    //Set scene and show in window. 
    Scene scene = new Scene(layout, 300, 100); 
    window.setScene(scene); 
    window.showAndWait(); 
} 

そして、もう一つの最後のコメント::彼らは有効なファイルを見つけ出すことができない場合(何も逃げ道はありません)これは、ユーザーに迷惑がある場合があります、ユーザーがファイルを選択できるようにするには、あなたが考えるかもしれませんFileChoosertutorial)を使用します。

関連する問題