繁体   English   中英

这是退出应用程序的正确方法吗?

[英]Is this the proper way of exiting from the application?

我有一个运行正常的JavaFX应用程序。 我已经使用Scene Builder创建了应用程序的GUI,并且设法钩住了控制器。 因此,应用程序从数据库加载数据,然后将其显示在TableView

因此,应用程序的设置如下:

控制器: StudenOverview.java

    @Override
public void initialize(URL location, ResourceBundle resources) {

    //SOMETHING IS MISSING HERE, WHAT IS IT?
    regNumber.setCellValueFactory(new PropertyValueFactory<Student, String>("regNumber"));
    name.setCellValueFactory(new PropertyValueFactory<Student, String>("firstName"));
    surname.setCellValueFactory(new PropertyValueFactory<Student, String>("lastName"));
     buildData();
}

public void buildData() {
    BDConnect connection = new BDConnect();
    students = FXCollections.observableArrayList();
    try {
        String loadDataSQL = "SELECT * FROM _students_ ORDER BY _id";
        ResultSet res = connection.connect().createStatement().executeQuery(loadDataSQL);
        while (res.next()) {
            Student st = new Student();
            st.setRegNumber(res.getString(1));
            st.setFirstName(res.getString(2));
            st.setLastName(res.getString(3));
            students.add(st);
        }
        table.setItems(students);
    } catch (SQLException | ClassNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "Could not load data, the system will exit!");
        //Of course i will print the error for debugging
        System.exit(-1);
        System.out.println("Could not laod data in the Table!");
    }
}

我的MainApp.java类:

public class MainApp extends Application {

private Stage primaryStage;

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("Student App");
    initRootLayout();

}

public static void main(String[] args) {
    launch(args);
}

/**
 * Initializes the root layout.
 */
public void initRootLayout() {
    try {
        // Load root layout from fxml file.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/StudentOverview.fxml"));
        // Show the scene containing the layout.
        Scene scene = new Scene(loader.load());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

一切正常,但我担心的是当buildData方法无法加载数据时,例如由于SQL语法,应用程序将在GUI中显示一个空表。 这很bad因为如果没有表中的数据,用户将无法使用该应用程序。 因此,就我而言,我已使用System.exit()从应用程序退出。

所以,我的问题是:

这是正确的做法吗? 您的程序员有什么建议? 如果您在哪里,该怎么办?

System.exit()是强制出口,您可以使用以下命令:

Platform.exit();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM