控制器与JavaFX中的主类之间的通信

我刚刚开始使用JavaFX。我正在为程序开发GUI,并且已经使用SceneBuilder设计了GUI。我目前有一个使用FXMLLoader加载.fxml文件并显示它的主类。该FXML文件具有另一个称为“控制器”的类作为控制器。就像我说的那样,我与JavaFX的关系不大,所以我可能做错了。

现在,我有两个问题:首先,我想在两个类之间进行通信(代码如下)。其次,我想知道如何将GUI的主类中的代码集成到我已经存在的主类中,我已经尝试过了,但是我总是只收到错误消息(后面是stacktrace和代码)。

希望有人能帮助我

isi_ko

我的GUI主班

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 {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent normalParent = FXMLLoader.load(getClass().getResource("normalLayout.fxml"));

        primaryStage.setTitle("Test");
        primaryStage.setScene(new Scene(normalParent, primaryStage.getWidth(), primaryStage.getHeight()));
        primaryStage.show();
    }
}

我的GUI控制器类

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class Controller implements EventHandler<ActionEvent> {

    @FXML Label activePercent;
    @FXML Label activeLabel;
    @FXML Label activeNumber;

    @FXML Label lastLable;
    @FXML Label lastNumber;

    @FXML Label nextLabel;
    @FXML Label nextNumber;

    @FXML Label faderValue1;
    @FXML Label faderValue2;
    @FXML Label faderValue3;
    @FXML Label faderValue4;
    @FXML Label faderValue5;

    @FXML
    public void test() {
    }

    @Override
    public void handle(ActionEvent event) {
        getFaderValueLable(1).setText("1");
        getFaderValueLable(2).setText("2");
        getFaderValueLable(3).setText("3");
        getFaderValueLable(4).setText("4");
        getFaderValueLable(5).setText("5");
    }

    public Label getFaderValueLable(int i){
        switch (i){
            case 1:
                return faderValue1;
            case 2:
                return faderValue2;
            case 3:
                return faderValue3;
            case 4:
                return faderValue4;
            case 5:
                return faderValue5;
            default:
                return null;
        }
    }
}

我的普通班(没有GUI代码)

import com.fazecast.jSerialComm.SerialPort;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class Main{

    Fader[] faders;
    final int baudRate = 1000000;                   //The Speed that is Used to Communicate with the Arduino
    private int physicalFaderAmount;                        //Amount of Physical Faders, counted from 1
    private int faderPages;                         //Amount of Faderpages, counted from 1
    private int activeFaderpage;                    //The Currently active "physical" Faderpage
    private ArduinoController arduinoController;    //The Class that communicates with the Arduino

    private EosOscController oscController;         //The Class that Communicates with Eos

    //region Getters and Setters
    public void setPhysicalFaderAmount(int physicalFaderAmount) {
        this.physicalFaderAmount = physicalFaderAmount;
    }

    public void setFaderPages(int faderPages) {
        this.faderPages = faderPages;
    }

    public void setActiveFaderpage(int activeFaderpage) {
        this.activeFaderpage = activeFaderpage;
    }

    public void setArduinoController(ArduinoController arduinoController) {
        this.arduinoController = arduinoController;
    }

    public void setOscController(EosOscController oscController) {
        this.oscController = oscController;
    }

    public int getPhysicalFaderAmount() {
        return physicalFaderAmount;
    }

    public int getFaderPages() {
        return faderPages;
    }

    public int getActiveFaderpage() {
        return activeFaderpage;
    }

    public int getBaudRate() {
        return baudRate;
    }

    public ArduinoController getArduinoController() {
        return arduinoController;
    }

    public EosOscController getOscController() {
        return oscController;
    }
    //endregion

    Main(String[] args) {
        //Todo Redo this part when UI done
        //at the Moment here are just some default values

        activeFaderpage = 0;
        physicalFaderAmount = 5;
        faderPages = 4;

        faders = new Fader[physicalFaderAmount];
        for (int i = 0; i < physicalFaderAmount; i++){
            faders[i] = new Fader(faderPages, i);
        }

        oscController = new EosOscController(8001, 8000, "192.168.178.133", this);
        arduinoController = new ArduinoController(SerialPort.getCommPort("COM4"), this);
    }

    public static void main(String[] args) {
        Main main = new Main(args);
    }

    //  This Class is for Storing Information about a particular physical Fader
    public class Fader {

        public int[][] values;
//        The last 3 Falues the Fader has on the Different Faderpages.
//        The First Koordinate is for the Page, the second coordinate is for wich of the last 3 Values you whant to use.
//        The First Value of Every page is the Current value of the Fader
//        The Others are for detecting inconsistant Potivalues

        public int faderID;         //The number of the Physical Fader, starting from 0.

//      Uses local values, not OSC
//      Sets the Value of the Fader
//      Can send the new Value direcktly to the Arduino or Eos
        public void setValue(int physicalPage, int value, boolean sendToEos, boolean sendToArduino) {
            if (value != this.values[physicalPage][2] || (Math.max(values[physicalPage][1], value) - Math.min(values[physicalPage][1], value)) > 1) {
                values[physicalPage][0] = value;

                //Todo: Test this
                if (sendToEos) {

                    oscController.setFaderIntensity(physicalPage, faderID, value / 100.0D, true);
                }

                //Todo Test this
                if (sendToArduino) {
                    arduinoController.sendMessage(new ArduinoController.MessageToArduino(faderID, value), true);
                }
            }

            values[physicalPage][2] = values[physicalPage][1];
            values[physicalPage][1] = value;
        }

        Fader(int pages, int faderID) {
            values = new int[pages][3];
            this.faderID = faderID;
        }

    }
}

我的普通主类(带有GUI代码)

import com.fazecast.jSerialComm.SerialPort;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class Main extends Application{

    Fader[] faders;
    final int baudRate = 1000000;                   //The Speed that is Used to Communicate with the Arduino
    private int physicalFaderAmount;                        //Amount of Physical Faders, counted from 1
    private int faderPages;                         //Amount of Faderpages, counted from 1
    private int activeFaderpage;                    //The Currently active "physical" Faderpage
    private ArduinoController arduinoController;    //The Class that communicates with the Arduino

    private EosOscController oscController;         //The Class that Communicates with Eos

    //region Getters and Setters
    public void setPhysicalFaderAmount(int physicalFaderAmount) {
        this.physicalFaderAmount = physicalFaderAmount;
    }

    public void setFaderPages(int faderPages) {
        this.faderPages = faderPages;
    }

    public void setActiveFaderpage(int activeFaderpage) {
        this.activeFaderpage = activeFaderpage;
    }

    public void setArduinoController(ArduinoController arduinoController) {
        this.arduinoController = arduinoController;
    }

    public void setOscController(EosOscController oscController) {
        this.oscController = oscController;
    }

    public int getPhysicalFaderAmount() {
        return physicalFaderAmount;
    }

    public int getFaderPages() {
        return faderPages;
    }

    public int getActiveFaderpage() {
        return activeFaderpage;
    }

    public int getBaudRate() {
        return baudRate;
    }

    public ArduinoController getArduinoController() {
        return arduinoController;
    }

    public EosOscController getOscController() {
        return oscController;
    }
    //endregion

    Main(String[] args) {
        //Todo Redo this part when UI done
        //at the Moment here are just some default values

        activeFaderpage = 0;
        physicalFaderAmount = 5;
        faderPages = 4;

        faders = new Fader[physicalFaderAmount];
        for (int i = 0; i < physicalFaderAmount; i++){
            faders[i] = new Fader(faderPages, i);
        }

        oscController = new EosOscController(8001, 8000, "192.168.178.133", this);
        arduinoController = new ArduinoController(SerialPort.getCommPort("COM4"), this);
    }

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent normalParent = FXMLLoader.load(getClass().getResource("normalLayout.fxml"));

        primaryStage.setTitle("Test");
        primaryStage.setScene(new Scene(normalParent, primaryStage.getWidth(), primaryStage.getHeight()));
        primaryStage.show();
    }

    //  This Class is for Storing Information about a particular physical Fader
    public class Fader {

        public int[][] values;
//        The last 3 Falues the Fader has on the Different Faderpages.
//        The First Koordinate is for the Page, the second coordinate is for wich of the last 3 Values you whant to use.
//        The First Value of Every page is the Current value of the Fader
//        The Others are for detecting inconsistant Potivalues

        public int faderID;         //The number of the Physical Fader, starting from 0.

//      Uses local values, not OSC
//      Sets the Value of the Fader
//      Can send the new Value direcktly to the Arduino or Eos
        public void setValue(int physicalPage, int value, boolean sendToEos, boolean sendToArduino) {
            if (value != this.values[physicalPage][2] || (Math.max(values[physicalPage][1], value) - Math.min(values[physicalPage][1], value)) > 1) {
                values[physicalPage][0] = value;

                //Todo: Test this
                if (sendToEos) {

                    oscController.setFaderIntensity(physicalPage, faderID, value / 100.0D, true);
                }

                //Todo Test this
                if (sendToArduino) {
                    arduinoController.sendMessage(new ArduinoController.MessageToArduino(faderID, value), true);
                }
            }

            values[physicalPage][2] = values[physicalPage][1];
            values[physicalPage][1] = value;
        }

        Fader(int pages, int faderID) {
            values = new int[pages][3];
            this.faderID = faderID;
        }

    }
}

使用GUI代码的主类的Stacktrace

Exception in Application constructor
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class Main
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodException: Main.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getConstructor(Class.java:1825)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$7(LauncherImpl.java:818)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$3(WinApplication.java:177)
评论