复制代码代码如下 :
paquet com.liuxing.test ;
importer java.awt.Color ;
importer java.awt.event.ActionEvent ;
importer java.awt.event.ActionListener ;
importer javax.swing.JButton ;
importer javax.swing.JColorChooser ;
importer javax.swing.JFrame ;
importer javax.swing.JLabel ;
importer javax.swing.JPanel ;
la classe publique ColorChooser étend JFrame {
private JLabel sampleText = new JLabel("Étiquette");
private JButton ChooseButton = new JButton("Choisir la couleur");
public static void main (String[] arguments) {
nouveau ColorChooser();
}
public ColorChooser() {
this.setSize(300, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
sampleText.setBackground(null);
panel1.add(sampleText);
ChooseButton.addActionListener(new ButtonListener());
panel1.add(chooseButton);
this.add(panneau1);
this.setVisible(true);
}
classe privée ButtonListener implémente ActionListener {
public void actionPerformed(ActionEvent e) {
Couleur c = JColorChooser.showDialog(null, "Choisir une couleur", sampleText.getForeground());
si (c != nul)
sampleText.setForeground(c);
}
}
}