[英]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
添加BarChart
或LineChart
。
你可以做
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.