JLabel wont change color twice
Posted
by Aly
on Stack Overflow
See other posts from Stack Overflow
or by Aly
Published on 2010-03-21T13:21:38Z
Indexed on
2010/03/21
13:31 UTC
Read the original article
Hit count: 207
Hi,
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)]); } }
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. Does anyone know why?
Thanks
© Stack Overflow or respective owner