Le code J'utilise est:fond changeant sur les déplacements JLabel composants
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)]);
index++;
}
}
public static void main(String[] args) {
new Test();
}
}
Lorsque je clique sur le bouton pour changer les labales colorent l'interface graphique ressemble à ceci:
Avant: alt text http://www.freeimagehosting.net/uploads/67d741157b.png Après: alt text http://www.freeimagehosting.net/uploads/5cc86874fa.png
Des idées pourquoi?
pourriez-vous fournir un exemple de la méthode paintComponent? – Aly