gpt4 book ai didi

java - 以编程方式运行 Swing 应用程序(远程类)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:41:11 25 4
gpt4 key购买 nike

我有一个稍微复杂的案例,我没有我试图自动运行的 swing 应用程序的源代码(或编译类)。

我将尝试在此应用程序上执行一系列任务,按一些按钮,单击某些部分等。我希望能够以编程方式执行此操作。

我遇到的每个 Swing 调试器/机器人都希望您拥有正在启动的类,并且调试器与类一起启动。

这里的问题是我的应用程序是由我启动 JNLP 应用程序启动的,它对我进行身份验证(我必须输入用户名和密码),然后在远程服务器上运行一堆类。然后启动 Swing 应用程序。

我希望现在可以附加到 swing 应用程序并以编程方式运行它。抱歉,这看起来太复杂了,但这就是这里的场景......

也许根本就没有办法做到这一点,如果是这样的话也请告诉我...

最佳答案

如果您只知道点击哪里,那么制作您自己的机器人应用程序不是问题。它通常只需要一个开始条件 - 实际程序在屏幕上的位置。

这可能会帮助您开始:

public class MyRobot extends Robot {

public MyRobot(Point initialLocation) throws AWTException {

setAutoDelay(20);

// focus on the program
click(initialLocation);

// if you need to take screen shot use
BufferedImage screen =
createScreenCapture(
new Rectangle(initialLocation.x, initialLocation.y, 200, 200));

// analyze the screenshot...
if(screen.getRGB(50, 50) > 3) /*do something :) */;


// go to the correct field
press(KeyEvent.VK_TAB);

// press "a"
press(KeyEvent.VK_A);

// go to the next field
press(KeyEvent.VK_TAB);

// write something...
type("Hello World..");
}

private void click(Point p) {
mousePress(InputEvent.BUTTON1_MASK);
mouseRelease(InputEvent.BUTTON1_MASK);
}

private void press(int key) {
keyPress(key);
keyRelease(key);
}

private void type(String string) {
// quite complicated... see
//http://stackoverflow.com/questions/1248510/convert-string-to-keyevents
}

@SuppressWarnings("serial")
public static void main(String[] args) throws Exception {
final JDialog d = new JDialog();
d.setTitle("Init");
d.add(new JButton(
"Put your mouse above the 'program' " +
"and press this button") {
{
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
synchronized (d) { d.notify(); }
d.dispose();
}
});}
});
d.setSize(200, 100);
d.setVisible(true);
// wait for it to be closed
synchronized (d) {
d.wait();
}
new MyRobot(MouseInfo.getPointerInfo().getLocation());
}
}

关于java - 以编程方式运行 Swing 应用程序(远程类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2974832/

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