簡體   English   中英

為什么sleep方法會影響/不顯示面板上更新的JTextArea?

[英]Why is the sleep method affecting/not showing my updated JTextArea on the panel?

我有一個帶有各種面板的窗口。 在一個面板上,我有一個名為dogTalk的JTextArea,用於更新其文本。 在用戶單擊按鈕后,我希望文本在setText中添加我在下面提到的內容。

我使用了sleep方法,以便用戶可以閱讀我的更新文本,並且窗口可以在4秒鍾內自動關閉。 (我不希望用戶能夠在關閉時關閉窗口,因此我沒有使用Jframe.EXIT_ON_CLOSE,而是使用了JFrame.DO_NOTHING_ON_CLOSE,並借助sleep和system.ext(0)進行了自動關閉。 )

但是,我注意到sleep方法不允許dogTalk進行更新。 不過,它會打印出“我們正在工作”的信息,所以我想這是窗戶的問題嗎? 我知道睡眠是導致問題的原因,而不是代碼中的其他問題,因為當我注釋掉sleep和system.exit(0)並測試我的if語句是否正在執行時,我注意到JTextArea確實用我的語句進行了更新! 請你幫助我好嗎?

如果(e.getActionCommand()。equals(“ buybone”)){

        System.out.println("We're working");
        dogTalk.setText(dogTalk.getText() + "\nWow bone very wow much thanks bye.");
        try
        {  
            TimeUnit.SECONDS.sleep(4);
        }
        catch ( InterruptedException e1 )
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

       System.exit(0);

}

與其調用System.exit ,不如讓應用程序正常退出。 當沒有非守護進程線程仍然存在時,應用程序終止。 守護程序只是一個標志,用於確定如果該線程仍在運行,則JVM是否應終止。 如果非守護程序線程正在運行,JVM仍將終止。

話雖如此,問題在於您正在事件調度線程上調用sleep

EDT處理Swing和AWT組件的所有更新和渲染,並執行事件偵聽器中指定的行為(例如ActionListener#actionPerformed(ActionEvent) )。 如果您導致它阻塞(通過休眠或其他形式的阻塞),它將無法處理更新和渲染。 調用setText ,EDT需要能夠調整文本。 您可以通過強迫其進入睡眠狀態來防止這種情況發生。

怎么修

產生一個新線程,讓它等待4秒鍾,然后處置它:

Java 8+

public void actionPerformed(ActionEvent e) {
     dogTalk.setText(...);
     new Thread(() -> {
          TimeUnit.SECONDS.sleep(4);
          frame.dispose();
     }).start();
}

在Java 8之前

public void actionPerformed(ActionEvent e) {
    dogTalk.setText(...);
    new Thread(new Runnable() {
        public void run() {
            TimeUnit.SECONDS.sleep(4);
            frame.dispose();
        }
    }).start();
}

就在dogTalk.setText(dogTalk.getText() + "\\nWow bone very wow much thanks bye."); 輸入以下代碼:

dogTalk.revalidate();
dogTalk.repaint();

以下代碼運行良好,我使用了工作正常的Frame.update函數。 在您的情況下,您必須更新面板

           dogTalk.setText(dogTalk.getText() + "\nWow bone very wow much thanks bye.");
        frame.update(getGraphics());
        try
        {  
            TimeUnit.SECONDS.sleep(4);
        }
        catch ( InterruptedException e1 )
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.exit(0);

這是我的完整程序

public class TestSleep extends JFrame implements ActionListener{

JTextArea are=new JTextArea("asdjkfh");

JButton button=new JButton("Submit done");

public TestSleep()
{
    are.setBounds(20, 20, 30, 10);
    button.setBounds(10, 50, 20, 20);
    this.add(are);
    this.add(button);
    button.addActionListener(this);

}
public static void main(String[] args)
{
    TestSleep sleep=new TestSleep();
    sleep.setLayout(new GridLayout());
    sleep.setVisible(true);
    sleep.setBounds(10, 10, 500, 280);
}
@Override
public void actionPerformed(ActionEvent e)
{
        System.out.println("Working");
        are.setText(are.getText() + "\nWow bone very wow much thanks bye.");
        this.update(getGraphics());
        try
        {  
            TimeUnit.SECONDS.sleep(4);
        }
        catch ( InterruptedException e1 )
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.exit(0);

}   

}

暫無
暫無

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

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