package com.version2;

/**
 * @author xu
 * @create 2019-07-12 7:54
 */
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;

/**
 * @author xu
 * @create 2019-07-11 10:47
 */
public class Frame01 {

    private JFrame frame;
    private JPanel bigPanel;
    private JPanel smaPanel;

    //时刻改变的值
    private JLabel labelRgb;
    private JLabel labelHex;

    //清空和复制按钮
    private JButton clear;
    private JButton copy;

    //列表框
    private JScrollPane listScroll;
    private JList<String> list;
    private DefaultListModel<String> dlm;
    private String[] elements = new String[6];

    //鼠标第一次按下相对frame的位置
    private int firstX;
    private int firstY;

    //鼠标拖动窗体后相对(0,0)的位置,
    private int ZeroX;
    private int ZeroY;

    //是否能改变JLabel内容标记
    private boolean isOK = true;
    //获取颜色线程
    private ColorGet colorGet;

    Frame01(){
        frame = new JFrame();
        bigPanel = new JPanel();
        smaPanel = new JPanel();

        clear = new JButton("清空");
        copy = new JButton("复制");

        labelRgb = new JLabel("rgb");
        labelHex = new JLabel("hex");

        list = new JList<String>();
        listScroll = new JScrollPane(list);
        dlm = new DefaultListModel<String>();

        init();
        colorGet = new ColorGet();
        colorGet.start();
    }

    public void init(){
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(200, 200, 300, 200);
        frame.setUndecorated(true);

        bigPanel.setLayout(null);
        bigPanel.setBounds(0,0,300,200);

        smaPanel.setBounds(10,10,100,100);
        smaPanel.setBackground(new Color(255,0,0));

        labelRgb.setBounds(10,120,100,30);
        labelHex.setBounds(10,160,100,30);
        labelHex.setFont(new Font("微软雅黑",Font.BOLD,14));
        labelRgb.setFont(new Font("微软雅黑",Font.BOLD,14));

        labelRgb.setBackground(new Color(255,255,0));
        labelHex.setBackground(new Color(255,0,255));

        clear.setBounds(135,160,65,30);
        copy.setBounds(210,160,65,30);
        clear.setBackground(new Color(240,240,240));
        copy.setBackground(new Color(240,240,240));
        clear.setFont(new Font("微软雅黑",Font.BOLD,13));
        copy.setFont(new Font("微软雅黑",Font.BOLD,13));
        copy.setBackground(Color.pink);

        listScroll.setBounds(135,10,140,150);
        //设置列表宽高
        list.setFixedCellHeight(30);
        list.setFixedCellWidth(135);
        //设置选择模式
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setFont(new Font("微软雅黑",Font.BOLD,15));

        bigPanel.add(smaPanel);
        bigPanel.add(labelRgb);
        bigPanel.add(labelHex);
        bigPanel.add(listScroll);
        bigPanel.add(clear);
        bigPanel.add(copy);

        frame.add(bigPanel);

        frameEvent();
        listEvent();
        frame.setVisible(true);
    }

    /**
     * 事件处理
     */
    private void frameEvent(){
        //全局键盘监听事件alt
        Toolkit tk = Toolkit.getDefaultToolkit();
        tk.addAWTEventListener(new AllAwtEventListener(), AWTEvent.KEY_EVENT_MASK);

        //窗体frame
        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
                isOK = false;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                super.mouseExited(e);
                isOK = true;
            }

            @Override
            public void mousePressed(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    firstX = e.getX();
                    firstY = e.getY();
                }
            }
        });

        //拖动frame
        frame.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                super.mouseDragged(e);
                Point p = frame.getLocation();
                //获取此时鼠标的位置,然后通过鼠标的位置-相对位置=frame此时的位置
                ZeroX = p.x + e.getX();
                ZeroY = p.y + e.getY();
                frame.setLocation(ZeroX-firstX,ZeroY-firstY);
            }
        });

        //清空按钮
        clear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dlm.clear();
                list.setModel(dlm);
            }
        });
    }

    private void listEvent(){
        /**
         * 添加选项选中状态被改变的监听器
         *     有问题:按下(未松开手)时,从“未选中--选中”。。(释放时)又从“选中--未选中”
         *     单击一下,执行了3次,也就有3次状态改变
         *     暂未解决,只有双击才能解决(目前可用的解决方法,)
         * */
        list.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {

                //获取选中项索引
                int index = e.getFirstIndex();
                //获取hex值
                String strHex = dlm.getElementAt(index);
                System.out.println(strHex);
                //复制到系统剪切板
                SystemTextTool.setClipboardString(strHex);
                //去掉#
                String colorHex = strHex.substring(1);
                //从字符串16进制恢复到整型16进制数
                int hexInt = Integer.parseUnsignedInt(colorHex,16);
                smaPanel.setBackground(new Color(hexInt));

                labelRgb.setText(ColorToHex.toInt(colorHex));
                labelHex.setText("#"+colorHex);
                //设置选中条颜色
                list.setSelectionBackground(new Color(hexInt));
                //勿漏,刷新ListModel
                list.setModel(dlm);
                copy.setBackground(Color.green);
            }
        });

        list.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                copy.setBackground(Color.pink);
            }
            @Override
            public void mouseEntered(MouseEvent e){
                isOK = false;
            }
            @Override
            public void mouseClicked(MouseEvent e){
                if(e.getClickCount()==1){
                    System.out.println("单击一次:"+list.getSelectedValue());
                }

            }
        });
    }
    public static void main(String[] args) {
        new Frame01();
        //new Fang();
    }

    //全局监听类
    class AllAwtEventListener implements AWTEventListener {
        @Override
        public void eventDispatched(AWTEvent event){
            //先判断事件类型--键盘按下事件
            if(event.getID() == KeyEvent.KEY_PRESSED){
                KeyEvent keyEvent = (KeyEvent)event;
                //如果按下的是Alt+C
                if(keyEvent.isAltDown() && keyEvent.getKeyCode()==KeyEvent.VK_C){

                    System.out.println("你按到'全局Alt+c'键了");
                    String hex = labelHex.getText().trim();
                    SystemTextTool.setClipboardString(hex);

                    //添加到list内,并改变list背景色为所获取颜色
                    //放到第一行,所有元素后移,插入头部---错误方法

                    //添加,删除元素,都使用DefaultListModel--ListModel的子类来
                    dlm.add(0,hex);
                    //把第一行颜色换成获取到的颜色
                    list.setModel(dlm);
                }
            }
        }
    }

    //获取颜色的线程
    class ColorGet extends Thread{
        private Point mousePoint;
        private Robot robot;
        private Color color;
        private String rgb;
        private String hex;

        @Override
        public void run(){
            while (true){

                // 使用中断机制,来终止线程
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("Interrupted ...");
                    break;
                }
                try{
                    robot = new Robot();
                }catch (AWTException e){
                    e.printStackTrace();
                }
                mousePoint = MouseInfo.getPointerInfo().getLocation();
                /*由于打包成.exe文件后,屏幕与原来的屏幕小了1.5倍,所以在这里先乘1.5倍。然后鼠标位置就是
                当前真正的位置
                forWindows
                *
                color = robot.getPixelColor((int)(mousePoint.getX()*1.5),(int)(mousePoint.getY()*1.5));
*/
                color = robot.getPixelColor((int)(mousePoint.getX()*1),(int)(mousePoint.getY()*1));

                hex = ColorToHex.toHex(color.getRed(),color.getGreen(),color.getBlue());
                rgb = color.getRed()+","+color.getGreen()+","+
                        color.getBlue();

                //isOK代表暂停窗体内容标记
                if(isOK){
                    //给面板设置为鼠标位置的颜色
                    smaPanel.setBackground(color);
                    //改变标签颜色
                    labelHex.setText(hex);
                    labelRgb.setText(rgb);
                }
            }
        }
    }
}
最近下载更多
CxlyboSoft  LV6 2020年2月16日
moyec50  LV1 2019年7月30日
hsl2019  LV3 2019年7月25日
旧梦圆  LV1 2019年7月24日
Demons_Robin  LV7 2019年7月23日
谭鬼鬼  LV48 2019年7月21日
最代码官方  LV167 2019年7月15日
最近浏览更多
jkjfdgbkl  LV2 2023年11月2日
暂无贡献等级
猪皮怪 2022年6月12日
暂无贡献等级
wubinbin  LV11 2022年2月13日
shisun  LV3 2021年7月5日
大连有个马猴  LV4 2020年12月19日
jpfjpfjpf  LV1 2020年12月12日
段朝洪  LV15 2020年11月21日
jeed141305  LV1 2020年7月4日
Andy0211 2020年7月1日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友