gpt4 book ai didi

java - 机器人类 mouseWheel 不工作

转载 作者:行者123 更新时间:2023-11-29 03:35:36 24 4
gpt4 key购买 nike

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

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

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

谢谢!

最佳答案

您的程序可能无法运行,因为在您的鼠标指针所在的 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();
}
});
}
}

关于java - 机器人类 mouseWheel 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15714609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com