因此,我正在尝试编写一个GUI,在linechart上显示实时数据。到目前为止,我可以让linechart工作,但不能进入GUI。
使用scenebuilder,我用linechart对象创建了一个视图,以便将其链接到生成的图表。但是,由于某些原因,这似乎不适用于我的mainApp中的这段代码。
public void showSes() {
try {
// Load the fxml file and set into the center of the main layout
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/Session.fxml"));
AnchorPane Session = (AnchorPane) loader.load();
rootLayout.setCenter(Session);
SessionController controller = loader.getController();
controller.setMainApp(this);
controller.initGraph();
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
}这只是用空行图显示视图。但是我知道图表应该知道,因为我可以用它来创建一个场景,并将它显示到GUI中,但是我在scenebuilder中创建的视图还有一些我想要显示的其他字段.
有谁有主意吗?
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.chart.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="900.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.view.SessionController">
<children>
<Pane prefHeight="900.0" prefWidth="1280.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<LineChart fx:id="linechart" layoutX="29.0" layoutY="194.0" prefHeight="416.0" prefWidth="1222.0" title="Temperature of session">
<xAxis>
<CategoryAxis label="Time (s)" fx:id="xAxis" />
</xAxis>
<yAxis>
<NumberAxis fx:id="yAxis" label="Temperature (°C)" side="LEFT" upperBound="160.0" />
</yAxis>
</LineChart>
<GridPane layoutX="254.0" layoutY="87.0" prefHeight="150.0" prefWidth="771.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="274.0" minWidth="10.0" prefWidth="274.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="273.0" minWidth="10.0" prefWidth="273.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="273.0" minWidth="10.0" prefWidth="273.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label prefHeight="53.0" prefWidth="153.0" text="Temperature fluid:" GridPane.halignment="CENTER" GridPane.valignment="TOP">
<font>
<Font size="16.0" />
</font>
</Label>
<Label prefHeight="52.0" prefWidth="181.0" text="Temperature vapor:" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="TOP">
<font>
<Font size="16.0" />
</font>
</Label>
<TextField fx:id="fluidT" editable="false" />
<TextField fx:id="gasT" editable="false" GridPane.columnIndex="2" />
</children>
</GridPane>
<Label layoutX="474.0" layoutY="14.0" text="TempTracker">
<font>
<Font size="50.0" />
</font>
</Label>
<TextArea editable="false" layoutX="190.0" layoutY="638.0" prefHeight="160.0" prefWidth="900.0" promptText="											------Warning log------- " wrapText="true" />
<Button layoutX="540.0" layoutY="808.0" mnemonicParsing="false" onAction="#handleStop" prefHeight="65.0" prefWidth="200.0" text="STOP">
<font>
<Font size="22.0" />
</font>
</Button>
</children></Pane>
</children>
</AnchorPane>控制器
private static final int MAX_DATA_POINTS = 50;
private String xSeriesData = "";
private XYChart.Series series1;
private XYChart.Series series2;
private ExecutorService executor;
private BlockingQueue<Number> dataQ1 = new ArrayBlockingQueue<Number>(1024);
private BlockingQueue<Number> dataQ2 = new ArrayBlockingQueue<Number>(1024);
@FXML
private CategoryAxis xAxis = new CategoryAxis();
@FXML
final NumberAxis yAxis = new NumberAxis();
@FXML
final LineChart<String, Number> linechart = new LineChart<String, Number>(xAxis, yAxis);
public void initGraph(){
xAxis.setAutoRanging(false);
xAxis.setTickLabelsVisible(false);
xAxis.setTickMarkVisible(false);
NumberAxis yAxis = new NumberAxis();
yAxis.setAutoRanging(true);
//Graph
final LineChart<String, Number> lc = new LineChart<String, Number>(xAxis, yAxis){
@Override
protected void dataItemAdded(Series<String, Number> series, int itemIndex, Data<String, Number> item){}
};
lc.setAnimated(false);
lc.setId("liveLineChart");
lc.setTitle("Animated Line Chart");
//Graph Series
series1 = new XYChart.Series<Number, Number>();
series2 = new XYChart.Series<Number, Number>();
linechart.getData().addAll(series1, series2);
series1.setName("T1");
series2.setName("T2");
fluidT.setText("0000");
gasT.setText("0000");
prepareTimeline();
Runnable con = new Consumer(this);
Thread c = new Thread(con);
c.start();
}发布于 2015-03-26 20:28:46
不要为@FXML注入成员创建新对象
永远不要将new与@FXML结合使用,即永远不要写:
@FXML
private CategoryAxis xAxis = new CategoryAxis();相反,只需写:
@FXML
private CategoryAxis xAxis;FXMLLoader将自动为FXML文件中的每个元素生成一个新对象,并将对该对象的引用注入到控制器中,在控制器中提供一个@FXML注释。因此,如果将@FXML成员引用重置为在控制器中创建的新对象,则该对象与加载程序创建的对象之间将不存在关联。
此外,不要在您的LineChart ()函数中创建另一个新的initGraph。您已经有了由LineChart创建的FXMLLoader,只需引用它。对于使用@FXML注入的NumberAxis和其他元素也是如此。
如果使用@FXML注释,也可以使用<fx:id>
你有:
@FXML
private CategoryAxis xAxis;因此,在fxml中,您应该定义:
<xAxis fx:id="xAxis">否则,FXMLLoader将无法注入对您在FXML中定义的轴的引用。
撇开
您的代码中可能有其他错误(例如,围绕并发和线程)。所以以上可能不是你所有的错误。通常,在创建mcve时,尝试消除与当前问题无关的任何内容(例如,FXML的线程代码和非线性部分),但包括其他人可以用来复制和粘贴代码以编译和运行代码以复制问题的所有内容。
注意:合奏示例应用程序包含一个基于音频频谱输入数据实时更新图形的示例程序。
https://stackoverflow.com/questions/29286698
复制相似问题