简体   繁体   中英

How to make my program let the user enter text in JtextField before running the rest of the code? (Java 8)

Here is the code:

public void stage() {
    textarea1.setText("You are walking home from a long journey to your castle when you witness a huge explosion!");
    textarea1.setText(textarea1.getText() + "\n" + "What do you do?:\n(1)Run into the burning castle\n(2)Attend to the wounded\n(3)Look for the arsonist.");
    // need to force user to input text here
    if (Integer.parseInt(textfield.getText()) == 1) {
        textarea1.setText(textarea1.getText() + "\n" +"You attempt to run into the castle to save the inhabitants but the smoke and heat are too much. You exit the castle.");
        textarea1.setText(textarea1.getText() + "\n" + "What do you do?:\n(1)Attend to the wounded\n(1)Look for the arsonist.");
        if (Integer.parseInt(textfield.getText()) == 1) {
            stage2();
        }
        if (Integer.parseInt(textfield.getText()) == 2) {
            stage3();
        } else {
            textarea1.setText(textarea1.getText() + "\n" +"Please type the number before the choice you would like and press ENTER");
            stage1();
        }
    }
    if (Integer.parseInt(textfield.getText()) == 2) {
        stage2();
    }
    if (Integer.parseInt(textfield.getText()) == 3) {
        stage3();
    } else {
        textarea1.setText(textarea1.getText() + "\n" +"Please type the number before the choice you would like and press ENTER");
        stage1();
    }
}

Basically I'm writing a game, and at the start of this method the user has a few choices. The user has to enter his choice (1, 2, or 3) in a JTextField and the program goes to different methods based on the input using a series of if/else statements. However the program doesn't allow the user time to input a choice in the JTextField before running the if/else statements therefore it always goes to else and creates an infinite loop. If I was writing in C++ I know I could use a system(pause) and if I wasn't using Swing in Java I could use a scanner class. How can I force my program to allow time for the user to input an integer in the JTextField ? Thanks for the help, it's much appreciated.

You could just call the if-statements after 'ENTER' was pressed.

myComponent.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        //insert if-statements
    }
}); 
// do this with all your textfields

I hereby suggest another solution, providing you a little bit advanced functionality. Please also consider @LuxxMiner's answer.

This solution uses a combobox, you can adapt it for a textfield if you are 'forced' to

public class Main extends Application {

  @Override public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("LittleGame");
    primaryStage.setScene(new Scene(new AnchorPane() {
      {
        getChildren().add(new ComboBox<String>() {
          {
            getItems().addAll("Run", "Fight", "Cry!");
            setValue("Choose your style");
            valueProperty().addListener((observable, oldValue, newValue) -> {
              switch (newValue) {
              case "Run":
                System.out.println("You chose to run, fool!");
                //Goto stage run
                break;
              case "Fight":
                System.out.println("You chose to fight!");
                //Goto stage fight
                break;
              case "Cry!":
                System.out.println("You chose to cry!");
                //Goto stage cry
                break;
              default:
                System.out.println("*This fight style is not available!*");
              }
            });
          }
        });
      }
    }));
    primaryStage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

This code uses anonymous classes to be as short as possible, you should use a more structured approach when really using this :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM