【问题标题】:robot class mouseWheel not working机器人类mouseWheel不工作
【发布时间】:2013-03-30 19:05:35
【问题描述】:

一段时间以来,我一直在使用 Robot 类模拟鼠标事件,一切都很好,直到我尝试使用 mouseWheel 函数滚动。我只有这么简单的一行:

  Robot robot = new Robot();
  robot.mouseWheel(-100);

我已经尝试了很长时间的变体,程序运行,什么也不做,然后正常终止。有人可以解释这是为什么吗?

谢谢!

【问题讨论】:

  • 拥有一些其他代码会非常有用,例如,当鼠标滚轮旋转时应该执行的代码。
  • 你在测试什么?有焦点吗?
  • @jimmyLee 好吧,这与我现在尝试做的事情无关。我只是想上下滚动页面。这段代码应该足够滚动了吧?
  • @MadProgrammer 我在我的笔记本电脑上运行它,是的,它在运行的整个过程中都专注于它。
  • @user6561:你是在 IDE 上运行它吗?

标签: java scroll mouseevent awtrobot


【解决方案1】:

您的程序可能无法运行,因为在您的鼠标指针所在的 GUI 上没有 scroll up 。或者,您还没有将鼠标指针停留在可以看到滚动效果的适当 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();
        }
    });
  }
}

【讨论】:

  • 是的!这行得通:)我现在明白我的错误了。非常感谢你:):):)
【解决方案2】:

这对我来说很好......

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();
        }
    }

}

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

【讨论】:

  • 看,同样的问题。它打印出 ++ 和 --,但在两者之间,鼠标什么也没有发生! :'( 我正在编写一个用于远程桌面控制的应用程序,到目前为止,我有鼠标移动、点击、双击,与机器人类完美配合,但由于某种原因,mouseWHeel 根本什么都不做!你有什么替代方法吗?建议?
  • 除了确保鼠标悬停的窗口具有焦点之外,我想不出其他选择。当它在我的 IDE 和 Firefox 窗口上运行时,我让它工作了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-15
  • 1970-01-01
  • 2021-01-17
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
相关资源
最近更新 更多