import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ExJColorChooser extends JFrame { /** Changer la couleur des textes ou objets avec JColorChooser */ JLabel monLabel; Color couleurCourante; public ExJColorChooser( ) { super("Utilisation de JColorChooser"); Container cp = getContentPane( ); JButton btn; cp.add(btn = new JButton("Changer la couleur..."), BorderLayout.NORTH); btn.setToolTipText("Cliquez ici pour le panneau des couleurs"); btn.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent actionEvent) { Color colorChoose = JColorChooser.showDialog( ExJColorChooser.this, // conteneur "Swing: monLabel JColorChooser", // titre de JColorChooser monLabel.getForeground( )); if (colorChoose != null) monLabel.setForeground(colorChoose); } }); cp.add(BorderLayout.CENTER, monLabel = new JLabel("I see your true color")); //La couleur s'applique à ce texte monLabel.setToolTipText("Voici la dernière couleur choisie"); pack( ); setDefaultCloseOperation(this.EXIT_ON_CLOSE); setLocationRelativeTo(null); // Afficher la fenêtre au milieu de l'écran } public static void main(String[] argv) { new ExJColorChooser( ).setVisible(true); } }