私は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;
}
}