复制代码代码如下:
Paket com.liuxing.test;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
öffentliche Klasse ColorChooser erweitert JFrame {
private JLabel sampleText = new JLabel("Label");
privater JButton ChooseButton = new JButton("Choose Color");
public static void main(String[] args) {
neuer ColorChooser();
}
public ColorChooser() {
this.setSize(300, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = neues JPanel();
sampleText.setBackground(null);
panel1.add(sampleText);
ChooseButton.addActionListener(new ButtonListener());
panel1.add(chooseButton);
this.add(panel1);
this.setVisible(true);
}
Die private Klasse ButtonListener implementiert ActionListener {
public void actionPerformed(ActionEvent e) {
Farbe c = JColorChooser.showDialog(null, "Wählen Sie eine Farbe", sampleText.getForeground());
if (c != null)
sampleText.setForeground(c);
}
}
}