[英]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.