我正在编写一个简单的 java 程序,旨在打破 afk 限制。每当我在 Eclipse 中运行它时,它都能正常工作。但是,当我将其导出为可运行的 jar 文件时,它不会模拟按键。我相信我有正确的运行配置和一切,这就是我如此困惑的原因。无论如何,这是主要课程:
package minebot;
import javax.swing.JOptionPane;
public class MineBotRunner
{
public static void main(String[] args)
{
try
{
MineBot bot = new MineBot();
bot.run();
}
catch (Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "MineBot encountered an error and will now close.", "MineBot", -1);
System.exit(0);
}
}
}
这是另一类:
package minebot;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MineBot implements ActionListener
{
private JFrame frame;
private JButton button;
private String title = "MineBot";
private boolean isMoving = false;
private long sysTime;
private long waitTime;
private long delay = 780000; //A 13 minute interval between actions to beat the 15 minute AFK limit.
private int holdDelay = 500; //A .5 second hold time for key presses.
private int wait = 200; //A .2 second wait time between forward and reverse actions.
private int forward = KeyEvent.VK_W; //The key held for the first action.
private int backward = KeyEvent.VK_S; //The key held for the second action.
private Robot robo;
public MineBot()
{
}
private void CreateGUI()
{
try
{
robo = new Robot();
}
catch (AWTException e)
{
JOptionPane.showMessageDialog(null, "MineBot encountered an error and will now close.",title, 2);
System.exit(0);
}
frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(250,150));
button = new JButton("Start");
button.setBackground(Color.RED);
button.addActionListener(this);
button.setActionCommand("button");
frame.add(button);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JOptionPane.showMessageDialog(frame, "<HTML><CENTER>Welcome to MineBot! If you have any ideas<br> for additional features or programs, please<br> email me at <a color=#fff>Zanda268@gmail.com</a>!</CENTER</HTML>",title, -1);
}
private void StartMoveLoop() throws InterruptedException
{
sysTime = System.currentTimeMillis();
waitTime = sysTime + delay;
while(true)
{
if(isMoving)
{
sysTime = System.currentTimeMillis();
if(sysTime>waitTime)
{
waitTime = sysTime + delay;
robo.keyPress(forward);
Thread.sleep(holdDelay);
robo.keyRelease(forward);
Thread.sleep(wait);
robo.keyPress(backward);
Thread.sleep(holdDelay);
robo.keyRelease(backward);
}
}
}
}
public void run() throws InterruptedException
{
CreateGUI();
StartMoveLoop();
}
public void actionPerformed(ActionEvent a)
{
if(a.getActionCommand().equals("button"))
{
if(isMoving)
{
button.setBackground(Color.RED);
button.setText("Start");
isMoving = false;
}
else
{
button.setBackground(Color.GREEN);
button.setText("Stop");
isMoving = true;
}
}
}
}
它们的 GUI 会弹出,我可以单击按钮,但是没有模拟按键。任何帮助将不胜感激!