有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用线程。sleep()和setBackground()在Swing中创建闪光效果?

我想通过以下方式实现闪光效果: 将(JTextArea的)背景更改为红色-->;然后等待1秒-->;回到白色。 我喜欢这样:

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

但它不起作用,我只有白色的背景,我看不到红色的

我怎么了

谢谢


共 (3) 个答案

  1. # 1 楼答案

    由于线程问题,这不起作用。你需要一个Worker Thread

    这应该是有效的:

                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
            }
        });
    

    了解事件调度器线程。有这么多关于这个的帖子。只需搜索单词

  2. # 2 楼答案

    因为你想要睡眠1秒的线程不是GUI线程。我想是秋千。实用程序有一种方法来提高实用性。调用器()

    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();
       }
    }
    
  3. # 3 楼答案

    你应该使用javax.swing.Timer,而不是使用Thread.sleep(...),因为它可以冻结你的GUI。此外,任何对GUI的更新都必须在EDT上完成,正如@MinhCatVO所说的那样。有关该主题的更多信息,请参阅Concurrency in 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();
                }
            });
        }
    }