信息发布→ 登录 注册 退出

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

发布时间:2026-01-11

点击量:

复制代码 代码如下:
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);
    }
  }
}

在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!