Java:警报窗口不会在“if”语句下打开

时间:2016-10-20 03:31:02

标签: java if-statement javafx alert

我是新来的,一般是编程世界所以请不要讨厌我。

我的班级刚开始使用“if”语句,由于某种原因,我无法显示我的javafx警告框。也欢迎任何使我的代码更短的提示。

public void start(Stage primarystage) {

    TextInputDialog getDemNums = new TextInputDialog();
    getDemNums.setTitle("How Many?");
    getDemNums.setHeaderText("Input number of people desired: ");
    Optional<String> sNumber = getDemNums.showAndWait();
    int result = Integer.valueOf(sNumber.get());

    if(result>=10){
        int bigGroup = result/2;
        TextInputDialog tenPlus = new TextInputDialog();
        tenPlus.setTitle("Group Assignment");
        tenPlus.setContentText("The group size is " + bigGroup + " people.");
    } else if (result>=3 && result<10) {
        int medGroup = result/3;
        Alert medWindow = new Alert(AlertType.INFORMATION);
        medWindow.setTitle("Group Assignment");
        medWindow.setContentText("The group size is " + medGroup + " people.");
    } else if(result<3) {
        Alert tooSmall = new Alert(AlertType.INFORMATION);
        tooSmall.setTitle("ERROR");
        tooSmall.setContentText("The number of people has to be at least three");

1 个答案:

答案 0 :(得分:2)

您永远不会对在ifelse块内创建的对话框执行任何操作。可能你打算给他们看......

此外!(result >= 10)相当于result < 10!(result >= 3)相当于result < 3所以您不必再次测试这些条件:

Dialog dialog;
if(result>=10){
    int bigGroup = result/2;
    dialog = new TextInputDialog();
    dialog.setTitle("Group Assignment");
    dialog.setContentText("The group size is " + bigGroup + " people.");
} else if (result>=3) {
    int medGroup = result/3;
    dialog = new Alert(AlertType.INFORMATION);
    dialog.setTitle("Group Assignment");
    dialog.setContentText("The group size is " + medGroup + " people.");
} else {
    dialog = new Alert(AlertType.INFORMATION);
    dialog.setTitle("ERROR");
    dialog.setContentText("The number of people has to be at least three");
}
dialog.show(); // or showAndWait()