【问题标题】:JavaFX chart variable scopeJavaFX 图表变量范围
【发布时间】:2016-01-05 23:27:09
【问题描述】:
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 类。我不知道如何将BarChartLineChart 添加到XYChart

【问题讨论】:

    标签: java javafx charts javafx-8


    【解决方案1】:

    你可以的

    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语句都不执行的情况,以避免空指针异常。

    【讨论】:

      猜你喜欢
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      • 2014-01-31
      • 2018-02-13
      相关资源
      最近更新 更多