Java 机器人级鼠标滚轮不工作

Java 机器人级鼠标滚轮不工作,java,scroll,mouseevent,awtrobot,Java,Scroll,Mouseevent,Awtrobot,我用Robot类模拟鼠标事件已经有一段时间了,一切都很好,直到我尝试使用mouseWheel函数滚动。我有一句简单的话: Robot robot = new Robot(); robot.mouseWheel(-100); 我已经尝试了很长一段时间,程序运行,什么也不做,然后正常终止。有人能解释这是为什么吗 谢谢 这对我来说很好 import java.awt.AWTException; import java.awt.Robot; public class TestRobotScr

我用Robot类模拟鼠标事件已经有一段时间了,一切都很好,直到我尝试使用mouseWheel函数滚动。我有一句简单的话:

  Robot robot = new Robot();
  robot.mouseWheel(-100);
我已经尝试了很长一段时间,程序运行,什么也不做,然后正常终止。有人能解释这是为什么吗


谢谢

这对我来说很好

import java.awt.AWTException;
import java.awt.Robot;

public class TestRobotScroll {

    public static void main(String[] args) {
        try {
            Robot bot = new Robot();
            bot.setAutoDelay(100);
            Thread.sleep(2000);
            System.out.println("++");
            bot.mouseWheel(25);
            Thread.sleep(2000);
            System.out.println("--");
            bot.mouseWheel(-25);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

我让它在编辑器中滚动并在浏览器中滚动…

这对我来说很好

import java.awt.AWTException;
import java.awt.Robot;

public class TestRobotScroll {

    public static void main(String[] args) {
        try {
            Robot bot = new Robot();
            bot.setAutoDelay(100);
            Thread.sleep(2000);
            System.out.println("++");
            bot.mouseWheel(25);
            Thread.sleep(2000);
            System.out.println("--");
            bot.mouseWheel(-25);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

我让它在编辑器中滚动并在浏览器中滚动…

您的程序可能无法工作,因为您的鼠标指针所在的GUI上没有向上滚动的内容。或者,您还没有将鼠标指针放在可以看到滚动效果的相应GUI上。下面是实现这一点的简单程序。我希望这能对你有所帮助:

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;

public class MouseScrollRobot extends JFrame
{

    JTextArea ta;
    boolean scrolledAway = false;
    Robot robot;
    boolean started = false;
    public void createAndShowGUI() 
    {
        setTitle("Robot Demonstration");
        JPanel panel = new JPanel();
        ta = new JTextArea();   
        StringBuilder sBuilder = new StringBuilder();
        try
        {
            robot = new Robot();
            BufferedReader bfr = new BufferedReader(new FileReader("MouseScrollRobot.java"));
            String line = null ;
            while ((line = bfr.readLine()) !=null)
            {
                sBuilder.append(line+"\n");
            }
        }
        catch (Exception ex){ex.printStackTrace();}
        ta.setText(sBuilder.toString());
        JScrollPane jsp = new JScrollPane(ta);
        final Timer timer = new Timer(100, new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent evt)
            {
                try
                {
                    robot.mouseMove((getLocationOnScreen().x + getWidth()) / 2 , (getLocationOnScreen().y + getHeight()) / 2);//Move mouse pointer to the Component which you want to scroll
                    ta.requestFocus();
                    robot.setAutoDelay(100);
                    if (!scrolledAway)
                    {
                        setTitle("Scrolling up");
                        robot.mouseWheel(-40);
                    }
                    else
                    {
                        setTitle("Scrolling Down");
                        robot.mouseWheel(40);
                    }
                    scrolledAway = !scrolledAway;
                    setTitle("Scrolled");
                }catch (Exception ex){ex.printStackTrace();}
            }
        });
        timer.setRepeats(true);
        timer.start();
        getContentPane().add(jsp);
        setSize(500,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
  }

  public static void main(String args[]) 
  {
    SwingUtilities.invokeLater( new Runnable()
    {
        @Override
        public void run()
        {
            MouseScrollRobot msr = new MouseScrollRobot();
            msr.createAndShowGUI();
        }
    });
  }
}

您的程序可能无法运行,因为您的鼠标指针所在的GUI上没有向上滚动的内容。或者,您还没有将鼠标指针放在可以看到滚动效果的相应GUI上。下面是实现这一点的简单程序。我希望这能对你有所帮助:

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;

public class MouseScrollRobot extends JFrame
{

    JTextArea ta;
    boolean scrolledAway = false;
    Robot robot;
    boolean started = false;
    public void createAndShowGUI() 
    {
        setTitle("Robot Demonstration");
        JPanel panel = new JPanel();
        ta = new JTextArea();   
        StringBuilder sBuilder = new StringBuilder();
        try
        {
            robot = new Robot();
            BufferedReader bfr = new BufferedReader(new FileReader("MouseScrollRobot.java"));
            String line = null ;
            while ((line = bfr.readLine()) !=null)
            {
                sBuilder.append(line+"\n");
            }
        }
        catch (Exception ex){ex.printStackTrace();}
        ta.setText(sBuilder.toString());
        JScrollPane jsp = new JScrollPane(ta);
        final Timer timer = new Timer(100, new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent evt)
            {
                try
                {
                    robot.mouseMove((getLocationOnScreen().x + getWidth()) / 2 , (getLocationOnScreen().y + getHeight()) / 2);//Move mouse pointer to the Component which you want to scroll
                    ta.requestFocus();
                    robot.setAutoDelay(100);
                    if (!scrolledAway)
                    {
                        setTitle("Scrolling up");
                        robot.mouseWheel(-40);
                    }
                    else
                    {
                        setTitle("Scrolling Down");
                        robot.mouseWheel(40);
                    }
                    scrolledAway = !scrolledAway;
                    setTitle("Scrolled");
                }catch (Exception ex){ex.printStackTrace();}
            }
        });
        timer.setRepeats(true);
        timer.start();
        getContentPane().add(jsp);
        setSize(500,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
  }

  public static void main(String args[]) 
  {
    SwingUtilities.invokeLater( new Runnable()
    {
        @Override
        public void run()
        {
            MouseScrollRobot msr = new MouseScrollRobot();
            msr.createAndShowGUI();
        }
    });
  }
}

有一些其他的代码是非常有用的,例如,鼠标滚轮旋转时应该执行的代码。你在测试什么?它有焦点吗?@jimmyLee嗯,这和我现在想做的事情无关。我只是想在页面上下滚动。这段代码应该足够滚动了,对吗?@MadProgrammer我在笔记本电脑上运行这段代码,是的,它在运行的整个过程中都有焦点。@user6561:您是在IDE上运行它吗?如果有一些其他代码,例如,鼠标滚轮旋转时应该执行的代码。您在测试什么?它有焦点吗?@jimmyLee嗯,这和我现在想做的事情无关。我只是想在页面上下滚动。这段代码应该足够滚动了,对吗?@MadProgrammer我在我的笔记本电脑上运行它,是的,它在运行的整个过程中都有焦点。@user6561:你是在IDE上运行它吗?看,同样的问题。它打印出++和-,但在这两者之间,鼠标没有任何变化!:'我正在编写一个用于远程桌面控制的应用程序,到目前为止,我有鼠标移动、轻敲、双击,与robot类完美配合,但出于某种原因,mouseWHeel根本不起任何作用!你有什么可以建议的替代方案吗?我想不出其他替代方案,然后确保鼠标所在的窗口具有焦点。我在IDE和Firefox windowsee上运行时也遇到了同样的问题。它打印出++和-,但在这两者之间,鼠标没有任何变化!:'我正在编写一个用于远程桌面控制的应用程序,到目前为止,我有鼠标移动、轻敲、双击,与robot类完美配合,但出于某种原因,mouseWHeel根本不起任何作用!你有什么可以建议的替代方案吗?我想不出其他替代方案,然后确保鼠标所在的窗口具有焦点。当它在我的IDE和Firefox窗口上运行时,我让它工作了是的!我现在明白我的错误了。非常感谢:::是的!我现在明白我的错误了。非常感谢:::