簡體   English   中英

如何在Swing中使用Thread.sleep()和setBackground()創建閃光效果?

[英]How to use Thread.sleep() and setBackground() to create flash effect in Swing?

我想通過以下方法來制作閃光效果:將(JTextArea)的背景更改為RED->然后等待1秒鍾->返回WHITE。 我喜歡這樣:

JTextArea jTextArea = new JTextArea();
jTextArea.setBackGround(Color.RED);
Thread.currentThread().sleep(1000);
jTextArea.setBackGround(Color.WHITE)

但這是行不通的,我所擁有的只是白色背景,我看不到紅色背景。

我怎么了

謝謝!

您應該使用javax.swing.Timer而不是使用Thread.sleep(...)來凍結GUI。 此外,如@MinhCatVO所說,對GUI的任何更新都必須在EDT上完成。 有關該主題的更多信息,請參考Swing中的並發 請看下面的代碼,並詢問您無法掌握的內容。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ColouringTextArea
{
    private JTextArea tarea;
    private Timer timer;
    private Color[] colours = {
                                Color.RED,
                                Color.BLUE,
                                Color.GREEN.darker(),
                                Color.DARK_GRAY,
                                Color.MAGENTA,
                                Color.YELLOW
                              };
    private int counter = 0;                          
    private ActionListener timerAction = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            if (counter < colours.length)
            {
                tarea.setBackground(colours[counter]);
                counter++;
            }
            else
            {
                tarea.setBackground(Color.PINK);
                counter = 0;
            }   
        }
    };

    private void displayGUI()
    {
        JFrame frame = new JFrame("Colouring JTextArea");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        tarea = new JTextArea(10, 10);
        contentPane.add(tarea);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        timer = new Timer(1000, timerAction);
        timer.start();
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ColouringTextArea().displayGUI();
            }
        });
    }
}

因為您要休眠1秒鍾的線程不是GUI線程。 我認為Swing.Utilities具有SwingUtilities.invokeLater()方法。

public void class Flash extends Thread {
  JTextArea jtextArea = new JTextArera();
  public void run() {
   SwingUtilities.invokeLater(new Runnable()) {
      jTextArea.setBackground(Color.WHITE);
   }
  }
}

public void class Main {
   public void main() {
      ...
      Flash fl = new Flash();
      fl.start();
   }
}

由於線程問題,這不起作用。 您需要一個工作線程

這應該工作:

            SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {
                @Override
                protected Object doInBackground() throws Exception {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            jTextArea.setBackground(Color.RED);
                        }
                    });
                    Thread.sleep(1000);
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            jTextArea.setBackground(Color.WHITE);
                        }
                    });
                    return null;
                }
            };

            sw.execute();

請注意,必須從“事件調度線程”中調用Swing對象上的所有方法。 為此,請使用以下模式:

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            // Do gui things
        }
    });

了解事件調度程序線程。 在SO中有很多與此相關的帖子。 只是搜索單詞。

暫無
暫無

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

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