javafx.fxml.LoadException:已指定控制器值

时间:2018-08-11 22:15:44

标签: javafx fxml

我已经查看了相关信息,而我仍然停留在这里-请问有什么帮助吗? 我正在尝试在Scenebuilder(IntelliJ)中显示折线图-但不断出现关于构造函数的不同错误。最后一个错误是 “ JavaFX错误:控制器已指定”,但之前已将其删除,但仍然出现错误。

,并且使用loader.setController(...)行在Main中为FXMLLoader尝试了不同的参数,但是没有运气。我一直在阅读有关将参数传递给构造函数的情况,有时需要工厂控制器的信息-所以以为可能是这样,但不知道如何编写一个。请提出任何建议吗?还是我可以阅读的好链接?

我最后一次从Main下面尝试FXMLLoaderCode ...

   FXMLLoader loader = new  FXMLLoader(getClass().getResource("/sample/view/graph.fxml"));
    loader.setController(new Controller(sortList));
    Parent root = loader.load();
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();

如果有人有任何有用的链接,那么我可以阅读有关工厂控制器的信息,这将非常有用。我的完整代码如下。

我的程序是 主要(Main.java) 程序的入口点,并使用FXML加载程序 模型(SortList.java) 将数字放在x和y(横坐标)组中 控制器(Controller.java) 将x,y添加到图形 查看(graph.fxml) 图形xml显示x-y折线图 ReadInData (ReadInData.java) 读入并解析数字

错误

javafx.fxml.LoadException: Controller value already specified.
/C:/Users/user/IdeaProjects/myjavafx/out/production/myjavafx/sample/view/graph.fxml:18

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:914)
    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.load(FXMLLoader.java:2409)
    at sample.Main.start(Main.java:32)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)

模型(SortList.java)

public class SortList {

    private final double abscissa;
    private final double ordinate;

    public SortList(double abscissa, double ordinate) {
        this.abscissa = abscissa;
        this.ordinate = ordinate;
    }

    public double getabscissa() {
        return abscissa;
    }

    public double getordinate() {
        return ordinate;
    }


}

查看(graph.java)

fxml

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

<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.chart.ScatterChart?>
<?import javafx.scene.layout.AnchorPane?>

<?import javafx.scene.control.Label?>
<AnchorPane id="AnchorPane"
            maxHeight="-Infinity"
            maxWidth="-Infinity"
            minHeight="-Infinity"
            minWidth="-Infinity"
            prefHeight="400.0"
            prefWidth="600.0"
            xmlns="http://javafx.com/javafx/8.0.121"
            xmlns:fx="http://javafx.com/fxml/1"
            fx:controller = "sample.controller.Controller">


    <children>
        <ScatterChart layoutX="76.0" layoutY="33.0" prefHeight="335.0" prefWidth="449.0" title="Test Graph">
            <xAxis>
                <CategoryAxis label="X axis" pickOnBounds="false" side="BOTTOM" />
            </xAxis>
            <yAxis>
                <NumberAxis label="Y axis" side="LEFT" upperBound="1000.0" />
            </yAxis>
        </ScatterChart>
    </children>

</AnchorPane>

控制器(Controller.java)

package sample.controller;

import javafx.fxml.Initializable;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;
import sample.model.SortList;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    public LineChart<Double, Double> lineChart;
    private final ArrayList<SortList> sortList;
    public LineChart label1;

    public Controller(ArrayList<SortList> sortList)


    {

        this.sortList = sortList;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        XYChart.Series<Double, Double> graphLine = new XYChart.Series<Double, Double>();

        for (SortList sortlist : sortList) {

            graphLine.getData().add(new XYChart.Data(sortlist.getabscissa(), sortlist.getordinate()));
        }
        lineChart.getData().add(graphLine);
    }
}

主要(Main.java)

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Callback;
import sample.IO.ReadInData;
import sample.controller.Controller;
import sample.model.SortList;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;


public class Main extends Application {

    @Override
    public void start(Stage stage) {
        stage.setTitle("Line Chart Sample");
        final ArrayList<SortList> sortList = ReadInData.ReadData("C:\\Users\\user\\Desktop\\Java problem\\test.csv");

        try {

            FXMLLoader loader = new FXMLLoader(getClass().getResource("/sample/view/graph.fxml"));
            loader.setController(new Controller(sortList));
            Parent root = loader.load();
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();

            loader.setControllerFactory(new Callback<Class<?>, Object>(){
                @Override
                public Object call(Class<?> param) {
                    return null;
                }

            });



        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

}
public class SortList {

    private final double abscissa;
    private final double ordinate;

    public SortList(double abscissa, double ordinate) {
        this.abscissa = abscissa;
        this.ordinate = ordinate;
    }

    public double getabscissa() {
        return abscissa;
    }

    public double getordinate() {
        return ordinate;
    }


}

输入/输出读入文件(ReadInData.java)

package sample.IO;

import sample.model.SortList;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

public class ReadInData {

    private static final String COMMA_DELIMITER = ",";

    static public ArrayList<SortList> ReadData(String filename) {

        BufferedReader br = null;

        ArrayList<SortList> inputList = new ArrayList<SortList>();

        try {

            br = new BufferedReader(new FileReader(filename));

            String line = "";

            br.readLine();

            while ((line = br.readLine()) != null) {

                String[] inputData = line.split(COMMA_DELIMITER);

                if (inputData.length > 0) {

                    try {
                        double abscissa = Double.parseDouble(inputData[0]);
                        double ordinate = Double.parseDouble(inputData[1]);

                        SortList row = new SortList(abscissa, ordinate);
                        inputList.add(row);

                    } catch (NumberFormatException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return inputList;
    }
}

1 个答案:

答案 0 :(得分:1)

使用Scene Builder打开FXML文件并选中 Scene Builder