2012-05-25 31 views
3

我正在使用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。

回答

1

当FXMLLoader加载“Main.fxml”文件时,会创建一个新的BorderPane(在Main.fxml中定义)。加载器还创建/初始化其控制器类,这是另一个带有自己实例的BorderPane派生类。所以有两个不同的BorderPanes实例。你的设计是通用的方法有些不同,来实现自己的目标,我建议增加一个集装箱到中心FXML文件是这样的:

<center> 
    <StackPane fx:id="pane"> 
     <children> 
       <Label text="Add New Dock of Home" /> 
     </children> 
    </StackPane> 
</center> 

你可以用任何你想要的窗格/布局改变StackPane。 接下来让控制器类到它的链接:

@FXML 
private StackPane pane; 

,你应该删除“扩展BorderPane”,因为它是没有任何意义了。最后,

@FXML 
private void showBorderPane(final ActionEvent event) { 
    final MenuItem menuItem = (MenuItem) event.getSource(); 
    pane.getChildren().clear(); // Clear old content. 
    switch (menuItem.getText()) { 
     case "Borderpane A": 
      pane.getChildren().add(borderPaneA); 
      break; 
     case "Borderpane B": 
      pane.getChildren().add(borderPaneB); 
      break; 
     default: 
      pane.getChildren().add(new Label("Add New Dock of Home")); 
    } 
} 
+0

我似乎只想在第一个例子中只显示borderPane的中心。但这似乎并不奏效。 因为当我调用this.getScene();从shoBorderPane方法 我得到一个空指针异常,我之前尝试过,因为我也认为主要的Borderpane应该有一个场景。或者我在做什么? – Firebird

+0

我不明白。儿童边界线已经在主边界线内。你为什么试图让它们重新居中?而在你的情况this.setCenter()就够了,不需要this.getScene()。 –

+0

另外附上你的main.fxml以更好地理解。 –