-1

我正在创建一个 java 程序,它为大奖赛/松木德比之类的比赛创建阵容,然后一旦输入分数,就会找到决赛选手并为他们分配一个车道/热号。

到目前为止,程序中的所有内容都完全按照我的意愿进行,直到我到达我试图创建决赛选手的部分。在我单击按钮/按 Enter 输入最终分数后,我的 GUI 冻结。这是冻结之前的代码:

//allows user to enter the places for each car in each heat in each round
void enterScores() {
    if(indexCount == 0 || (indexCount) % numLanes == 0) {
        textArea.append("\nRound " + (roundCount + 1) + " Heat " + (heatCount+1) + ": ");
    }
    userResponse.setText("");
    prompt.setText(holderNameArray[roundCount][indexCount] + ": ");
    textArea.append("\n" + holderNameArray[roundCount][indexCount] + ": ");
    userResponse.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            holderScoreArray[roundCount][indexCount] = Integer.parseInt(userResponse.getText());
            textArea.append("" + holderScoreArray[roundCount][indexCount]);
            indexCount++;
            for(ActionListener act : enter.getActionListeners()) {
                enter.removeActionListener(act);
            }
            for(ActionListener act : userResponse.getActionListeners()) {
                            userResponse.removeActionListener(act);
                        }
            repeatEnterScores();
        }
    });
    enter.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            holderScoreArray[roundCount][indexCount] = Integer.parseInt(userResponse.getText());
            textArea.append("" + holderScoreArray[roundCount][indexCount]);
            indexCount++;
            for(ActionListener act : enter.getActionListeners()) {
                enter.removeActionListener(act);
            }
            for(ActionListener act : userResponse.getActionListeners()) {
                            userResponse.removeActionListener(act);
                        }
            repeatEnterScores();
        }
    });

}
//helps repeat enterScores() due to anon class restrictions
//checks to change the number of heats/rounds
void repeatEnterScores() {
    if((indexCount) % numLanes == 0 || indexCount == numCars) {
        heatCount++;
    }
    if(indexCount == numCars) {
        indexCount = 0;
        heatCount = 0;
        roundCount++;
    }
    if(roundCount < numRounds) {
        enterScores();
    } else {
        textArea.setText("You may now click the Scores tab to see the scores you have just entered.");
        scoresPanelSetup();
    }
}

它过去不会冻结并继续,而是会在以后的地方冻结。我是初学者,所以我不确定为什么现在这里会结冰。如果您需要更多代码/信息,请告诉我。每当我调试它时,我的所有代码都很好,只是 gui 有问题。

谢谢!

4

1 回答 1

1

如果您尝试在 EDT(事件调度线程)上执行长时间操作,GUI 可能会挂起。您可以通过使用 SwingWorker 来避免这种情况。SwingWorker 有两种方法,即doInBackground() 和done()。您应该在 doInBackground() 方法中进行计算并在 done() 方法中更新 UI。您可以从此处http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html了解有关 SwingWorker 的更多信息。

于 2013-09-15T23:59:04.830 回答