-3

我正在编写一个简单的 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 会弹出,我可以单击按钮,但是没有模拟按键。任何帮助将不胜感激!

4

2 回答 2

3

Eclipse 中包含很多 Jar 文件,它使用 jar 来执行项目。

但是当您将其转换为可运行的 Jar 时。它不会扭曲其中的所有包裹。您必须将它们单独添加到您的项目库中。否则它会导致运行 Jar 时出现问题。!!使用 Add Build Path 添加依赖项。Java 构建路径用于编译 Java 项目以发现依赖类,这就是 JVM 识别依赖类的方式。

在大型项目中,您可以使用类依赖分析器来查找项目中的依赖关系。

于 2013-08-22T05:27:58.017 回答
-3

哇对不起。Eclipse 确实导出了这些依赖项,但像个白痴一样,我忘记将我的 JRE 添加到我的构建路径中。PS @Dileep,您不必“单独”做。

于 2013-08-24T05:16:31.163 回答