复制代码代码如下:
paket com.liuxing.test;
impor java.awt.Color;
impor java.awt.event.ActionEvent;
impor java.awt.event.ActionListener;
impor javax.swing.JButton;
impor javax.swing.JColorChooser;
impor javax.swing.JFrame;
impor javax.swing.JLabel;
impor javax.swing.JPanel;
ColorChooser kelas publik memperluas JFrame {
private JLabel sampleText = new JLabel("Label");
private JButton pilihButton = new JButton("Pilih Warna");
public static void main(String[] args) {
Pemilih Warna baru();
}
Pemilih Warna publik() {
ini.setUkuran(300, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = JPanel baru();
sampleText.setBackground(null);
panel1.add(sampleText);
pilihButton.addActionListener(ButtonListener baru());
panel1.add(pilihTombol);
this.add(panel1);
this.setVisible(benar);
}
kelas pribadi ButtonListener mengimplementasikan ActionListener {
public void actionPerformed(ActionEvent e) {
Warna c = JColorChooser.showDialog(null, "Pilih Warna", sampleText.getForeground());
jika (c != nol)
sampleText.setForeground(c);
}
}
}