当前位置: 代码迷 >> J2SE >> 问一个如何监听全屏幕的有关问题
  详细解决方案

问一个如何监听全屏幕的有关问题

热度:60   发布时间:2016-04-24 14:34:26.0
问一个怎么监听全屏幕的问题
我想写一个小程序,就是鼠标移动到屏幕任何一点,就显示该点的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();
                }
        }

------解决方案--------------------
我以前写的 你看看吧 初学者 哪里写的不对的还请指教 呵呵。

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);
  相关解决方案