2012-11-06 152 views
1

我是新来的Java与PHP,HTML,CSS的经验。当我尝试更改宽度和高度时,我的图表在窗口中占用的位置就是NetBeans给出的错误:调整JavaFX图的高度和宽度

错误:setWidth(double)在区域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); } 
} 

回答

2

ScatterChart.getHeight()(这又是Region.getHeight())的Javadoc说

Gets the value of the property height.
Property description:
The height of this resizable node. This property is set by the region's parent during layout and may not be set by the application. If an application needs to explicitly control the size of a region, it should override its preferred size range by setting the minHeight, prefHeight, and maxHeight properties.

即可以调整,并与约束任何图表的大小:

ScatterChart.setPrefHeight(double) 
ScatterChart.setMinHeight(double) 
ScatterChart.setMaxHeight(double) 

ScatterChart.setPrefWidth(double) 
ScatterChart.setMinWidth(double) 
ScatterChart.setMaxWidth(double) 

ScatterChart.setPrefSize(double, double) 
ScatterChart.setMinSize(double, double) 
ScatterChart.setMaxSize(double, double) 
+0

完全,工程谢谢! –

+0

有没有办法将图表的高度绑定到另一个图表,这样当图表A缩放时,图表B会自动缩放? – SDS

+0

@SDS,是尝试chartB.prefHeightProperty()。bind(chartA.heightProperty()); –

相关问题