java使用颜色选择器示例分享

时间:2021-05-19

复制代码 代码如下:
package 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;
public class ColorChooser extends JFrame {
private JLabel sampleText = new JLabel("Label");
private JButton chooseButton = new JButton("Choose Color");

public static void main(String[] args) {
new 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(panel1);
this.setVisible(true);
}

private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Color c = JColorChooser.showDialog(null, "Choose a Color", sampleText.getForeground());
if (c != null)
sampleText.setForeground(c);
}
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章