JavaFX 具有不同窗格的相同文本区域
JavaFX Same textarea with different Pane's
我有一个包含 maven、javafx 和 fxml 的项目。我有一个主要的 BorderPane welcome.fxml
和 Pane ready.fxml
.
我的启动方法是;
@Override
public void start(Stage primaryStage) throws Exception {
try {
Pane root = (Pane) FXMLLoader.load(getClass().getResource("welcome.fxml"));
Scene scene = new Scene(root, 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
makeAlert(e, false);
}
}
现在,我的 welcome.fxml
中有一个按钮,我想用 ready.fxml
更改 BorderPane 的中心。这是我的按钮处理程序;
@FXML
private void buttonHandler() throws IOException, InterruptedException {
stage = (Stage) myButton.getScene().getWindow();
Pane sub = (Pane) FXMLLoader.load(getClass().getResource("../ready.fxml"));
BorderPane root = (BorderPane) FXMLLoader.load(getClass().getResource("../welcome.fxml"));
root.setCenter(sub);
//Scene scene = new Scene(root, 640, 480);
//stage.getScene().setRoot(root);
}
UPDATE:这是我的错误,正如@James_D所注意到的,我在我的控制器中再次加载了welcome.fxml
,因此,我的整个场景发生了变化只有中心。
正确的方法应该是;
stage = (Stage) brokerConnect.getScene().getWindow();
Pane center = (Pane) FXMLLoader.load(getClass().getResource("../ready.fxml"));
// FIXME: Get root like this
BorderPane root = (BorderPane) stage.getScene().getRoot();
root.setCenter(center);
已编辑:Java 添加了代码。
您应该更新 现有 边框窗格的中心,而不是创建新边框并设置新边框的中心。
您只需按照通常的方式将边框窗格注入控制器即可。所以在welcome.fxml
的根元素上加一个fx:id
:
<!-- imports, etc... -->
<BorderPane fx:id="root" fx:controller="..." xmlns:fx="..." ... >
<!-- ... -->
</BorderPane>
然后在控制器中
public class Controller { /* or whatever name you have, again, you can't be bothered to post a MCVE */
@FXML
private BorderPane root ;
@FXML
private void buttonHandler() throws IOException {
Pane sub = (Pane) FXMLLoader.load(getClass().getResource("../ready.fxml"));
root.setCenter(sub);
}
// ...
}
我有一个包含 maven、javafx 和 fxml 的项目。我有一个主要的 BorderPane welcome.fxml
和 Pane ready.fxml
.
我的启动方法是;
@Override
public void start(Stage primaryStage) throws Exception {
try {
Pane root = (Pane) FXMLLoader.load(getClass().getResource("welcome.fxml"));
Scene scene = new Scene(root, 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
makeAlert(e, false);
}
}
现在,我的 welcome.fxml
中有一个按钮,我想用 ready.fxml
更改 BorderPane 的中心。这是我的按钮处理程序;
@FXML
private void buttonHandler() throws IOException, InterruptedException {
stage = (Stage) myButton.getScene().getWindow();
Pane sub = (Pane) FXMLLoader.load(getClass().getResource("../ready.fxml"));
BorderPane root = (BorderPane) FXMLLoader.load(getClass().getResource("../welcome.fxml"));
root.setCenter(sub);
//Scene scene = new Scene(root, 640, 480);
//stage.getScene().setRoot(root);
}
UPDATE:这是我的错误,正如@James_D所注意到的,我在我的控制器中再次加载了welcome.fxml
,因此,我的整个场景发生了变化只有中心。
正确的方法应该是;
stage = (Stage) brokerConnect.getScene().getWindow();
Pane center = (Pane) FXMLLoader.load(getClass().getResource("../ready.fxml"));
// FIXME: Get root like this
BorderPane root = (BorderPane) stage.getScene().getRoot();
root.setCenter(center);
已编辑:Java 添加了代码。
您应该更新 现有 边框窗格的中心,而不是创建新边框并设置新边框的中心。
您只需按照通常的方式将边框窗格注入控制器即可。所以在welcome.fxml
的根元素上加一个fx:id
:
<!-- imports, etc... -->
<BorderPane fx:id="root" fx:controller="..." xmlns:fx="..." ... >
<!-- ... -->
</BorderPane>
然后在控制器中
public class Controller { /* or whatever name you have, again, you can't be bothered to post a MCVE */
@FXML
private BorderPane root ;
@FXML
private void buttonHandler() throws IOException {
Pane sub = (Pane) FXMLLoader.load(getClass().getResource("../ready.fxml"));
root.setCenter(sub);
}
// ...
}