如何检查是否选择了所有文本字段和单选按钮?

How to check whether all the TextFileds and RadioButtons are selected?

当且仅当所有 TextFiled 已填充且 RadioButtons 已被选中时,else 部分才必须执行。否则应弹出警报。 但是,我的代码没有按预期工作。

输入所有值后,弹出警告消息,您可以在图像中看到。单击下图打开输出图像。请帮忙。

sample.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<Pane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="485.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <children>
        <Label layoutX="234.0" layoutY="14.0" prefHeight="27.0" prefWidth="133.0" text="Bismillah Fruits" textFill="#f20404">
            <font>
                <Font name="System Bold" size="18.0" />
            </font>
        </Label>
        <Label layoutX="56.0" layoutY="72.0" text="Name:">
            <font>
                <Font name="System Italic" size="14.0" />
            </font>
        </Label>
        <TextField fx:id="name" layoutX="109.0" layoutY="69.0" promptText="Please Enter Name" />
        <DatePicker fx:id="date" layoutX="411.0" layoutY="69.0" promptText="Select Date" />
        <ChoiceBox fx:id="choiceBox1" layoutX="40.0" layoutY="153.0" prefHeight="25.0" prefWidth="112.0" />
        <TextField fx:id="kgs1" layoutX="160.0" layoutY="153.0" promptText="value" />
        <TextField fx:id="mul1" layoutX="331.0" layoutY="153.0" prefHeight="25.0" prefWidth="36.0" promptText="box" />
        <ChoiceBox fx:id="choiceBox2" layoutX="38.0" layoutY="198.0" prefHeight="25.0" prefWidth="112.0" />
        <TextField fx:id="kgs2" layoutX="160.0" layoutY="198.0" promptText="value" />
        <TextField fx:id="mul2" layoutX="331.0" layoutY="198.0" prefHeight="25.0" prefWidth="36.0" promptText="box" />
        <ChoiceBox fx:id="choiceBox3" layoutX="40.0" layoutY="236.0" prefHeight="25.0" prefWidth="112.0" />
        <TextField fx:id="kgs3" layoutX="160.0" layoutY="236.0" promptText="value" />
        <TextField fx:id="mul3" layoutX="331.0" layoutY="236.0" prefHeight="25.0" prefWidth="36.0" promptText="box" />
        <Separator layoutX="411.0" layoutY="131.0" orientation="VERTICAL" prefHeight="300.0" prefWidth="4.0" />
        <Label layoutX="485.0" layoutY="122.0" text="Total" />
        <Label layoutX="155.0" layoutY="122.0" text="Details">
            <font>
                <Font name="System Italic" size="12.0" />
            </font>
        </Label>
        <Separator layoutX="26.0" layoutY="138.0" prefHeight="4.0" prefWidth="543.0" />
        <Label layoutX="315.0" layoutY="157.0" text="x" />
        <Label layoutX="315.0" layoutY="202.0" prefHeight="17.0" prefWidth="6.0" text="x" />
        <Label layoutX="315.0" layoutY="240.0" text="x" />
        <Label fx:id="value1" alignment="CENTER" contentDisplay="CENTER" layoutX="495.0" layoutY="157.0" text="0" />
        <Label fx:id="value2" alignment="CENTER" contentDisplay="CENTER" layoutX="495.0" layoutY="202.0" text="0" />
        <Label fx:id="value3" alignment="CENTER" contentDisplay="CENTER" layoutX="495.0" layoutY="240.0" text="0" />
        <Separator layoutX="421.0" layoutY="419.0" prefHeight="4.0" prefWidth="167.0" />
        <Label layoutX="43.0" layoutY="335.0" text="Advance:" />
        <Label layoutX="43.0" layoutY="368.0" text="Coolie:" />
        <Label layoutX="43.0" layoutY="403.0" text="Market:" />
        <Label layoutX="42.0" layoutY="303.0" text="Commission:" />
      <RadioButton fx:id="eight" layoutX="167.0" layoutY="303.0" mnemonicParsing="false" text="8%" />
      <Label layoutX="211.0" layoutY="303.0" text="or" />
      <RadioButton fx:id="ten" layoutX="228.0" layoutY="303.0" mnemonicParsing="false" text="10%" />
      <Label fx:id="value4" alignment="CENTER" contentDisplay="CENTER" layoutX="469.0" layoutY="303.0" text="Commission" />
        <TextField fx:id="advance" layoutX="160.0" layoutY="331.0" promptText="Advance" />
        <TextField fx:id="coolie" layoutX="160.0" layoutY="364.0" prefHeight="25.0" prefWidth="64.0" promptText="Coolie" />
      <Label layoutX="230.0" layoutY="368.0" text="x" />
      <RadioButton fx:id="three" layoutX="240.0" layoutY="368.0" mnemonicParsing="false" text="3" />
      <Label layoutX="270.0" layoutY="368.0" text="or" />
      <RadioButton fx:id="five" layoutX="285.0" layoutY="368.0" mnemonicParsing="false" text="5" />
      <Label fx:id="value5" alignment="CENTER" contentDisplay="CENTER" layoutX="480.0" layoutY="368.0" text="Coolie" />
        <TextField fx:id="market" layoutX="160.0" layoutY="399.0" promptText="Market charge" />
        <Label fx:id="total" alignment="CENTER" contentDisplay="CENTER" layoutX="485.0" layoutY="423.0" text="Total" />
      <Separator layoutX="425.0" layoutY="333.0" prefHeight="4.0" prefWidth="167.0" />
      <Separator layoutX="425.0" layoutY="280.0" prefHeight="4.0" prefWidth="167.0" />
        <Button fx:id="cal" layoutX="208.0" layoutY="440.0" mnemonicParsing="false" text="Calculate" />
    </children>
</Pane>

和controller.java

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;

import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    private int total43;
    private static final String ORANGE = "Orange";
    private static final String APPLES = "Apples";
    private static final String MANGO = "Mango";
    private static final String CORN = "Corn";

    @FXML
    public Pane pane;

    @FXML
    public TextField name;

    @FXML
    public DatePicker date;

    @FXML
    public TextField kgs1, kgs2, kgs3, mul1, mul2, mul3, advance, coolie, market;

    @FXML
    public Label value1, value2, value3, value4, value5, total;

    @FXML
    public Button cal;

    @FXML
    public ChoiceBox choiceBox1, choiceBox2, choiceBox3;

    @FXML
    public RadioButton three, five, eight, ten;

    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    private ToggleGroup toggleGroup, toggleGroup2;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        System.out.println("Inside initialize.");
        choiceBox1.getItems().add(ORANGE);
        choiceBox1.getItems().add(APPLES);
        choiceBox1.getItems().add(MANGO);
        choiceBox1.getItems().add(CORN);

        System.out.println("choiceBox2 items added.");
        choiceBox2.getItems().add(ORANGE);
        choiceBox2.getItems().add(APPLES);
        choiceBox2.getItems().add(MANGO);
        choiceBox2.getItems().add(CORN);

        System.out.println("choiceBox3 items added.");
        choiceBox3.getItems().add(ORANGE);
        choiceBox3.getItems().add(APPLES);
        choiceBox3.getItems().add(MANGO);
        choiceBox3.getItems().add(CORN);

        choiceBox1.setValue(ORANGE);
        choiceBox1.getSelectionModel().selectedItemProperty().addListener((observableValue, o, t1) -> {
            System.out.println("choice1 clicked");
        });

        choiceBox2.setValue(APPLES);
        choiceBox2.getSelectionModel().selectedItemProperty().addListener((observableValue, o, t1) -> {
            System.out.println("choice2 clicked");
        });

        choiceBox3.setValue(MANGO);
        choiceBox3.getSelectionModel().selectedItemProperty().addListener((observableValue, o, t1) -> {
            System.out.println("choice3 clicked");
        });

        //
        System.out.println("ToggleGroup 8 or 10 object creation begin.");
        toggleGroup = new ToggleGroup();
        System.out.println("ToggleGroup 8 or 10 object created.");
        eight.setToggleGroup(toggleGroup);
        ten.setToggleGroup(toggleGroup);
        //
        System.out.println("ToggleGroup2 3 or 5 object creation begin.");
        toggleGroup2 = new ToggleGroup();
        System.out.println("ToggleGroup2 3 or 5 object created.");
        three.setToggleGroup(toggleGroup2);
        five.setToggleGroup(toggleGroup2);

        cal.setOnAction(actionEvent -> {
            System.out.println("cal function called. ");
            cal();
            System.out.println("cal function ended. ");
        });
    }

    public boolean isTrue(){
        if (!(value1.getText().trim().isEmpty() && value2.getText().trim().isEmpty() &&
                value3.getText().trim().isEmpty() && mul1.getText().trim().isEmpty() &&
                mul2.getText().trim().isEmpty() && mul3.getText().trim().isEmpty() &&
                advance.getText().trim().isEmpty() && coolie.getText().trim().isEmpty() &&
                market.getText().trim().isEmpty() && name.getText().trim().isEmpty() &&
                date.getValue() != null && toggleGroup.getSelectedToggle().isSelected() &&
                toggleGroup2.getSelectedToggle().isSelected())) {

            System.out.println("inside if ");
            alert.setTitle("Information");
            System.out.println("alert.setTitle(Error)");
            alert.setHeaderText("Error Occurred");
            System.out.println("alert.setHeaderText(Error Occurred);");
            alert.setContentText("Please Enter All Values");
            System.out.println("alert.setContentText(Please Enter All Values)");
            alert.setAlertType(Alert.AlertType.INFORMATION);
            System.out.println("alert.setAlertType(Alert.AlertType.ERROR)");
            alert.show();
            System.out.println("alert.show()");

            return false;

        }

        return true;

    }

    private void cal() {

        float comm, comm2;

        if (isTrue()) {

            System.out.println("Else begins. ");
            value1.setText(String.valueOf(Integer.parseInt(kgs1.getText()) * Integer.parseInt(mul1.getText())));
            value2.setText(String.valueOf(Integer.parseInt(kgs2.getText()) * Integer.parseInt(mul2.getText())));
            value3.setText(String.valueOf(Integer.parseInt(kgs3.getText()) * Integer.parseInt(mul3.getText())));
            total43 = Integer.parseInt(value1.getText()) +
                    Integer.parseInt(value2.getText()) +
                    Integer.parseInt(value3.getText());
            //
            total.setText(String.valueOf(Integer.parseInt(value1.getText()) +
                    Integer.parseInt(value2.getText()) +
                    Integer.parseInt(value3.getText())));
            System.out.println("Total value " + total43);

            // -------------------------------------
            System.out.println("8 or 10 if begin.");
            if (eight.isSelected()) {
                comm = (total43 * 8) / 100;
                comm2 = total43 - comm;
                value4.setText(String.valueOf(comm2));
            } else {
                comm = (total43 * 10) / 100;
                comm2 = total43 - comm;
                value4.setText(String.valueOf(comm2));
            }
            System.out.println("8 or 10 if closed.");

            System.out.println("advance " + advance.getText());
            //
            System.out.println("3 or 5 if begin.");
            if (three.isSelected()) {
                value5.setText(String.valueOf(Integer.parseInt(coolie.getText()) * 3));
            } else
                value5.setText(String.valueOf(Integer.parseInt(coolie.getText()) * 5));
            System.out.println("3 or 5 if closed.");
            //
            System.out.println("market value.");
            System.out.println("market value: " + market.getText());
        }

    }
}

After entering all the values the alert message till popping up. 提前谢谢你。

我相信您在 isTrue() 函数中的条件不是您想要的。你可能想说这样的话:if(value1.getText().trim().isEmpty() || value2.getText().trim().isEmpty()...)

此外,条件 date.getValue() != null(为空时为假)与所有其他条件(为空时为真)的奇偶性相反。

在您的 if 语句中,我认为更好的解决方案可能是使用 TextField.length()==0 并去掉毯子 ! 以避免在阅读代码时造成混淆。

您正在进入if,如果不是所有的条件都是true。例如。如果 TextField 中至少有一个非空,则 () 中的表达式为 false,导致条件计算为 true。您需要删除否定并将 ands (&&) 替换为 ors ||.

不过,我建议减少这段代码的重复性。如果将这些元素添加到集合或数组中,您可以简单地迭代它们:

private List<TextField> textInputs;
private List<ToggleGroup> toggleGroups;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
    ...
    textInputs = Arrays.asList(value1, value2, value3, mul1, mul2, mul3, advance, coolie, market, name);

    // after initializing the ToggleGroups
    toggleGroups = Arrays.asList(toggleGroup, toggleGroup2);
}

...

public boolean isTrue() { // choose a better name for this
    boolean valid = (date.getValue() != null);

    if (valid) {
        for (TextField tf : textInputs) {
            if (tf.getText().trim().isEmpty()) {
                valid = false;
                break;
            }
        }
        if (valid) {
            for (ToggleGroup tg : toggleGroups) {
                if (tg.getSelectedToggle() == null) {
                    valid = false;
                    break;
                }
            }
        }
    }
    if (!valid) {
        // show alert here...
    }
    return valid;
}

这样您就可以避免一遍又一遍地重复检查,这样可以更轻松地一次更改所有 TextFields/ToggleGroups 的检查。请注意,如果 Toggle 的 none 处于选中状态,则 ToggleGroup 的选中切换为 null;在这种情况下,您的代码会产生 NPE。

我确实建议用 GridPane 替换你的 Pane 顺便说一句:GridPane 能够根据使用的字体(大小)处理不同的控件尺寸,这可能是不同的操作系统之间。 Pane 根本无法处理这个问题;它只是使用独立于其子项大小的绝对位置。