簡體   English   中英

JTextField不會使用Thread.sleep()更新

[英]JTextField Doesn't Update With Thread.sleep()

我試圖弄清楚為什么文本字段沒有更新。 我知道使用SwingWorker可能會解決此問題,但是我不明白為什么它首先不起作用。

public class waitExample {

private JFrame frame;
private JTextField txtLeadingText;
private String one = "update string 1";
private String two = "update string 2";
private String three = "update string 3";

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                waitExample window = new waitExample();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public waitExample() {
    initialize();
}

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    txtLeadingText = new JTextField();
    txtLeadingText.setHorizontalAlignment(SwingConstants.CENTER);
    txtLeadingText.setText("leading text");
    frame.getContentPane().add(txtLeadingText, BorderLayout.SOUTH);
    txtLeadingText.setColumns(10);

    JButton btnClickMeTo = new JButton("CLICK ME TO UPDATE TEXT");
    btnClickMeTo.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {

            try {

                updateOne();
                Thread.sleep(1000);
                updateTwo();
                Thread.sleep(1000);
                updateThree();
                Thread.sleep(1000);
                updateLast();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    frame.getContentPane().add(btnClickMeTo, BorderLayout.CENTER);
}

private void updateOne() {

    txtLeadingText.setText(one);
}

private void updateTwo() {

    txtLeadingText.setText(two);
}

private void updateThree() {

    txtLeadingText.setText(three);
}

private void updateLast() {

    txtLeadingText.setText("default text");
    }
}

據我了解,默認線程將阻止任何GUI更新。 沒關系,因為我是在Thread.sleep之前設置textField的。 為什么文本字段不更新? 不應該設置文本,然后線程等待嗎?

編輯:根據答案,上述代碼已更新。

您正在調用Thread.sleep(1000); 在EDT上。 這意味着當您的方法結束時-僅在隨后的某個時間點,repaint()才會觸發。

在此之前,您的GUI被凍結。

考慮到這是在一個線程上進行的(因此處理很簡單):

txtLeadingText.setText(one);
Thread.sleep(1000);
txtLeadingText.setText(two);
Thread.sleep(1000);
txtLeadingText.setText(three);
Thread.sleep(1000);
...
<returning from updateText()>
<processing other events on button click>
...
// some time later
<Swing finds out that GUI needs repaint: calls rapaint()>

這是您應該做的(我沒有編譯或測試過):

    public class MyRunnable implements Runnable {
        private List<String> strsToSet;
        public MyRunnable(List<String> strsToSet) {
            this.strsToSet = strsToSet;
        }
        @Override
        public void run() {
            try {
                if(strsToSet.size() > 0) {
                    final String str = strsToSet.get(0);
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            txtLeadingText.setText(str);
                        }
                    });

                    Thread.sleep(1000);
                    List<String> newList = new LinkedList<String>(strsToSet);
                    newList.remove(0);
                    new Thread(new MyRunnable(newList)).start();
                }
            }
            catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    new Thread(new MyRunnable(Arrays.asList(one, two, three))).start();

在Swing中很難做到這一點,但是相比之下,在動態語言(如Groovy)中,它就這么簡單(您將更好地了解正在發生的事情):

    edt { 
        textField.setText(one)
        doOutside { 
            Thread.sleep(1000); 
            edt {
                textField.setText(two)
                doOutside { 
                    Thread.sleep(1000); 
                    edt {
                        textField.setText(three)
                    } 
                }
            } 
        }
    }

GUI事件循環會更新屏幕,但是直到您返回,它才能更新屏幕。

我建議您避免在GUI事件線程中執行任何阻止操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM