兄弟问个问题,
如何用 java 监听 windows 事件?例如侦听鼠标事件!

解决方案 »

  1.   

    看看<java核心技术>卷一 第八章
    下面是其中一个例子:import java.awt.*;public class MouseTest { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    MouseFrame frame = new MouseFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }}class MouseFrame extends JFrame {
    public MouseFrame() {
    setTitle("MouseTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    MousePanel panel = new MousePanel();
    add(panel);
    }
    public static final int DEFAULT_WIDTH = 300;
    public static final int DEFAULT_HEIGHT = 200;
    }class MousePanel extends JPanel {
    public MousePanel() {
    squares = new ArrayList<Rectangle2D>();
    current = null;
    addMouseListener(new MouseHandler());
    addMouseMotionListener(new MouseMotionHandler());
    }

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    for (Rectangle2D r : squares) {
    g2.draw(r);
    }
    }

    public Rectangle2D find(Point2D p) {
    for(Rectangle2D r : squares) {
    if(r.contains(p)) return r;
    }
    return null;
    }

    public void add(Point2D p) {
    double x = p.getX();
    double y = p.getY();

    current  = new Rectangle2D.Double(
    x - SIDELENGTH/2,
    y - SIDELENGTH/2,
    SIDELENGTH,
    SIDELENGTH);
    squares.add(current);
    repaint();
    }

    public void remove(Rectangle2D s) {
    if(s == null) return;
    if(s == current) current = null;
    squares.remove(s);
    repaint();
    }

    private static final int SIDELENGTH = 10;
    private ArrayList<Rectangle2D> squares;
    private Rectangle2D current;

    private class MouseHandler extends MouseAdapter {
    public void mousePressed(MouseEvent event) {
    current = find(event.getPoint());
    if(current == null) add(event.getPoint());
    }
    public void mouseClicked(MouseEvent event) {
    current = find(event.getPoint());
    if(current != null && event.getClickCount() >= 2) remove(current);
    }

    }

    private class MouseMotionHandler implements MouseMotionListener {
    public void mouseMoved(MouseEvent event) {
    if(find(event.getPoint()) == null)
    setCursor(Cursor.getDefaultCursor());
    else 
    setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    }

    public void mouseDragged(MouseEvent event) {
    if(current != null) {
    int x = event.getX();
    int y = event.getY();
    current.setFrame(
    x - SIDELENGTH/2,
    y - SIDELENGTH/2, 
    SIDELENGTH, 
    SIDELENGTH);
    repaint();
    }
    }
    }
    }
      

  2.   

    楼上,我刚试了试,java只能在自己的panel 处理事件,能不能跨步到其它的软件,比如 windows自带的记事本等,敬请各路豪侠相助!
      

  3.   

    用jin吧,java就是跨平台的,系统监听是没办法做的,做了就没办法跨平台了,依赖系统函数了.
      

  4.   

    目前只能用jni调用C++之类写的dll实现
      

  5.   

    java<-tcp/udp<-c/c++/dll/exe<-windows
      

  6.   

    楼上,我刚试了试,java只能在自己的panel 处理事件,能不能跨步到其它的软件,比如 windows自带的记事本等,敬请各路豪侠相助!
     这是JNI实现了 JDK1.6已经实现了 可以看一下 新出的几个类 其实复制 也就是复制到WIN的 NOTEPAD