BufferedReader导致Java GUI挂起

时间:2016-12-07 17:05:39

标签: java swing bufferedreader

我目前正致力于一款名为osu的节奏游戏的附加功能!使用Java。涉及多个窗口,但在为其中一个窗口调用actionPerformed事件侦听器之后,它会创建另一个窗口,该窗口创建一个对象,其构造函数调用两个方法,每个方法都使用一个BufferedReader。但是,一旦我单击actionPerformed的按钮,程序就会挂起并冻结,直到它从任务管理器终止。以下是GUI窗口的actionPerformed代码:

private void btnCreateActionPerformed(ActionEvent evt) throws IOException {
    String text = textBeats.getText();
    if (text == null) {
        JOptionPane.showMessageDialog(contentPane, "Please enter a positive number.");
    }
    boolean isNumber = true;

    for (char c : text.toCharArray()) {
        if (!Character.isDigit(c)) {
            isNumber = false;
        } else if (c == '-') {
            isNumber = false;
        }
    }

    if (isNumber) {
        double beats = Double.parseDouble(text);
        WindowCode window = new WindowCode(drawArea, file, beats);
        window.setVisible(true);
        this.dispose();
    } else {
        JOptionPane.showMessageDialog(contentPane, "Please enter a positive number.");
    }

}

以下是创建WindowCode时调用的两个方法:

public double[] getLastTimingPoint() {
    String line;
    String timings[] = new String[8]; 
    double pointElements[] = new double[8];
    boolean isTiming = false;

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(osuFile), "UTF-8"))){
        while ((line = reader.readLine()) != null) {
            if (line.contains("[TimingPoints]")) {
                isTiming = true;
            } else if (line.contains("[Colours]") || line.contains("[HitObjects]")) {
                isTiming = false;
            }
            if (isTiming) {
                if (!line.contains("[TimingPoints]") && !line.contains("[Colours]") && !line.contains("[HitObjects]") && line.length() > 0) {
                    timings = line.split(",");
                }
            }
        }
        reader.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    for (int i = 0; i < timings.length; i++) {
        pointElements[i] = Double.parseDouble(timings[i]);
    }

    System.out.println("1");
    return pointElements;
}

public double[] getLastInheritedPoint() {
    String line;
    String timings[] = new String[8];
    double pointElements[] = new double[8];
    boolean isTiming = false;

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(osuFile), "UTF-8"))) {
        while ((line = reader.readLine()) != null) {
            if (line.contains("[TimingPoints]")) {
                isTiming = true;
            }
            while (isTiming) {
                if (!line.contains("[TimingPoints]") && !line.contains("[Colours]") && !line.contains("-")) {
                    timings = line.split(",");
                }
            }
        }
        reader.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    for (int i = 0; i < timings.length; i++) {
        pointElements[i] = Double.parseDouble(timings[i]);
    }

    System.out.println("2");
    return pointElements;
}

我试图打印出检查点号码,它只向控制台打印“1”,这让我相信这是造成这种情况的第二种方法。我的问题是,如果BufferedReader以某种方式影响EDT,如果它影响了,我应该如何解决它。

1 个答案:

答案 0 :(得分:1)

在第二种方法中你有这个内循环:

        while (isTiming) {
            if (!line.contains("[TimingPoints]") && !line.contains("[Colours]") && !line.contains("-")) {
                timings = line.split(",");
            }
        }

如果正在读取的文件包含此字符串“[TimingPoints]”,那么变量isTiming将被设置为true,并且没有其他人将其重置为false,被捕获到无限循环中。

你应该修改那个循环逻辑。