0

我想在 JTextField 中以注释的形式向用户显示程序流。仅显示最后一条消息(“完成”)。我怎样才能让我打电话时出现每条消息setText()

private class CalculateButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {            
        String keyValue = keywordTF.getText();
        currentTF.setText("calling 1");
        methodCall1();
        currentTF.setText("calling 2");
        methodCall2();
        currentTF.setText("calling 3");
        methodCall3();
        currentTF.setText("complete");
    }
}
4

2 回答 2

2

原因是 EDT 没有时间重新绘制文本字段,因为您的methodCall*方法正在 EDT 上运行。

如果您想显示繁重任务的进度,您应该在工作线程上执行繁重的工作并更新 EDT 上的 UI。通常这是通过使用工作线程中的SwingWorker或使用来实现的。SwingUtilities#invokeLater

' Concurrency in Swing'教程包含更多信息

于 2012-05-15T15:48:30.700 回答
0

尝试:

String keyValue = keywordTF.getText();

currentTF.setText("calling 1");
methodCall1();
currentTF.setText(currentTF.getText()+"\ncalling 2");
methodCall2();
currentTF.setText(currentTF.getText()+"\ncalling 3");
methodCall3();
currentTF.setText(currentTF.getText()+"\ncomplete");

效率不是很高,但它会完成工作。

使用JTextArea. JTextField主要用于显示一行文本,但听起来您想保留更多日志...也许?如果您仍想使用 a JTextField,则将调用中的“\n”字符替换为setText()其他适当的分隔符。

于 2012-05-15T15:43:07.377 回答