English 中文(简体)
如何从 JavaFX 应用程序类获取阶段
原标题:How to retrieve stage from JavaFX Application Class

I am writing an application with 2 different BorderPanes, BorderPane A and BorderPane B. The application has 2 menuitems, such that, when clicked it HAS to show BorderPane A or BorderPane B.

这是应用程序类, 它有我要使用的阶段

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

在选择菜单时, 主雅瓦级有两个边框, 我想在应用程序上显示边板 。

Does someone knows how to show the Borderpane(set the Scene on Stage) from this method(showBorderPane)? I d like to retrieve the Stage and set the scene with de 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);
}

时 时

我的主体. 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>

I ve seen it is possible to do this from the start method of the application. But I d like to implement it in the Main.java because of structure and I am using mainly FXML to declare the GUI.

最佳回答

当 FXMLLoader 装入“ 主程序” 文件时, 将创建一个新的 BrainPane (定义在 Main. fxml 中) 。 装载器还将创建/ 初始化其控制器类, 这是另一个由BrontPane 衍生的类别 。 因此有两种不同的 BrontPanes 实例。 您的设计与一般方法略有不同, 但是, 为了实现您的目标, 我建议将一个容器添加到 FXML 文件的中心 :

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

You can change the StackPane with any pane/layout you want. Next make a link to it in the controller class:

@FXML
private StackPane pane;

您的“ 强” 应 < / 强” 删除“ 扩展边界Pane ”, 因为它已经没有意义了。 最后,

@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"));
    }
}
问题回答

暂无回答




相关问题
JavaFX with Maven [closed]

I recently started a JavaFX project, and I d like to use Maven as my compiler/deployment tool. Is there a good tutorial or plugin to integrate JavaFX and Maven?

Displaying a string with variables in JavaFX

I have little quiz game I ve working on and am tracking correct answers. I have a text object that displays the number of consecutive correct answers. This is the content: content: bind consecutive....

non java-developer question

Just a basic question about Java (haven t really done anything with it personally yet): Is it possible to write a Java program that runs in a web browser (via JRE) on the client machine? Is ...

Pros and Cons of JavaFX and Silverlight [closed]

I know there is already a question about the performance of Flex, JavaFX, and Silverlight. My question is a bit more broad: We are evaluating the merits of JavaFX and Silverlight to serve as the GUI ...

How to simplify SVG code?

Is it possible to simplify / clean up svg code by replacing the use-tags with standard svg elements? Maybe an inkscape plugin? Haven t found anything... Background: I m converting some svgs to javafx ...

Dynamically changing a keyframes time attribute in JavaFX

I am making a game in JavaFX and have implemented a slider for controlling the game speed. I have a simple slider (javafx.scene.control.Slider) and I m binding the time attribute for the gameloop to ...

Migrate Java Applet to what/where?

I am reviewing currently a medium size code base (around 30K LOC) which uses a huge Applet and interfaces with other systems. It s a tool to create custom labels, so we need drag-n-drop and other ...

热门标签