• P37 JColorChooser颜色选择器


    系统:Win10
    Java:1.8.0_333
    IDEA:2020.3.4
    Gitee:https://gitee.com/lijinjiang01/JavaSwing

    1.概述

    JColorChooser:颜色选取器。JColorChooser提供一个用于允许用户操作和选择颜色的控制器对话框。
    使用方法非常简单,就调用 JColorChooser 的一个静态方法便可:

    /**
     * 显示一个颜色选取器对话框(线程将被阻塞, 直到对话框被关闭)
     * 
     * 参数说明:
     *     component: 对话框的父组件, 对话框将紧靠 component 的中心显示; 如果传 null, 则对话框显示在屏幕中心。
     *     title: 对话框标题。
     *     initialColor: 初始选中的颜色; 如果传 null, 则默认为非透明白色。
     *
     * 返回:
     *     返回选择的颜色; 如果点击了取消或关闭, 则返回 null。
     */
    public static Color showDialog(Component component, String title, Color initialColor)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.代码实例

    import com.lijinjiang.beautyeye.BeautyEyeLNFHelper;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Demo01 {
        public static void main(String[] args) throws Exception {
            try {
                BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
                BeautyEyeLNFHelper.launchBeautyEyeLNF();
            } catch (Exception e) {
                e.printStackTrace();
            }
            final JFrame frame = new JFrame("Demo01");
            frame.setSize(300, 200);
    
            JPanel panel = new JPanel();
    
            // 创建一个标签, 用于显示选择的原色
            final JLabel label = new JLabel();
            label.setPreferredSize(new Dimension(150, 150));
            label.setOpaque(true);
            label.setBackground(Color.BLACK);
            panel.add(label);
    
            JButton btn = new JButton("选择颜色");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // 显示颜色选取器对话框, 返回选取的颜色(线程将被阻塞, 直到对话框被关闭)
                    Color color = JColorChooser.showDialog(frame, "选取颜色", null);
    
                    // 如果用户取消或关闭窗口, 则返回的 color 为 null
                    if (color == null) {
                        return;
                    }
    
                    // 把选取的颜色设置为标签的背景
                    label.setBackground(color);
    
                    // 获取颜色的 ARGB 各个分量值
                    int alpha = color.getAlpha();
                    int red = color.getRed();
                    int green = color.getGreen();
                    int blue = color.getBlue();
    
                    label.setText("A=" + String.format("%02x", alpha) + ", " +
                            String.format("#%02x%02x%02x", red, green, blue));
                }
            });
            panel.add(btn);
    
            frame.add(panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    3.效果演示

    在这里插入图片描述

  • 相关阅读:
    10:00面试,10:06就出来了,问的问题有点变态。。。
    线程安全问题及关键字synchronized,volatile
    翻译docker官方文档(残缺版)
    客户CRM能给企业带来哪些用处?
    .Net core web api 上传图片代码
    【老生谈算法】matlab实现图像阈值分割算法——图像阈值分割
    Linux命令(91)之mv
    【Godot4自学手册】第四十四节用着色器(shader)实现溶解效果
    el-form的resetFields()谷歌浏览器有效,在火狐浏览器失效不清空
    cocos鼠标选装
  • 原文地址:https://blog.csdn.net/qq_35132089/article/details/126900484