JavaFX:从MenuItem更改场景

时间:2015-04-13 18:02:05

标签: javafx menuitem fxmlloader

这是一个有两个项目的菜单栏。在"章"我希望能够从MenuItems中选择一个章节并让它加载一个新的.fxml文件。

我已经制作了一个缩减版本,在选择MenuItem时会给出System.out.println结果。在"章"它没有加载所选的MenuItem。

我已将main,fxml和controller文件与运行时的输出列表一起复制。

Main是JavaFXMenulItem.java

package javafxmenuitem;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class JavaFXMenuItem extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLMenuItem.fxml"));        
        Scene scene = new Scene(root);       
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }   
}

FXML文件是FXMLMenulItem.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" fx:id="root" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="javafxmenuitem.FXMLMenuItemController">
   <children>
      <MenuBar layoutY="1.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem fx:id="miclose" mnemonicParsing="false" onAction="#handleButtonAction" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Chapter">
            <items>
              <MenuItem fx:id="michapone" mnemonicParsing="false" onAction="#handleButtonAction" text="Ch.1  Introduction" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </children>
</AnchorPane>

Controller是FXMLMenulItemController.java

package javafxmenuitem;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;

public class FXMLMenuItemController implements Initializable {   
    @FXML
    private MenuItem miclose;
    @FXML
    private MenuItem michapone;
    @FXML
    private Parent root;    
    Stage stage;

    @FXML
    private void handleButtonAction(ActionEvent e) throws IOException {      
        if(e.getSource()==miclose){
            System.out.println("this is Close");
            }    
        else if(e.getSource()==michapone){
            System.out.println("this is chap 1");  

            //get reference to the button's stage
            stage = (Stage) root.getScene().getWindow();
            //load up OTHER FXML document
            root = FXMLLoader.load(getClass().getResource("FXMLMenuItem1.fxml"));

            //create a new scene with root and set the stage
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
            }      
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }       
}

输出列表是

ant -f C:\\Users\\john\\Documents\\NetBeansProjects\\JavaFXMenuItem jfxsa-run
init:
Deleting: C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\build\built-jar.properties
Compiling 2 source files to C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\build\classes
Copying 2 files to C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\build\classes
compile:
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from C:\Program Files\Java\jdk1.8.0_31\jre\..\lib\ant-javafx.jar
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing.
         Please set manifest.custom.codebase property to override the current default non-secure value '*'.
Launching <fx:deploy> task from C:\Program Files\Java\jdk1.8.0_31\jre\..\lib\ant-javafx.jar
jfx-deployment-script:
jfx-deployment:
jar:
Copying 12 files to C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\dist\run644417306
jfx-project-run:
Executing C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\dist\run644417306\JavaFXMenuItem.jar using platform C:\Program Files\Java\jdk1.8.0_31\jre/bin/java
this is chap 1
Deleting directory C:\Users\john\Documents\NetBeansProjects\JavaFXMenuItem\dist\run644417306
jfxsa-run:
BUILD SUCCESSFUL (total time: 18 seconds)

第二个.fxml文件FXMLMenuItem1.fxml(只是AnchorPane等):

<AnchorPane id="AnchorPane" fx:id="root" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="javafxmenuitem.FXMLMenuItemController">
   <children>
      <MenuBar layoutY="1.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem fx:id="miclose" mnemonicParsing="false" onAction="#handleButtonAction" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Chapter">
            <items>
              <MenuItem fx:id="michapone" mnemonicParsing="false" onAction="#handleButtonAction" text="Ch.1  Introduction" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </children>
</AnchorPane>

任何帮助将不胜感激

0 个答案:

没有答案