首页 > 解决方案 > Netbeans IDE 在创建新的 ActionTimer、ChangeListener 后“停止工作”

问题描述

@Override
public void start(Stage stage) {
    // The class Random is used to randomize the dice rolls
    Random random = new Random();

    NumberAxis xAxis = new NumberAxis();
    // y-axes represents the average of the rolls. The average is always between [1-6]
    NumberAxis yAxis = new NumberAxis(1, 6, 1);

    LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
    // removing elements of the chart, e.g. circles on points
    lineChart.setLegendVisible(false);
    lineChart.setAnimated(false);
    lineChart.setCreateSymbols(false);

    // we create a variable representing the data and add it to the chart
    XYChart.Series average = new XYChart.Series();
    lineChart.getData().add(average);

    new AnimationTimer() {
        private long previous;
        private long sum;
        private long count;

        @Override
        public void handle(long current) {
            if (current - previous < 100_000_000L) {
                return;
            }

            previous = current;

            // roll the dice
            int number = random.nextInt(6) + 1;

            // we grow the sum and increment the count
            sum += number;
            count++;

            // we add a new data point to the chart
            average.getData().add(new XYChart.Data(count, 1.0 * sum / count));
        }
    }.start();

    Scene scene = new Scene(lineChart, 400, 300);
    stage.setScene(scene);
    stage.show();
}

我正在学习 mooc.fi Java 课程,现在在第 14 部分,并且有一个我想尝试的示例代码。这是代码有问题的部分。当我将它粘贴到main 方法上方的DiceRolling类(即 extends )中时,IDE 将失去其所有功能。Application这意味着Ctrl+Space在代码中无处显示任何建议,如果我用鼠标选择部分代码我无法Ctrl+X它,停止检查代码是否有错误(谈论红色下划线),它只是整体停止检查代码。我可以将原因缩小到new ActionTimer代码的一部分。只有第一个大括号被写入之后才会出现问题。

我之前遇到过这个问题,当时我ChangeListener在 lambda 表达式中创建了一个新的,第一个大括号也导致了这个问题。我不知道它为什么会发生,或者它是否有意义或者它只是一个错误。

标签: javanetbeans

解决方案


推荐阅读