我的問題是如何爲每個程序顯示多個用戶界面屏幕。我確信這個問題之前已經被問過了,但我還沒有找到適合我的解決方案(或者我應該說我明白了)。關於我正在談論的場景,沒有任何異乎尋常的東西。首先是驗證來自屏幕的輸入並在出現錯誤的情況下重新顯示相同的屏幕。如何在同一個程序中顯示兩個javafx GUI屏幕
我會根據第二個更復雜的場景提出問題:顯示輸入數據屏幕,處理輸入;然後顯示輸出。這很複雜,因爲第一個,帶有5個文本框和命令按鈕的簡單屏幕使用FXML文件,而第二個,多選列表框不使用。流量是:
1.主程序調用
2.它加載FXML並以某種方式或另一種呼叫
3.接收到該輸入,並對其進行處理,以產生輸出的控制裝置的加載器程序。
最後一步是以多選列表框的形式顯示輸出。請注意,第一個GUI使用一個單獨的文件控制器來處理輸入,而第二個GUI使用與屏幕定義相同文件的事件處理程序在用戶單擊時進行選擇一個命令按鈕。
各種SO帖子紛紛表示,要走的路是一旦第一GUI完成不關閉應用程序通過,但保持了JavaFX運行時在後臺與
Platform.setImplicitExit(假)去;
並定義每個GUI並簡單地將場景切換到您想要顯示的場景。但是,鑑於我描述的情況,你把代碼放在哪裏?第二個GUI有三部分:屏幕定義,事件處理程序和場景切換代碼。你把每個放在哪裏? #2或#3。如果你在#2中放一些,在#3中放一些,#3怎麼知道你在#2中做了什麼?
守則#2 FMXL裝載機:
public class inputData extends Application {
public static void load() {
launch();
}
@Override
public void start(Stage stage) throws Exception {
GridPane inpRoot = FXMLLoader.load(getClass().getResource("inputData.fxml"));
Scene inpScene = new Scene(inpRoot, 300, 275);
stage.setTitle("Amsnag 2.1 - Query Input");
stage.setScene(inpScene);
stage.show();
}
}
守則#3,列表框的定義和處理程序,工作正常運行分開。只有當我試圖將其與程序的其餘部分結合時,它纔會失敗。
public class multiList extends Application {
public static void load() {
launch();
}
public static final ObservableList options = FXCollections.observableArrayList();
@Override
public void start(final Stage stage) {
final ListView<String> listView = new ListView<>();
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
// load list from DB
Connection conn = sql.connect();
try {
// initialize option table
ResultSet rs = sql.select(conn,
"select distinct connDesc,accom from option order by connDEsc,accom");
while (rs.next()) {
String opt = rs.getString("connDesc") + ": " + rs.getString("accom");
listView.getItems().add(opt);
}
conn.close();
}
catch (SQLException e) {
System.out.println(e.getMessage()+ " from init");
}
// button to display fares
final Button displayButton = new Button("Display Fares");
// handle button click
displayButton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
Platform.exit(); // close list box
ObservableList selectedIndices = listView.getSelectionModel().getSelectedItems();
// lcreate temp table with selected options
Connection conn = sql.connect();
try {
// initialize option table
ResultSet rs = sql.select(conn,
"create temporary table selected (connDesc varchar(200),accom varchar(50))");
for(Object o : selectedIndices){
String option = o.toString();
// extract connDesc+accom from displayed option
msg.g(option);
}
conn.close();
}
catch (SQLException e) {
System.out.println(e.getMessage()+ " from init");
}
}
} ); // end of display handler
// quit button
final Button resetButton = new Button("Quit");
resetButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Platform.exit();
}
});
final HBox controls = new HBox(10);
controls.setAlignment(Pos.CENTER);
controls.getChildren().addAll(displayButton, resetButton);
final VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.setStyle("-fx-padding: 10; -fx-background-color: cornsilk;");
layout.getChildren().setAll(listView, controls);
layout.setPrefWidth(320);``enter code here
Scene scene = new Scene(layout);
// stage.setScene(new Scene(layout));
stage.setScene(scene);
stage.setTitle("Select one or more options");
stage.show();
}
public static void main(String[] args) { launch(args); }
}
您應該o在你的應用程序中只有一個'Application'子類。目前還不清楚你想用兩個這樣的類來做什麼。也許https://stackoverflow.com/questions/32464698/java-how-do-i-start-a-standalone-application-from-the-current-one-when-both-are有幫助嗎? –
對。我沒有說清楚,第二組代碼multiList類是獨立的版本。我想要做的是顯示兩個GUI。 –
您不能在另一個應用程序中重用'Application'子類。將要重複使用的部分分解爲單獨的課程。這與我上面鏈接的問題相同。 –