我有一个名为player.fxml
的主页,它是一个BorderPane。类似这样的
<BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="cn.will.controller.PlayerController"
fx:id="root"
prefHeight="670.0" prefWidth="1020.0" stylesheets="@../css/player.css">
<top>
<fx:include source="titleBar.fxml"/>
</top>
<left>
<fx:include source="userLeft.fxml"/>
</left>
<center>
<fx:include source="albumDetail.fxml"/>
</center>
<bottom>
<fx:include source="playBar.fxml"/>
</bottom>
</BorderPane>
在 Main.class 中我启动应用程序。
@SpringBootApplication
public class Main extends AbstractJavaFxApplicationSupport implements ApplicationContextAware{
private static BorderPane root;
@Autowired
private ApplicationContext applicationContext;
public static void main(String[] args) {
launchApp(Main.class, PlayerView.class, new BlueprintSplashScreen(),args);
}
@Override
public void start(Stage stage) throws Exception {
super.start(stage);
stage.getIcons().add(new Image("img/music-icon32.png"));
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
// ignore this. It's something I tried to meet my requirement.
//I try to use
//PlayerView view = applicationContext.getBean(PlayerView .class);
//root = (BorderPane) view.getView();
//but it throws NullPointerException dor applicationContext, it seems inject fail
public ApplicationContext getApplicationContext() {
return applicationContext;
}
}
我想动态更改 BorderPane 中的中心内容。我确实需要获取此 BorderPane,以便可以更改它。 顺便说一句,我也使用Main.showView(xxx.class),但我只是想改变中心。而不是整个舞台。 非常感谢。
最佳答案
我对spring更加熟悉了。通过使用springboot-javafx-support注释@FXMLView, View 已经注册到spring bean工厂。所以当我想使用 View 时就使用它。 使用 @Autowire @Resources 将 View 注入(inject)到您想要的位置。这样您就可以获取 View 并调用 getView() 方法来获取根 Pane 。
关于java - 我可以使用 springboot-javafx-support 从初级阶段获取根 Pane 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48237646/