我想写一个小程序,就是鼠标移动到屏幕任何一点,就显示该点的RGB值。
程序是写完了,但只能在程序的当前窗口中使用,移出去了就没用了。
其他界面代码就略了,请帮我看看下面的代码怎么改好。
private void formMouseMoved(java.awt.event.MouseEvent evt) 
        try {
            java.awt.Robot r=new java.awt.Robot();
            String s=r.getPixelColor((int)evt.getX(),(int)evt.getY()).toString();
            
            jLabel1.setText(s);
        } catch (AWTException ex) {
            ex.printStackTrace();
        }
    }

解决方案 »

  1.   

    我以前写的 你看看吧  初学者 哪里写的不对的还请指教 呵呵。import java.awt.*;import javax.swing.*;
    import java.awt.Rectangle;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BorderFactory;
    import java.awt.event.MouseMotionAdapter;public class Frame1 extends JFrame {
        JPanel contentPane;
        Robot bot=null;
        JTextField txtred = new JTextField();
        JTextField txtblue = new JTextField();
        JTextField txtgreen = new JTextField();
        JLabel jLabel1 = new JLabel();
        JLabel jLabel2 = new JLabel();
        JLabel jLabel3 = new JLabel();
        int screenwidth=0;
        public Frame1() {
            try {
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                jbInit();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }    /**
         * Component initialization.
         *
         * @throws java.lang.Exception
         */    private void jbInit() throws Exception {
            contentPane = (JPanel) getContentPane();
            contentPane.setLayout(null);
            this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
            this.setResizable(false);
            setSize(new Dimension(229, 114));
            setTitle("得到RGB值");
            screenwidth=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
            this.setLocation(0,0);        try {
                bot=new Robot();
            } catch (Exception ex) {
                System.out.println(ex);
            }
            txtred.setBorder(BorderFactory.createEtchedBorder());
            txtred.setBounds(new Rectangle(58, 8, 157, 18));
            txtblue.setBorder(BorderFactory.createEtchedBorder());
            txtblue.setBounds(new Rectangle(58, 36, 157, 18));
            txtgreen.setBorder(BorderFactory.createEtchedBorder());
            txtgreen.setBounds(new Rectangle(58, 66, 157, 18));
            jLabel1.setText("红");
            jLabel1.setBounds(new Rectangle(15, 1, 20, 30));
            jLabel2.setText("蓝");
            jLabel2.setBounds(new Rectangle(15, 29, 17, 31));
            jLabel3.setText("绿");
            jLabel3.setBounds(new Rectangle(15, 49, 14, 41));
            contentPane.addMouseMotionListener(new
                    Frame1_contentPane_mouseMotionAdapter(this));
            contentPane.add(jLabel3);
            contentPane.add(jLabel2);
            contentPane.add(txtred);
            contentPane.add(txtblue);
            contentPane.add(txtgreen);
            contentPane.add(jLabel1);
            this.setVisible(true);        new xiancheng(this);
            this.setAlwaysOnTop(true);    }    public static void main(String[] args)
        {
            Frame1 bb=new Frame1();    }
        class xiancheng extends Thread{        public xiancheng(Frame1 aa)
            {
                super("xiancheng");            this.start();
            }        public void run()
            {
                try {
                    while (true) {
                        int x = (int) MouseInfo.getPointerInfo().getLocation().getX();
                        int y = (int) MouseInfo.getPointerInfo().getLocation().getY();
                        Color color = bot.getPixelColor(x, y);
                        txtred.setText("" + color.getRed());
                        txtblue.setText("" + color.getBlue());
                        txtgreen.setText("" + color.getGreen());                    this.sleep(10);
                    }
                } catch (InterruptedException ex) {
                } catch (HeadlessException ex) {
                }
            }
        }    int i=0;
        public void contentPane_mouseMoved(MouseEvent e) {
            if (i%2==0)
            {
            this.setLocation(screenwidth-this.getWidth(),0);
            i++;
            }else
            {
                this.setLocation(0,0);
                i++;
            }
        }
    }
    class Frame1_contentPane_mouseMotionAdapter extends MouseMotionAdapter {
        private Frame1 adaptee;
        Frame1_contentPane_mouseMotionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }    public void mouseMoved(MouseEvent e) {
            adaptee.contentPane_mouseMoved(e);
        }
    }