有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java文本输入if语句JavaFX

我正在为我的程序使用JavaFX Netbeans和Scene Builder。我正在尝试创建一个学生信息输入场景。我现在所有的输入都在工作,并进入一个文本文件。它还会显示一个成功弹出框。然而,我试图使它,如果其中一个输入是空白的,将不会输入任何内容,并会出现一个错误弹出窗口。现在,两个弹出窗口都能工作,但都坏了,只有成功出现。无论所有输入是否存在,错误框都会出现4次。我不明白为什么,因为我有每个if语句。如果一个字段没有填写,我也不知道从哪里开始不输入任何信息

import javafx.collections.*;
import javafx.event.ActionEvent;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.*;
import java.net.URL;
import java.nio.file.*;
import java.nio.file.StandardOpenOption;
import java.util.ResourceBundle;

public class CController implements Initializable {

    @FXML
    private Button button;

    @FXML
    private Text Student;

    @FXML
    private Button Add;

    @FXML
    private ComboBox Grade;

    @FXML
    private ComboBox Type;

    @FXML
    private TextField Name;

    @FXML
    private TextField ID;

    ObservableList<String> GRADES = FXCollections.observableArrayList("9", "10", "11", "12");
    ObservableList<String> EVENTS = FXCollections.observableArrayList("Business Law and Ethics", "Buying and Merchandising", "Financial Services", "Hospitality Services", "Marketing Communications", "Sports and Entertainment Marketing", "Travel and Tourism", "Accounting Applications", "Apparel and Accessories Marketing", "Automotive Services Marketing", "Business Finance", "Business Services Marketing", "Food Marketing", "Hotel and Lodging Management", "Human Resources Management", "Marketing Management", "Quick Serve Restaurant Management", "Restaurant and Food Service Management", "Retail Merchandising", "Sports and Entertainment Marketing");

    @FXML
    private void handleButtonAction(ActionEvent event) throws IOException {
        Parent FXMLDocument2Parent = FXMLLoader.load(getClass().getResource("B.fxml"));
        Scene FXMLDocument2Scene = new Scene(FXMLDocument2Parent);
        Stage AppStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        AppStage.setScene(FXMLDocument2Scene);
        AppStage.show();
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Grade.setItems(GRADES);
        Type.setItems(EVENTS);
    }

    @FXML
    public void addtolist(ActionEvent e) throws IOException {
        Alert success = new Alert(Alert.AlertType.INFORMATION);
        success.setTitle("Information");
        success.setHeaderText(null);
        success.setContentText("Student Added");
        success.showAndWait();

        Alert nosave = new Alert(Alert.AlertType.INFORMATION);
        String idinput = ID.getText();
        SendToText("\r\n");
        SendToText(idinput);
        if (idinput.isEmpty())
            nosave.setTitle("Error");
        nosave.setHeaderText(null);
        nosave.setContentText("Please Enter All Student Information");
        nosave.showAndWait();

        String nameinput = Name.getText();
        SendToText(",");
        SendToText(nameinput);
        if (nameinput.isEmpty())
            nosave.setTitle("Error");
        nosave.setHeaderText(null);
        nosave.setContentText("Please Enter All Student Information");
        nosave.showAndWait();

        String grade;
        grade = (String) Grade.getValue();
        SendToText(",");
        SendToText(grade);
        if (grade.isEmpty())
            nosave.setTitle("Error");
        nosave.setHeaderText(null);
        nosave.setContentText("Please Enter All Student Information");
        nosave.showAndWait();

        String event;
        event = (String) Type.getValue();
        SendToText(",");
        SendToText(event);
        if (event.isEmpty())
            nosave.setTitle("Error");
        nosave.setHeaderText(null);
        nosave.setContentText("Please Enter All Student Information");
        nosave.showAndWait();
    }


    private void SendToText(String cartone) throws IOException {
        Path file = Paths.get("C:/Users/Shaheer.K/Documents/Shaheer/IAiffy/src/ia/Extra/list.txt");
        byte[] data = cartone.getBytes();
        OutputStream output = null;
        try {
            output = new
                    BufferedOutputStream(Files.newOutputStream(file, StandardOpenOption.APPEND));
            output.write(data);
            output.flush();
            output.close();
        } catch (IOException e) {
            System.out.println("Message: " + e);
        }
    }
} 

共 (0) 个答案