0

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

我制作了一个剪切版本,在选择menuitem时给出了一个system.out.println结果。在“章节”上,它不会加载所选的 MenuItem。

我已经在运行时复制了我的主文件、fxml 和控制器文件以及输出列表。

主要是 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>

控制器是 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>

任何帮助,将不胜感激

4

0 回答 0