简体   繁体   English

JTextField setText()方法不更新Field

[英]JTextField setText() method not updating Field

I am attempting to update the JTextField text via the setText() method. 我正在尝试通过setText()方法更新JTextField文本。 However, when I attempt to do this, nothing appears to happen. 但是,当我尝试执行此操作时,似乎没有任何反应。 I walked through with the debugger and determined this is indeed the case. 我遍历了调试器,并确定确实如此。 payField is the JTextField instance I am using. payField是我正在使用的JTextField实例。

Here is the code: 这是代码:

public void payBill(double payment) {

    if((this.bill - payment) > 0)
        payField.setText("Bill not completely paid! You still owe " + "$" + (this.bill - payment));
    else {
        payField.setText("Thank you for choosing Team Turbo!");//Not updating!
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        payFrame.setVisible(false);
        System.exit(0);
    }


}

Visually, nothing happens after this method is called: 在视觉上,调用此方法后没有任何反应:

在此处输入图片说明

Any idea what is going on here? 知道这里发生了什么吗? The entire GUI is ran inside a SwingUtilities.invokeLater thread, just for clarification. 整个GUI都在SwingUtilities.invokeLater线程中运行,仅作说明。

This... 这个...

try {
    Thread.sleep(1500);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
payFrame.setVisible(false);
System.exit(0);

is highly suspect. 高度怀疑。

Swing is a single threaded API. Swing是单线程API。 That is, any long running or blocking operation will prevent the UI from been updated. 也就是说,任何长时间运行或阻止的操作都将阻止UI的更新。

In your case, I might recommend a Swing Timer instead, which will trigger a callback after a specified delay, without blocking the UI. 在您的情况下,我可能建议使用Swing Timer ,它会在指定的延迟后触发回调,而不会阻塞UI。

Start by taking a look at Concurrency in Swing and How to Use Swing Timers for more details 首先了解Swing中的并发性如何使用Swing计时器,以了解更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM