JavaFX-包含带有事件的fxml

JavaFX-包含带有事件的fxml,java,events,model-view-controller,javafx,fxml,Java,Events,Model View Controller,Javafx,Fxml,我目前正试图用JavaFX实现一个非常基本的应用程序,只是为了做一些测试。这里的最终目标是实现一个分为几个部分的接口,每个部分都有自己的.fxm和控制器 首先,我尝试使用此体系结构开发一个基本应用程序: 我有一个主VueGlobale.fxml文件,其中包括另一个.fxml文件clavier.fxml: VueGlobale.fxml <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.layout.HB

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

首先,我尝试使用此体系结构开发一个基本应用程序:

我有一个主VueGlobale.fxml文件,其中包括另一个.fxml文件clavier.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>
<?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>
<?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>
型号 包装模型; 公共类模型{

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

Main

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);
    }
}
问题是:我不知道在我的main中应该做什么,这样我就可以给我的clavier.fxml一个ClavierController(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

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);
    }
}
您还可以使用反射来实现逻辑,如“如果提供的控制器类有一个采用模型的构造函数,则调用该构造函数并传入模型,否则调用默认构造函数”:

现在为这个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();
这一切都应该很顺利。

ButtonPane.fxml
    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);
    }
}
---------------- PaneOne.fxml --------------- 按钮控制器 -------------------- 导入java.net.URL; 导入java.util.ResourceBundle; 导入com.jfoenix.controls.JFXButton; 导入javafx.fxml.fxml; 导入javafx.fxml.Initializable; 导入javafx.scene.layout.ancorpane; 导入javafx.scene.layout.HBox; 公共类ButtonPaneController实现可初始化{ @FXML 公共jfx按钮btnClose; @FXML 公共jfx按钮btnDelete; @FXML 公共jfx按钮btnAdd; @FXML 公共jfx按钮btnEdit; @FXML 私人锚烷按钮; @FXML 专用HBox按钮; @凌驾 公共void初始化(URL位置、ResourceBundle资源){ btnClose.setOnAction(事件->{ System.out.println(“In按钮窗格”); }); btnAdd.setOnAction(事件->{ System.out.println(“In按钮窗格”); }); } 公共无效隐藏按钮(JFXButton…JFXButton){ 对于(int i=0;i{ System.out.println(“窗格一”); }); } } 导入javafx.application.application; 导入javafx.fxml.fxmloader; 导入javafx.scene.Parent; 导入javafx.scene.scene; 导入javafx.scene.control.Label; 导入javafx.stage.stage; 导入javafx.stage.StageStyle; 公共类主扩展应用程序{ 公共静态阶段初级阶段; 公共静态标签lblTitle; @凌驾 公共无效开始(阶段primaryStage){ 试一试{ Main.primaryStage=primaryStage; 父根=fxmloader.load(Main.class.getResource(“view/PaneOne.fxml”); 场景=新场景(根); scene.getStylesheets().add(Main.class.getResource(“view/css/application.css”).toExternalForm()); 初级阶段。场景(场景); 初级阶段。初始风格(阶段风格。未装饰); primaryStage.show(); }捕获(例外e){ e、 printStackTrace(); } } 公共静态void main(字符串[]args){ 发射(args); } }
执行此操作的步骤:

  • 创建ButtonPane.fxml并声明锚定窗格的id和所有 按钮
  • 使用
    ,重要的是当您包含fxml时必须 声明id与ButtonPane.fxml的锚定窗格id相同
  • 为初始化变量创建ButtonPaneController
  • 创建PaneonController并使用 @FXML注释
  • 运行程序,它与按钮的动作事件工作良好

  • 您发布的代码无法编译。即使您告诉我们什么是
    model
    ,在
    ClavierController
    中也没有构造函数,只有默认构造函数。即使您解决了这个问题,也可能存在其他问题,因为您希望第一个FXML使用
    VueGlobaleController
    ,但控制器工厂返回的是
    ClavierController
    ,您好,谢谢您的回答,这对该型号来说是正确的,我在这里发布之前已经修改了代码,我忘记了更改这部分。好的,我明白你的意思,但是我应该在主体中放些什么,这样才能影响到这一事件?(因为即使我将ClavierController更改为VueGlobaleController,我仍然会收到相同的错误消息)因此,您可以修复它吗?这里的任何人都不可能区分您在实际代码中犯下的错误(导致异常的错误-您应该发布c
    public class TopController {
    
        private Model model ;
    
        public void setModel(Model model) {
            this.model = model ;
        }
    
        @FXML
        private void someHandlerMethod(ActionEvent event) {
            model.doSomething();
        }
    }
    
    Model model = new Model();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/path/to/fxml"));
    loader.setController(new MainController(model));
    Parent root = loader.load();
    
        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);
        }
    }