如何修复';JavaFX对话框接受用户输入';问题
我正在创建一个JavaFx对话框,并且在很大程度上编写了代码。我的问题是,如果用户输入了无效的输入,如何显示错误消息。我知道我必须在某个地方使用while循环,但由于JavaFx对话框的结构,我不确定在哪里使用while循环。第二个问题是,如果用户输入了正确的输入,比如说1表示是,我想调用一个函数来执行任务 我编写的代码将弹出框,并将用户输入的结果打印到控制台如何修复';JavaFX对话框接受用户输入';问题,javafx,dialog,iteration,Javafx,Dialog,Iteration,我正在创建一个JavaFx对话框,并且在很大程度上编写了代码。我的问题是,如果用户输入了无效的输入,如何显示错误消息。我知道我必须在某个地方使用while循环,但由于JavaFx对话框的结构,我不确定在哪里使用while循环。第二个问题是,如果用户输入了正确的输入,比如说1表示是,我想调用一个函数来执行任务 我编写的代码将弹出框,并将用户输入的结果打印到控制台 public static void AnotherMatch() { //creates a popUp window St
public static void AnotherMatch() {
//creates a popUp window
Stage popUp = new Stage();
// makes sure no changes are made in the Main window while this window is open
popUp.initModality(Modality.APPLICATION_MODAL);
popUp.setTitle("New Game");
popUp.setMinWidth(400);
popUp.setHeight(200);
TextPanel textPanel2 = new TextPanel();
TextField nameInput = new TextField();
Button button = new Button("Enter");
//label explains how the game works
Label displayLabel = new Label();
displayLabel.setText("Do you want to play another match: Yes: 1 -- No: 2");
button.setOnAction(e -> isChoice(nameInput, nameInput.getText()));
//vbox stores label and is set in centre
VBox windowDisplay = new VBox();
windowDisplay.setStyle("-fx-background-color:Wheat"); //background colour is set
windowDisplay.getChildren().addAll(displayLabel,nameInput, button);
windowDisplay.setAlignment(Pos.CENTER);
Scene scene = new Scene(windowDisplay);
popUp.setScene(scene);
popUp.showAndWait(); }
isChoice函数的代码
private static boolean isChoice(TextField nameInput, String message) {
// TODO Auto-generated method stub
try {
int choice = Integer.parseInt(nameInput.getText());
if(choice == 1) {
System.out.println("I want to play game again");
return true;
}
else if (choice == 2){
System.out.println("I want to stop playing");
return false;
}
else {
System.out.println("Invalid entry");
return false;
}
}
catch(NumberFormatException e){
System.out.println(message + " Invalid .Enter 1 for yes and 2 for no");
return false;
}
}
应要求用户输入是或否。如果用户输入无效,应向用户显示一条错误消息,并再次询问答案,直到他们回答是或否。一种方法是使用
绑定禁用按钮
,除非文本字段
包含是
或否
(忽略案例)
演示应用程序使用
一种方法是使用绑定
禁用按钮
,除非文本字段
包含是
或否
(忽略大小写)
演示应用程序使用
Sedrick对此给出了一个很好的答案,展示了如何使用绑定和动作侦听器。现在我只是好奇,也许它更适合游戏的风格,或者只是你更喜欢它,但是对于这种情况,当它是一个是或否对话框时,仅仅有一个“是”和“否”按钮不是更简单?这意味着你不必检查根本不需要输入。相反,您可以只添加两个操作侦听器,每个按钮一个,然后执行您的操作。点击次数更少,而且不允许用户输入无效值也有好处。只是一些想法:)Sedrick给出了一个很好的答案,说明了如何使用绑定和操作侦听器。现在我只是好奇,也许它更适合游戏的风格,或者你更喜欢它,但是对于这种情况,当它是一个“是”或“否”对话框时,仅仅有一个“是”和“否”按钮不是更简单吗?这意味着您根本不必检查输入。相反,您可以只添加两个操作侦听器,每个按钮一个,然后执行您的操作。减少点击次数,并且不允许用户输入无效值。只是一些想法:)为迟来的回复道歉。我使用了一些代码行,效果很好。唯一的例外是,如果输入了无效的输入,并且我想将无效消息输出到对话框,我觉得这会迫使用户在前进之前输入正确的信息。因此,对我来说,它消除了对话框的需要。我将放弃在顶部关闭的选项并设置模式。@Robert也给出了很好的建议。我在代码中更改了这一行,因为“bind”似乎不是选项之一。我必须输入“是”。如果我输入YES或其他字母组合,它会将输入视为无效的“btn.disableProperty().bind(Bindings.notEqualIgnoreCase(“YES”,textField.textProperty())。和(Bindings.notEqualIgnoreCase(“no”,textField.textProperty());”对迟来的答复表示歉意。我使用了一些代码行,效果很好。唯一的例外是,如果输入了无效的输入,并且我想将无效消息输出到对话框,我觉得这会迫使用户在前进之前输入正确的信息。因此,对我来说,它消除了对话框的需要。我将放弃在顶部关闭的选项并设置模式。@Robert也给出了很好的建议。我在代码中更改了这一行,因为“bind”似乎不是选项之一。我必须输入“是”。如果我输入YES或其他字母组合,它会将输入视为无效的“btn.disableProperty().bind(Bindings.notEqualIgnoreCase(“YES”,textField.textProperty())。和(Bindings.notEqualIgnoreCase(“no”,textField.textProperty());”
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication357 extends Application
{
@Override
public void start(Stage primaryStage)
{
TextField textField = new TextField();
Button btn = new Button();
btn.disableProperty().bind(Bindings.notEqualIgnoreCase("yes", textField.textProperty()).and(Bindings.notEqualIgnoreCase("no", textField.textProperty())));
btn.setText("Say 'Hello World'");
btn.setOnAction((ActionEvent event) -> {
System.out.println("Hello World!");
});
StackPane root = new StackPane(new VBox(textField, btn));
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}