java swing panel问题_Java Swing问题-使用调色板

我在这里遇到问题-选择颜色后(使用JColorChooser),十六进制值存储在文本字段中。我想做的是在十六进制值旁边的另一个文本框中显示颜色的 名称

,但是我不确定如何获得颜色名称?我包含了我的代码,也许有人可以给我一些有用的提示:

public class Main extends JComponent implements Accessible {

public ColorSelectionModel selectionModel;

public static final String SELECTION_MODEL_PROPERTY = "selectionModel";

public JColorChooser chooser;

public Color color;

public void process() {

JFrame frame;

JButton button;

final JTextField text1, text2;

// Initialize variables

chooser = new JColorChooser();

frame = new JFrame();

JPanel panel = new JPanel();

button = new JButton("Show color Palette");

text1 = new JTextField(20);

text2 = new JTextField(20);

// Setup UI

frame.add(panel);

panel.add(button);

panel.add(text1);

panel.add(text2);

panel.add(chooser)

chooser.setVisible(false);

button.setLocation(800, 600);

button.setActionCommand("");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

color = chooser.showDialog(chooser, "SHOW THE COLOR",

chooser.getColor());

{

if (color != null) {

String hex = Integer.toHexString(color.getRGB() & 0xffffff);

hex = "#" + hex;

text1.setText(hex);

}

}

}

});

frame.setVisible(true);

frame.setSize(1000, 800);

}

public static void main(String[] argv) {

// Run the code

Main m1 = new Main();

m1.process();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值