【问题标题】:Constantly changing background color of JLabelJLabel不断变化的背景颜色
【发布时间】:2012-08-08 06:09:58
【问题描述】:

我是 GUI 编程的新手,我尝试了一些代码并希望不断更改名为“HeaderPanel”的 JPanel 的背景。

为什么这不能如我所愿? (颜色保持不变...)

private void changeColors() {
    int r = 0;
    int g = 155;
    int b = 12;

    while(true) {
        r = (r+1)%255;
        g = (g+1)%255;
        b = (b+1)%255;

        Color color = new Color(r,g,b);
        HeaderPanel.setBackground(color);
    }
}

【问题讨论】:

    标签: java swing colors background jlabel


    【解决方案1】:

    您需要告诉您的JPanel 自己绘制,可能是通过调用invalidate()

    【讨论】:

      【解决方案2】:

      您的 while 循环正在阻止重绘管理器更改颜色。

      你需要,以某种方式,在后台执行请求,像这样

      public class TestLabel extends JLabel {
      
          private Timer timer;
      
          private int r = 0;
          private int g = 155;
          private int b = 12;
      
          public TestLabel() {
      
              setText("Hello world");
              setOpaque(true);
      
              timer = new Timer(1000, new ActionListener() {
                  @Override
                  public void actionPerformed(ActionEvent e) {
      
                      System.out.println("tick");
      
                      r = (r + 1) % 255;
                      g = (g + 1) % 255;
                      b = (b + 1) % 255;
      
                      Color color = new Color(r, g, b);
                      setBackground(color);
      
                      System.out.println(color);
      
                      if (r == 0 && b == 0 && g == 0) {
      
                          r = 0;
                          g = 155;
                          b = 12;
      
                      }
      
                      invalidate();
                      revalidate();
                      repaint();
      
                  }
              });
      
              timer.setRepeats(true);
              timer.setCoalesce(true);
              timer.start();
      
          }
      }
      

      您可能想继续阅读

      使用扩展示例更新

      public class TestLabel extends JLabel {
      
          private Timer timer;
      
          private Object[][] colors = {{"Black", Color.BLACK},
              {"Blue", Color.BLUE},
              {"Cyan", Color.CYAN},
              {"Dark Gray", Color.DARK_GRAY},
              {"Gray", Color.GRAY},
              {"Green", Color.GREEN},
              {"Light Gary", Color.LIGHT_GRAY},
              {"Mangenta", Color.MAGENTA},
              {"Orange", Color.ORANGE},
              {"Pink", Color.PINK},
              {"Red", Color.RED},
              {"White", Color.WHITE},
              {"Yellow", Color.YELLOW}};
      
          public TestLabel() {
      
              setText("Hello world");
              setOpaque(true);
      
              timer = new Timer(1000, new ActionListener() {
                  @Override
                  public void actionPerformed(ActionEvent e) {
      
                      System.out.println("tick");
      
                      int index = (int) Math.round((colors.length - 1) * Math.random());
      
                      Object[] group = colors[index];
      
                      setBackground((Color)group[1]);
                      setText((String)group[0]);
      
                  }
              });
      
              timer.setInitialDelay(0);
              timer.setRepeats(true);
              timer.setCoalesce(true);
              timer.start();
      
          }
      }
      

      【讨论】:

      • 我很确定在 Java6 及更高版本中永远不需要 invalidate(); (re)validate, repaint() ...对于 EDT 上的 JLabel
      • @mKorbel 你可能是对的,我是老派(Java 1.3),颜色变化很微妙,我想确保它已更新:P
      • pssst 不要谈论 MouseMotionListener 需要(仅)repaint() 的任何内容,否则任何更改.... :-),丢失在 API 中,有人忘记添加此代码行: -)
      • +1 用于使用setOpaque(),这是许多程序员错过的唯一东西,同时将颜色设置为特定的JComponent,尽管正如@mKorbel 所建议的那样,这些东西真的不需要,不透明属性就够了:-)
      • -1 作为消除噪音的激励 ;-) 周围有足够多的糟糕示例代码随机且不必要地调用 x-validate/paint ...
      【解决方案3】:

      我不是 Swing 开发人员,但是当您更改颜色属性时,您是否不必“重新绘制”或类似的东西? 另一方面,您将需要另一个线程来不断更新颜色。

      总结

      • 更改面板背景颜色的代码(包括重绘)
      • 调用上述代码的线程。

      【讨论】:

      • 这也是我的第一个想法。
      • 不,更改基本属性时通常不需要重新绘制。如果有疑问,请不要假设而是阅读源代码 :-) 不,您当然不希望 EDT 以外的线程访问这些属性。
      • Kleopatra,根据Curzon的示例代码,他似乎需要一个后台任务。所以如果您需要根据时间或在另一个非用户事件中更改颜色,您肯定必须添加一个线程。
      猜你喜欢
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 2017-04-14
      • 2021-10-02
      • 2014-09-14
      • 2021-02-13
      相关资源
      最近更新 更多