java - JavaFXML + Scenebuilder:LineChart 未显示值
问题描述
我有一个带有一些MenuItem
s 的 RootStage 并且想将我的 Rootstage 的中心设置为AnchorPane
aLineChart
在中间。
图表CategoryAxis
在 x 轴上有 a NumberAxis
,在 y 轴上有 a。
Scene
但是现在如果我用它调用控制器LineChart
,我可以看到LineChart
但它没有值。或者至少不显示它们。
为什么LineChart
没有被填满?
或者为什么它不显示值?
MainController.class
public class MainController extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
@FXML
private MenuBar menuBar;
@FXML
private Menu menuAnsicht, menuOptionen;
@FXML
private MenuItem menuItemTag, menuItemWoche, menuItemAbout, menuItemCloseWindow;
@Override
public void init() {
}
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
initRootLayout();
}
private void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/de/mgo/temperaturstatistics/view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
logger.debug("ERROR: " + e.getMessage());
e.printStackTrace();
}
}
@FXML
public void handleTagItem(ActionEvent event) {
try {
BorderPane nowLayout = (BorderPane) menuBar.getScene().getRoot();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/de/mgo/temperaturstatistics/view/DailyScene.fxml"));
nowLayout.setCenter(loader.load());
} catch (Exception e) {
logger.debug("ERROR: " + e.getMessage());
e.printStackTrace();
}
}
DailySceneController.class
public class DailySceneController extends Application {
@FXML
private Label titleDaily;
@FXML
private LineChart<String, Number> dailyChart;
@FXML
private CategoryAxis xDaily;
@FXML
private NumberAxis yDaily;
public int count = 0;
public DailySceneController() {
count++;
System.out.println(count);
}
@Override
public void init() {
this.setChartDaily();
}
@Override
public void start(Stage primaryStage) {
}
public void setChartDaily() {
LocalDate localDate = LocalDate.now().minusDays(0);
List<Temperaturen> listeNodemcu_JHL = MainController.getListeNodemcu_JHL();
this.xDaily.setAutoRanging(false);
this.yDaily.setAutoRanging(false);
this.dailyChart.setAnimated(false);
XYChart.Series<String, Number> seriesJHL = new XYChart.Series<String, Number>();
seriesJHL.setName("Nodemcu_JHL");
seriesJHL.getData().add(new XYChart.Data<String, Number>("Test", 15));
this.dailyChart.getData().add(seriesJHL);
}
}
DailyScene.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?package de.mgo.temperaturstatistics.controller.Controller?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>
<BorderPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.mgo.temperaturstatistics.view.DailySceneController">
<top>
<Label fx:id="titleDaily" text="Temperatur-Statistik Tagesansicht" BorderPane.alignment="CENTER">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Label>
</top>
<center>
<LineChart fx:id="dailyChart" prefHeight="300.0" prefWidth="500.0" BorderPane.alignment="CENTER">
<xAxis>
<CategoryAxis fx:id="xDaily" animated="false" label="Uhrzeit" side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis fx:id="yDaily" animated="false" autoRanging="false" label="Temperatur" lowerBound="10.0" side="LEFT" tickLabelGap="1.0" tickUnit="1.0" upperBound="30.0" />
</yAxis>
</LineChart>
</center>
</BorderPane>
解决方案
您正在Application
使用您的控制器类进行扩展,期望它的生命周期方法被调用。这是不好的做法,也不会导致生命周期方法的调用。
如果要调用方法,请使用Initializable.initialize
或仅使用无参数initialize
方法:
@FXML
private void initialize() {
this.setChartDaily();
}
推荐阅读
- python - 为每个字典键值提供键
- javascript - textarea中的多个单独区域选择?
- android - 在 NativeScript 中推送本地通知。如何让它们全屏显示?
- java - 将 zip 文件转换为 gzip 并写入 hdfs
- c# - 从 OneDrive API 获取登录成功后如何获取登录用户信息
- r - 在R中的循环内创建新的数据框列到全局环境中
- c - 使用 cwebpage 在 Windows 窗口中嵌入 Web 浏览器
- node.js - 为什么我不能在 Mongoose Schema 键的开头使用 $ 字符
- azure - 将 Azure AD 用户扩展添加到 id 令牌声明时出现问题
- typescript - 打字稿:类返回空对象