2012-11-06 197 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()); –

相關問題