我已经看过这个的帖子了,我仍然被卡住了--有什么帮助吗?我试图在scenebuilder (IntelliJ)中显示一个折线图--但是不断收到关于constructors.The的不同错误。最后一个错误是'JavaFX error : Controller already‘,但是我之前删除了它,仍然得到一个错误。
并尝试了在Main中使用loader.setController(...)作为FXMLLoader的不同参数一条线,没有运气。我一直在读到,如果参数被传递给构造器,有时需要工厂控制器-所以我认为可能是这样,但不知道如何编写one.Any建议?或者有好的链接让我去读?
我最后一次尝试从下面的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();
如果任何人有任何有用的链接,以便我可以阅读有关工厂控制器,那将是非常好的。我的完整代码如下。
我的程序是程序的入口点,使用FXML加载器Model(SortList.java)将数字放在x和y(横坐标)组中Controller(Controller.java)将x,y添加到图形View(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 (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;
}
}
发布于 2018-08-12 06:33:25
使用场景构建器打开FXML文件并选中此选项
https://stackoverflow.com/questions/51804193
复制相似问题