簡體   English   中英

如何在java中添加計時器來更改JTextPane的文本?

[英]How to add a timer to change text of a JTextPane in java?

我正在研究一個java程序,我想要實現的是一旦窗口加載,JTextPane中的文本集就會改變。 我有以下代碼創建JTextPane;

JTextPane txtpnReady = new JTextPane();
        txtpnReady.setText("Ready");
        txtpnReady.setEditable(false);
        txtpnReady.setBounds(181, 39, 169, 20);
        contentPane.add(txtpnReady);

我不確定如何添加一個定時器來改變這個JTextPane的文本,2秒后,我需要它說准備一旦程序加載,然后2秒后穩定然后2秒后,Go。

謝謝。

您可能需要了解如何使用Swing Timers跟蹤。 你可以這樣做:

    Timer timer = new Timer(2000, new ActionListener() {

        private String nextText = "Steady";

        @Override
        public void actionPerformed(ActionEvent e) {
            txtpnReady.setText(nextText);
            if(!nextText.equals("Go")) {                    
                nextText = "Go";
            } else {
                ((Timer)e.getSource()).stop();
            }
        }
    });

    timer.setRepeats(true);
    timer.setDelay(2000);
    timer.start();

無關

對這個:

txtpnReady.setBounds(181, 39, 169, 20);

使用setBounds()不是一個好習慣,因為swing應用程序必須能夠在不同的平台和Look&feel上運行。 您應該讓布局管理器完成工作,避免使用setBounds()set(Preferred|Maximum|Minimum)Size

建議讀數:

暫無
暫無

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

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