Java linux如何移动鼠标光标

时间:2015-08-19 21:54:04

标签: java linux cursor mouse move

我正在制作一个3d多平台应用程序,我想将鼠标光标始终设置为中间。 robot.mouseMove(x,y)适用于Windows,但对于Linux,它确实不起作用。

我有一个问题,是否有一种方法可以让我将鼠标光标设置在Windows,Linux,Mac的特定位置。

*编辑
这就是我得到的,适用于Windows。

public class Frame extends JFrame implements KeyListener, MouseMotionListener, MouseListener{
    private static final long serialVersionUID = 4887525192006201710L;
    private Frame self = this;
    private JPanel buffer = new JPanel(){
        public void paintComponent(Graphics g){
            g.setColor(Color.BLACK);
            g.fillRect(0,0, getWidth(), getHeight());
        }
    };
    private Robot robot;

    public Frame(String title){
        super(title);
        add(buffer);
        setBounds(0,0, 640,480);
        setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        setUndecorated(true);       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

        try {
            robot = new Robot();
        } catch (AWTException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        addKeyListener(this);
        addMouseMotionListener(this);

        this.addComponentListener(new ComponentListener() {
            public void componentResized(ComponentEvent e){
                buffer.setBounds(0,0, getWidth(), getHeight());
            }

            public void componentHidden(ComponentEvent e){}
            public void componentMoved(ComponentEvent e){}
            public void componentShown(ComponentEvent e){}
        });
    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
            self.dispatchEvent(new WindowEvent(self, WindowEvent.WINDOW_CLOSING));
        }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("Mouse moved");
        robot.mouseMove(getX()+getWidth()/2, getY()+getHeight()/2);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}

1 个答案:

答案 0 :(得分:0)

* SOLUTION
好吧,机器人工作正常。问题是没有问题(抱歉)。我认为机器人不工作是因为VirtualBox没有将我的鼠标移动到虚拟Linux上的屏幕中间,但后来我发现它正在工作但是由于启用了鼠标集成我没有看到原版内部操作系统光标。

对于那些运行VirtualBox并希望禁用鼠标集成的人,请按快捷键hostkey(right ctrl) + I,光标将成为本机虚拟操作系统光标。