我一直在模擬鼠標事件與Robot類一段時間,一切都很好,直到我試圖使用mouseWheel函數滾動。我只是有這個簡單的一行:機器人類mouseWheel不工作
Robot robot = new Robot();
robot.mouseWheel(-100);
我一直在嘗試了很久的這個變化,並在程序運行時,什麼都不做,然後正常終止。有人可以闡明這是爲什麼嗎?
謝謝!
我一直在模擬鼠標事件與Robot類一段時間,一切都很好,直到我試圖使用mouseWheel函數滾動。我只是有這個簡單的一行:機器人類mouseWheel不工作
Robot robot = new Robot();
robot.mouseWheel(-100);
我一直在嘗試了很久的這個變化,並在程序運行時,什麼都不做,然後正常終止。有人可以闡明這是爲什麼嗎?
謝謝!
程序可能無法正常工作,因爲無論有什麼好scroll up
在您的鼠標指針休息的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();
}
});
}
}
yes!this works :)我明白我的錯誤現在。非常感謝你:) :) :) –
我的快樂。 :) –
這工作得很好,我...
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窗口 – MadProgrammer
這將是非常有用的,有一些其他的代碼,例如,當鼠標滾輪旋轉時應該執行的代碼。 –
你在測試什麼?它有重點嗎? – MadProgrammer
@jimmyLee好吧,那與我現在試圖做的事情無關。我只是試圖在頁面上下滾動。這段代碼應該足夠用於滾動,對吧? –