2016-10-24 18 views
-1

我有一个使用maven,javafx和fxml的项目。我有一个主要的BorderPane,welcome.fxml和Pane,ready.fxmlJavaFX与不同窗格的相同文本区

我的开始方法是;

@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,我想改变我的BorderPane的中心,ready.fxml。这是我的按钮处理程序;

@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等等,我的整个场景的变化insted的唯一中心。

正确的方法应该是;

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代码。

+0

你为什么要更换场景的全根,如果你只是想更换边框的中心窗格?就目前而言,你的问题并没有真正的答案......我建议你创建一个[MCVE]并[编辑]你的问题来包含它。解释你得到的行为与你想要的行为有什么不同。 –

+0

我执行这个之后,'root.setCenter(newPane);'中心没有改变,所以我替换rootScene。我刚开始学习JavaFX,对于我的无知感到抱歉。 –

+0

然后你在没有向我们显示的代码中做错了什么。这并不是说你不知道JavaFX在这里令人沮丧,而是你发布了一个没有任何人需要回答的信息的问题。 –

回答

1

您应该更新现有边框窗格的中心,而不是创建一个新窗口并设置新窗口的中心。

所有你需要的是以通常的方式注入边界窗格到控制器中。所以需要添加fx:idwelcome.fxml根元素:

<!-- 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); 
    } 

    // ... 
} 
相关问题