我是 Java 新手,有 PHP、HTML、CSS 经验。当我尝试更改我的图表在窗口中占用的宽度和高度时,NetBeans 给了我错误:
错误:setWidth(double) 在 Region chart.setWidth(450) 中具有受保护的访问权限;
我搜索了 javafx 文档,发现宽度/高度绑定到区域,但我不确定我的代码中是什么,我尝试了一些东西但没有找到它......
我确定这很简单..
在此先感谢,布拉德。
package test;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.shape.Rectangle;
public class Test extends Application {
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setScene(new Scene(root,1000,1000));
root.getStylesheets().add("test/Chart.css");
Rectangle rect = new Rectangle(35,70);
rect.setLayoutX(30);
rect.setLayoutY(30);
rect.getStyleClass().add("my-rect");
NumberAxis xAxis = new NumberAxis("X Axis", -24d, 24.0d, 2.0d);
NumberAxis yAxis = new NumberAxis("Y Axis", -24.0d, 24.0d, 1.0d);
ObservableList<XYChart.Series> data = FXCollections.observableArrayList(
new ScatterChart.Series("Series 1", FXCollections.<ScatterChart.Data>observableArrayList(
new XYChart.Data(0.2, 3.5),
new XYChart.Data(0.7, 4.6),
new XYChart.Data(1.8, 1.7),
new XYChart.Data(2.1, 2.8),
new XYChart.Data(4.0, 2.2),
new XYChart.Data(4.1, 2.6),
new XYChart.Data(4.5, 2.0),
new XYChart.Data(6.0, 3.0),
new XYChart.Data(7.0, 2.0),
new XYChart.Data(7.8, 4.0)
)),
new ScatterChart.Series("Series 2", FXCollections.<ScatterChart.Data>observableArrayList(
new XYChart.Data(6.2,3.0),
new XYChart.Data(6.0,4.0),
new XYChart.Data(5.8,5.0)
))
);
ScatterChart chart = new ScatterChart(xAxis, yAxis, data);
chart.setWidth(450);
chart.setHeight(450);
chart.setLayoutX(250);
chart.setLayoutY(250);
root.getChildren().addAll(chart,rect);
}
@Override public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) { launch(args); }
}