我实际上想要构建一个从一个fxml转到另一个fxml的fx应用程序(使用此过程:fxml编号1加载然后单击和另一个加载)。
我已经在我的主类静态中创建了舞台并一次又一次地使用它。
根据ram中堆栈空间的限制是一个好主意?还是有更好的方法?
这些是我的代码的一部分:
public class Main extends Application {
public static Stage stage;
@Override
public void start(Stage primaryStage) throws Exception{
stage=primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("MainWidget.fxml"));
stage.setTitle("Welcome");
stage.setScene(new Scene(root, 300, 275));
stage.show();
}
我的控制器是(加载另一个fxml !!)
public void someButtonController{
Parent root = FXMLLoader.load(getClass().getResource("/View/ShowWidget.fxml"));
Scene scene = new Scene(root,300,300);
Main.stage.setScene(scene);
Main.stage.show();}
答案 0 :(得分:1)
我根本不会公开Stage
。这样做会将您的FXML-Controller对与您的Main
类相结合,并阻止您在没有该类的情况下使用它。
相反,做一些像
这样的事情@FXML
private Button someButton ;
// ...
public void someButtonController{
Window window = someButton.getScene().getWindow();
if (window instanceof Stage) {
Parent root = FXMLLoader.load(getClass().getResource("/View/ShowWidget.fxml"));
Scene scene = new Scene(root,300,300);
Stage stage = (Stage) window ;
stage.setScene(scene);
stage.show(); // isn't it necessarily showing already?
}
}
这里我假设控制器是FXML文件的控制器,表示Node
显示在您尝试访问的Stage
中。如果情况并非如此,你仍然可以做类似的事情,虽然它会更复杂。