簡體   English   中英

JavaFX圖表變量范圍

[英]JavaFX chart variable scope

final CategoryAxis yAxis = new CategoryAxis();
final NumberAxis zAxis = new NumberAxis(); 

if (cbTypeGraphView.equals("Bar Chart")) {
    BarChart<String, Number> chart = new BarChart<String, Number>(yAxis,xAxis);
}
if (cbTypeGraphView.equals("Line Chart")) {
    LineChart<String, Number> chart = new LineChart<String, Number>(yAxis,xAxis);
}

AnchorPane.setTopAnchor(chart, 110d);
AnchorPane.setLeftAnchor(chart, 10d);
AnchorPane.setRightAnchor(chart, 5d);
AnchorPane.setBottomAnchor(chart, 50d);

chart變量使范圍超出了if語句之外的范圍,我想知道如何解決此問題,以便chart不會失去超出if語句范圍的范圍。 我在考慮使用它的父類XYChart類。 我不確定如何向XYChart添加BarChartLineChart

你可以做

final CategoryAxis yAxis = new CategoryAxis();
final NumberAxis zAxis = new NumberAxis(); 

XYChart<String, Number> chart = null ;

if (cbTypeGraphView.equals("Bar Chart")) {
    chart = new BarChart<String, Number>(yAxis,xAxis);
}
if (cbTypeGraphView.equals("Line Chart")) {
    chart = new LineChart<String, Number>(yAxis,xAxis);
}

AnchorPane.setTopAnchor(chart, 110d);
AnchorPane.setLeftAnchor(chart, 10d);
AnchorPane.setRightAnchor(chart, 5d);
AnchorPane.setBottomAnchor(chart, 50d);

您可能應該處理都不執行if語句的情況,以避免出現空指針異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM