[英]New windows open in JavaFX / fxml, but i dont want it to open a new scene
當我運行我的代碼時,我打開一個帶有 3 個按鈕的窗口,我想要的是當我按下一個打開新窗口的按鈕時,它只會“超過”前一個窗口而不是打開一個新窗口,導致我有2 個打開的窗口而不是 1 個。下面是我的 GUIController:
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.io.IOException;
public class GUIController {
public void patientVindue(ActionEvent actionEvent) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/patientGUI.fxml"));
GridPane gridPane = loader.load();
Scene scene = new Scene(gridPane);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
public void lægeVindue(ActionEvent actionEvent) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/lægeGUI.fxml"));
GridPane gridPane = loader.load();
Scene scene = new Scene(gridPane);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
public void sundhedsprofessionelVindue(ActionEvent actionEvent) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/sundhedsprofessionelGUI.fxml"));
GridPane gridPane = loader.load();
Scene scene = new Scene(gridPane);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
public tilbageVindue(ActionEvent actionEvent) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/gui.fxml"));
GridPane gridPane = loader.load();
Scene scene = new Scene(gridPane);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
}
-- 添加更多信息。 這是我使用的另一個類:
public class GUI extends Application {
@Override
public void start(final Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/gui.fxml"));
GridPane gridPane = loader.load();
Scene scene = new Scene(gridPane);
stage.setScene(scene);
stage.show();
}
}
您必須為那個窗口制作新舞台,我建議您將舞台設為靜態,然后將該場景傳遞到該舞台
創建一個全局舞台,然后將新場景設置為該舞台。 您總是在創建一個新舞台,然后將新場景設置為該新舞台,這就是您總是獲得新窗口的原因。
啟動登錄頁面的 MainApp:
public class MainApp extends Application {
private Stage primaryStage;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Access The Unicorn World");
showLogin(this.primaryStage);
}
public static void showLogin(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/Login.fxml"));
loader.setController(new LoginController(primaryStage));
Parent loginLayout = loader.load();
primaryStage.setScene(new Scene(loginLayout));
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
然后對於登錄頁面,我有一個控制器,我在上面設置並通過了階段。 在登錄頁面中,當有人沒有帳戶並且我想打開注冊窗口時,我可以執行以下操作:
private Stage primaryStage;
public LoginController(Stage primaryStage) {
this.primaryStage = primaryStage;
}
@FXML
void onRegisterAction(ActionEvent event) {
showRegister(this.primaryStage);
}
public static void showRegister(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/Register.fxml"));
loader.setController(new RegisterController(primaryStage));
Parent registerLayout = loader.load();
primaryStage.setScene(new Scene(registerLayout));
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.