有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaFX中的java线条图

我试图在JavaFX中处理一个线形图,我有两个类:Main类和Controller类,Main类有Stage和Scene,Controller类有线形图的功能,在Contoller类中初始化线形图之后,在运行代码之后,折线图没有显示图表上的坐标,为什么会出现这种情况

以下是主要课程:

package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Line Chart");
    primaryStage.setScene(new Scene(root, 900, 500));
    primaryStage.show();
}

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

控制器类:

package sample;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

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

public class Controller implements Initializable {

@FXML
private ComboBox functionChooser;

@FXML
private TextField tfWidth,tfMin,tfMax;

@FXML
private RadioButton showFunction,hideFunction;

@FXML
private Label labelWidth,labelMin,labelMax;

@FXML
private LineChart lnChart;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {

    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();

    String funcs[] = {"y(x) = sin(X)","y(x) = cos(x) - 2 * sin(x)","y(x) = sin(x*x)"};
    functionChooser.getItems().setAll(funcs);
    functionChooser.getSelectionModel().selectFirst();

    Stage stage = new Stage();

    lnChart = new LineChart<Number,Number>(xAxis,yAxis);
    XYChart.Series series1 = new XYChart.Series();
    series1.setName("Portfolio 1");
    series1.getData().add(new XYChart.Data(0, 23));
    series1.getData().add(new XYChart.Data(1, 14));
    series1.getData().add(new XYChart.Data(2, 15));
    series1.getData().add(new XYChart.Data(3, 24));
    series1.getData().add(new XYChart.Data(4, 34));

    lnChart.getData().add(series1);

}
}

共 (1) 个答案

  1. # 1 楼答案

    从不将标记为@FXML的成员设置为new

    FXML注释的成员由FXMLLoader创建,并自动添加到加载程序创建的节点层次结构中

    你要做的是创建第二个折线图,将数据放入其中,并且从不将其添加到场景中

    相反,您要做的是将数据放在加载器已经创建的现有折线图中