简体   繁体   English

JLabel不会两次更改颜色

[英]JLabel wont change color twice

I have the following code: 我有以下代码:

   public class Test extends JFrame implements ActionListener{
 private static final Color TRANSP_WHITE = new Color(new Float(1), new Float(1), new Float(1), new Float(0.5)); 
 private static final Color TRANSP_RED = new Color(new Float(1), new Float(0), new Float(0), new Float(0.1));
 private static final Color[] COLORS = new Color[]{ TRANSP_RED, TRANSP_WHITE};
 private int index = 0;

 private JLabel label;
 private JButton button; 
 public Test(){
  super();

  setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
  label = new JLabel("hello world");
  label.setOpaque(true);
  label.setBackground(TRANSP_WHITE);

  getContentPane().add(label);

  button = new JButton("Click Me");
  button.addActionListener(this);

  getContentPane().add(button);

  pack();
  setVisible(true);
 }

 @Override
 public void actionPerformed(ActionEvent e) {
  if(e.getSource().equals(button)){
   label.setBackground(COLORS[index % (COLORS.length - 1)]);
index++;
      }
     }

 public static void main(String[] args) {
  new Test();
 }
    }

When I run it I get the label with the TRANSP_WHITE background and then when I click the button this color changes to TRANSP_RED but when I click it again I see no change in color. 当我运行它时,我得到带有TRANSP_WHITE背景的标签,然后当我单击按钮时,该颜色变为TRANSP_RED但是当我再次单击它时,我看到颜色没有变化。 Does anyone know why? 有人知道为什么吗?

Thanks 谢谢

Well, what were you expecting to happen? 好吧,您期望发生什么?

label.setBackground(COLORS[index % (COLORS.length - 1)]);

The index variable is hard coded to 0. and COLORS.length -1 is essentially a constant. index变量被硬编码为0。COLORS.length-1本质上是一个常数。 So every time you click your setting the background to COLORS[0]; 因此,每次单击将背景设置为COLORS [0]时;

If you change your action method to the following you'll get the results you are looking for: 如果将操作方法​​更改为以下内容,则会得到所需的结果:

 @Override
 public void actionPerformed(ActionEvent e) {
  if(e.getSource().equals(button)){
   label.setBackground(COLORS[index % COLORS.length]);
   index++;
  }
 }

First: The modulo operator will always return a value between 0 and one less than the value passed to it. 第一:模运算符将始终返回一个比传递给它的值小0到一个之间的值。 So 所以

index % COLORS.length

Will always return a value between 0 and COLORS.length -1. 将始终返回介于0和COLORS.length -1之间的值。

Second: You were forgetting to increment index after every call. 第二:您忘记了在每次调用后增加索引。

Hey! 嘿! You forgot to increment index. 您忘记增加索引。 In this expression: 在此表达式中:

label.setBackground(COLORS[index % (COLORS.length - 1)]);

index % (COLORS.length - 1) is always 0. index % (COLORS.length - 1)始终为0。

BTW. 顺便说一句。 you don't have to use new Float(1) when creating Color . 创建Color时不必使用new Float(1) 1F should work too. 1F应该工作。

这是您必须使用的代码

label.setBackground(COLORS[index % (COLORS.length)]);
index++;

You are doing it wrong. 你做错了。 It should be done like that 应该那样做

label = new JLabel("hello world"){
     public void paintComponent(Graphics g)
     {
         //draw background
         Color old=g.getColor();
         g.setColor(getBackground());
         g.fillRect(0,0,getWidth(),getHeight());
         g.setColor(old);
         super.paintComponent(g);
     }
};
label.setOpaque(false); // your component is not opaque!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM