代码之家  ›  专栏  ›  技术社区  ›  Paul Thirozier

JavaFX-包含包含事件的fxml

  •  -1
  • Paul Thirozier  · 技术社区  · 7 年前

    我目前正试图用JavaFX实现一个非常基本的应用程序,只是为了做一些测试。这里的最终目标是实现一个分为几个部分的接口,每个部分都有自己的接口。fxm和控制器。

    首先,我尝试用这种架构开发一个基本的应用程序:

    Project explorer

    我有一个主要的VueGlobale。fxml文件,其中包括另一个。fxml文件 键盘。fxml

    VueGlobale。fxml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.VBox?>
    
    <VBox
    maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="529.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1">
       <children>
            <fx:include source="clavier.fxml" fx:id="clavier" />
       </children>
    </VBox>
    

    键盘。fxml

    <?xml version="1.0" encoding="UTF-8"?>
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.layout.AnchorPane?>
    
    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.ClavierController">
       <children>
          <Button layoutX="274.0" layoutY="188.0" mnemonicParsing="false" onAction="#ajouterDo" text="Button" />
       </children>
    </AnchorPane>
    

    A这是 : 机组控制器;

    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import model.Model;
    
    public class ClavierController  {
    
        private Model model;
    
        public ClavierController(Model m) {
            this.model = m;
        }
    
        @FXML
        public void ajouterDo(ActionEvent e){
            System.out.println("Click !");
            this.model.doSomething();
        }
    }
    

    模型 包装型号; 公共类模型{

    public void doSomething() {
        System.out.println("Model !");
    }
    

    }

    主要的

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception{
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("views/VueGlobale.fxml"));
    
            Parent root = loader.load();
            primaryStage.setTitle("Test");
            primaryStage.setScene(new Scene(root, 300, 275));
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    问题是:我不知道该怎么做,这样我才能给我的键盘。fxml a键盘控制器(m型)。(它使用的是一个没有参数的控制器,但如果我需要精确的参数呢?)

    这是StackTrace,如果它能帮助您:

    Exception in Application start method
    java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
    Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
        at java.lang.Thread.run(Unknown Source)
    Caused by: javafx.fxml.LoadException: 
    /J:/Programming/Telecom%20Nancy/S3/Test/bin/views/clavier.fxml:6
    /J:/Programming/Telecom%20Nancy/S3/Test/bin/views/VueGlobale.fxml:9
    
        at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
        at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
        at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
        at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
        at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
        at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
        at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.access$2700(FXMLLoader.java:103)
        at javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1143)
        at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
        at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
        at Main.start(Main.java:14)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
        ... 1 more
    Caused by: java.lang.InstantiationException: controllers.ClavierController
        at java.lang.Class.newInstance(Unknown Source)
        at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
        at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
        ... 23 more
    Caused by: java.lang.NoSuchMethodException: controllers.ClavierController.<init>()
        at java.lang.Class.getConstructor0(Unknown Source)
        ... 26 more
    Exception running application Main
    

    提前感谢您的帮助和时间,祝您度过愉快的一天。

    编辑:

    通过包含几个fxml,我的意思是:

    VueGlobale。fxml

    <?xml version="1.0" encoding="UTF-8"?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.VBox?>
    <VBox
    maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="529.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1">
       <children>
            <fx:include source="viewtop.fxml" fx:id="top" />
       </children>
    <children>
            <fx:include source="clavier.fxml" fx:id="clavier" />
       </children>
    <children>
            <fx:include source="viewbottom.fxml" fx:id="bottom" />
       </children>
    </VBox>
    

    所以我有三个。VueGlobale中包含的fxml文件。fxml。让我们假设这一切。fxml有自己的控制器(ClavierController.java、TopController.java、BottomController.java)。所有这些控制器都需要模型。我应该在我的主要工厂做什么?像这样的事情是行不通的:

    import controllers.ClavierController;
    import controllers.TopController;
    import controllers.BottomController;
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    import model.Model;
    
    public class Main extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception{
            Model m = new Model();
    
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("views/VueGlobale.fxml"));
            loader.setControllerFactory(ic -> new ClavierController(m));
            loader.setControllerFactory(ic -> new TopController(m));
            loader.setControllerFactory(ic -> new BottomController(m));
    
            Parent root = loader.load();
            primaryStage.setTitle("Test");
            primaryStage.setScene(new Scene(root, 300, 275));
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Himanshu THE ONLY ONE    5 年前
        ButtonPane.fxml
        ----------------
        <?xml version="1.0" encoding="UTF-8"?>
    
        <?import com.jfoenix.controls.*?>
        <?import java.lang.*?>
        <?import javafx.scene.layout.*?>
        <?import javafx.scene.layout.AnchorPane?>
    
        <AnchorPane fx:id="buttonPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="30.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.everest.amcu.ButtonPaneController">
            <children>
                <HBox fx:id="hboxButton" alignment="CENTER_LEFT" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" spacing="5.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <children>
                        <JFXButton fx:id="btnAdd" maxHeight="1.7976931348623157E308" maxWidth="100.0" prefWidth="50.0" text="Add" HBox.hgrow="ALWAYS" />
                        <JFXButton fx:id="btnEdit" layoutX="10.0" layoutY="10.0" maxHeight="1.7976931348623157E308" maxWidth="100.0" prefWidth="50.0" text="Edit" HBox.hgrow="ALWAYS" />
                        <JFXButton fx:id="btnDelete" layoutX="62.0" layoutY="10.0" maxHeight="1.7976931348623157E308" maxWidth="100.0" prefWidth="50.0" text="Delete" HBox.hgrow="ALWAYS" />
                        <JFXButton fx:id="btnClose" layoutX="114.0" layoutY="10.0" maxHeight="1.7976931348623157E308" maxWidth="100.0" prefWidth="50.0" text="Close" HBox.hgrow="ALWAYS" />
                    </children>
                </HBox>
            </children>
        </AnchorPane>
    
    
    
        PaneOne.fxml
        ---------------    
                <?xml version="1.0" encoding="UTF-8"?>
                          <?import javafx.scene.layout.AnchorPane?>
                          <AnchorPane xmlns:fx="http://javafx.com/fxml/1"       fx:controller="com.everest.amcu.PaneOneController">     
                 <fx:include source="ButtonPane.fxml" fx:id="buttonPane" /> </AnchorPane>
    
    ButtonPaneController
    --------------------
    
    import java.net.URL;
    import java.util.ResourceBundle;
    import com.jfoenix.controls.JFXButton;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.HBox;
    
    public class ButtonPaneController implements Initializable {
    
        @FXML
        public JFXButton btnClose;
    
        @FXML
        public JFXButton btnDelete;
    
        @FXML
        public JFXButton btnAdd;
    
        @FXML
        public JFXButton btnEdit;
    
        @FXML
        private AnchorPane buttonPane;
    
        @FXML
        private HBox hboxButton;
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
            btnClose.setOnAction(event -> {
                System.out.println("In Button Pane");
            });
            btnAdd.setOnAction(event -> {
                System.out.println("In Button Pane");
            });
        }
    
        public void hideButton(JFXButton... jfxButton) {
            for (int i = 0; i < jfxButton.length; i++) {
                hboxButton.getChildren().remove(jfxButton[i]);
            }
        }
    
    }
    
    PaneOneController
    ------------------
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    
    public class PaneOneController implements Initializable {
    
        @FXML
        ButtonPaneController buttonPaneController;
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
            buttonPaneController.btnAdd.setOnAction(event -> {
                System.out.println("In Pane One");
            });
        }
    }
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.stage.Stage;
    import javafx.stage.StageStyle;
    
    public class Main extends Application {
    
        public static Stage primaryStage;
        public static Label lblTitle;
    
        @Override
        public void start(Stage primaryStage) {
            try {
                Main.primaryStage = primaryStage;
                Parent root = FXMLLoader.load(Main.class.getResource("view/PaneOne.fxml"));
                Scene scene = new Scene(root);
                scene.getStylesheets().add(Main.class.getResource("view/css/application.css").toExternalForm());
                primaryStage.setScene(scene);
                primaryStage.initStyle(StageStyle.UNDECORATED);
                primaryStage.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    操作步骤:

    1. 创建按钮平面。fxml和声明锚定窗格的id以及所有 按钮
    2. 创建PaneOne。fxml和include按钮。fxml使用
      ,重要的是当您包括fxml时必须 声明id与ButtonPane的锚定窗格id相同。fxml。
    3. 为初始化变量创建ButtonPaneController。
    4. 创建PaneonController,只需使用 @FXML注释。
    5. 运行程序,它与按钮的动作事件工作良好。
        2
  •  0
  •   James_D    7 年前

    这个 controllerFactory 与任何其他财产一样是一项财产。如果你设置了它的值,它就有这个值,所以在三行连续的代码中将它的值设置为三个不同的值是没有意义的。

    您可以创建一个控制器工厂,只需检查参数并返回适当的控制器:

    public class Main extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception{
            Model m = new Model();
    
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("views/VueGlobale.fxml"));
            loader.setControllerFactory(ic -> {
                if (ic == ClavierController.class) {
                    return new ClavierController(m);
                } else if (ic == TopController.class) {
                    return new TopController(m);
                } else if (ic == BottomController.class) {
                    return new BottomController(m) ;
                }
                throw new IllegalArgumentException("Unexpected controller type: "+ic.getName());
            });
    
            Parent root = loader.load();
            primaryStage.setTitle("Test");
            primaryStage.setScene(new Scene(root, 300, 275));
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    您还可以使用反射来实现逻辑,例如“如果提供的控制器类有一个采用模型的构造函数,则调用该构造函数并传入模型,否则调用默认构造函数”:

    loader.setControllerFactory(ic -> {
        try {
            for (Constructor<?> c : ic.getConstructors()) {
                if (c.getParameterCount() == 1 && c.getParameterTypes()[0]==Model.class) {
                    return c.newInstance(m);
                }
            }
            return ic.newInstance();
        } catch (Exception e) {
            // fatal...
            throw new RuntimeException(e);
        }
    });
    

    不过,对于你想要实现的目标来说,这一切都有点过于苛刻。您只是试图将模型传递给嵌套控制器(通过加载的FXML文件的控制器) <fx:include> ). 这个 documentation 为此明确提供了一种基于注入的方法。

    具体来说,如果您的“主”FXML添加 fx:id s到 <外汇:包括> s、 然后控制器可以注入主控制器。所以你可以这样做:

    <?xml version="1.0" encoding="UTF-8"?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.VBox?>
    <VBox
    maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="529.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1">
       <children>
            <fx:include source="viewtop.fxml" fx:id="top" />
       </children>
    <children>
            <fx:include source="clavier.fxml" fx:id="clavier" />
       </children>
    <children>
            <fx:include source="viewbottom.fxml" fx:id="bottom" />
       </children>
    </VBox>
    

    现在为这个FXML文件定义一个“主”控制器:

    public class MainController {
    
        @FXML
        private Parent top ;
        @FXML
        private Parent clavier ;
        @FXML
        private Parent bottom ;
    
        @FXML
        private TopController topController ;
        @FXML
        private ClavierController clavierController ;
        @FXML
        private BottomController bottomController ;
    
        private final Model model ;
    
        public MainController(Model model) {
            this.model = model ;
        }
    
        @FXML
        public void initialize() {
            topController.setModel(model);
            clavierController.setModel(model);
            bottomController.setModel(model);
        }
    
        // ...
    }
    

    只需使用设置模型的适当方法定义“嵌套”控制器:

    public class TopController {
    
        private Model model ;
    
        public void setModel(Model model) {
            this.model = model ;
        }
    
        @FXML
        private void someHandlerMethod(ActionEvent event) {
            model.doSomething();
        }
    }
    

    最后,在主fxml中加载:

    Model model = new Model();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/path/to/fxml"));
    loader.setController(new MainController(model));
    Parent root = loader.load();
    

    一切都应该很好。