如何检查是否已选择所有TextFileds和RadioButtons?

问题描述 投票:0回答:3

如果且仅当所有TextFileds都已填充且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.预先谢谢你。

java javafx javafx-8
3个回答
0
投票

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

此外,条件date.getValue() != null(为空时为false)与所有其他条件具有相反的奇偶校验(为空时为true)。


0
投票

[在您的if语句中,我相信一个更好的解决方案可能是使用TextField.length()==0并摆脱通用!,以避免在阅读代码时造成混淆。


0
投票

如果不是所有条件都为if,则您正在输入true。例如。如果TextField中的至少一个为非空,则() s内部的表达式为false,导致条件评估为true。您需要删除负数,然后将ors(&&)替换为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;
}

这样可以避免一遍又一遍地重复检查,这使得一次更改所有TextField / ToggleGroup的检查变得更加容易。请注意,如果ToggleGroup均未处于选定状态,则null的选定切换为Toggle。在这种情况下,您的代码将产生NPE。

[我确实建议您将Pane替换为GridPane,但:GridPane能够根据所使用的字体(大小)来处理不同的控件尺寸,这些字体在操作系统之间可能会有所不同。 Pane根本无法处理;它只是使用与子项大小无关的绝对位置。

© www.soinside.com 2019 - 2024. All rights reserved.