我正在使用2个不同的BorderPanes,BorderPane A和BorderPane B编写应用程序。 该应用程序有2个菜单项,单击它时将显示BorderPane A或BorderPane B.如何从JavaFX应用程序类别检索舞台
这是应用程序类,它有我想使用
public class SampleApp extends Application {
private Stage primaryStage;
public static void main(final String[] args) {
launch(args);
}
@Override
public void start(final Stage stage)
throws Exception {
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SampleAppFactory.class);
final SpringFxmlLoader loader = new SpringFxmlLoader(context);
final Parent root = (Parent) loader.load("Main.fxml", Main.class);
final Scene scene = new Scene(root, 600, 480, Color.ALICEBLUE);
this.primaryStage = stage;
scene.getStylesheets().add("fxmlapp.css");
stage.setScene(scene);
stage.show();
}
}
的Main.java类有两个BorderPanes的阶段,当菜单项被选择我想显示上应用程序的borderpane。
是否有人知道如何使用此方法显示Borderpane(设置舞台场景)(showBorderPane)? 我想找回舞台上,并设定德borderpane场景:
public class Main extends BorderPane implements Initializable {
@FXML
private Verbinding verbindingContent; // BORDERPANE A
@FXML
private Beheer beheerContent;// BORDERPANE A
@FXML
private MenuBar menuContent;
@Override
public void initialize(final URL url, final ResourceBundle rb) {
System.out.println(url);
menuContent.setFocusTraversable(true);
}
@FXML
private void showBorderPane(final ActionEvent event) {
final MenuItem menuItem = (MenuItem) event.getSource();
}
@FXML
private void handleCloseAction(final ActionEvent event) {
System.exit(0);
}
}
我的main.xml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.effect.*?>
<?import nl.mamaloe.tab.view.Main?>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="nl.mamaloe.tab.view.Main">
<fx:define>
<fx:include source="scene/Beheer.fxml" fx:id="beheerContent" />
<!-- fx:include source="Menu.fxml" fx:id="menuContent"/-->
</fx:define>
<top>
<MenuBar fx:id="menuContent" onMouseClicked="#handleKeyInput">
<menus>
<Menu text="Systeem">
<items>
<MenuItem text="Verbinding maken" onAction="#handleVerbindingAction"/>
<SeparatorMenuItem />
<MenuItem text="Afsluiten" onAction="#handleCloseAction"/>
</items>
</Menu>
<Menu text="TAB">
<items>
<MenuItem text="Script toevoegen" onAction="#handleAboutAction"/>
<SeparatorMenuItem />
<MenuItem text="Script draaien" />
</items>
</Menu>
<Menu text="About" onAction="#handleAboutAction">
<items>
<MenuItem text="Op basis van wizard" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center> <Label text="Add New Dock of Home" />
</center>
我已经看到它是可能会从应用程序的启动方法中执行此操作。 但我想在Main.java中实现它,因为结构,我主要使用FXML来声明GUI。
我似乎只想在第一个例子中只显示borderPane的中心。但这似乎并不奏效。 因为当我调用this.getScene();从shoBorderPane方法 我得到一个空指针异常,我之前尝试过,因为我也认为主要的Borderpane应该有一个场景。或者我在做什么? – Firebird
我不明白。儿童边界线已经在主边界线内。你为什么试图让它们重新居中?而在你的情况this.setCenter()就够了,不需要this.getScene()。 –
另外附上你的main.fxml以更好地理解。 –