Question

My eclipse tells me that i need to use an static modifier but when i do so the hole thing becomes wrong. Here is my code I hope that you can help me and tell me what i did messed up(i got the hint for the inner class from StealthyHunter7):

public class ClickBot 
{

private class Key
    implements KeyListener
    {
        private boolean spacebarPressed = false;

        @Override
        public void keyTyped(KeyEvent e)
        {
        }

        @Override
        public void keyPressed(KeyEvent e)
        {
            if(e.getKeyCode() == KeyEvent.VK_SPACE)
            {
                spacebarPressed = true;
            }
        }

        @Override
        public void keyReleased(KeyEvent e)
        {
            if(e.getKeyCode() == KeyEvent.VK_SPACE)
            {
                spacebarPressed = false;
            }
        }

        public boolean isSpacebarPressed()
        {
            return spacebarPressed;
        }    
    }

    Key keyObject = new Key();

    public static void main(String[] args) throws IOException, AWTException
    {       
        JFrame.addKeyListener(keyObject);
        final Robot robot = new Robot();
        robot.delay(2000);

        while(keyObject.spacebarPressed())
        {
        {
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);

                robot.delay(30);   
            }
        }
    }
}
Was it helpful?

Solution 2

Your main method is static, therefore, you cannot access non-static variables. If you don't want static on your Key object, then do the following:

1) Make a constructor for your ClickBot class

2) Initialize keyObject in it

2) In your main method create an instance of ClickBot

3) Make a new method in your ClickBot class (public void methodName())

4) Put all your further code in it

5) Call clickBotObject.methodName() in your main method.

Otherwise, if static on your Key object is alright with you, then just add static on it.

Or, make it a local variable in your main method if that's alright with you.

OTHER TIPS

The following must NOT be a field, but placed in the main method, that is static.

Key keyObject = new Key();

With a Key field being initialized in a Key class, look what would happen on new Key():

  • a Key instance is created, it has a field Key that must be instantiated
  • a Key instance is created, it has a field Key that must be instantiated
  • a Key instance is created, it has a field Key that must be instantiated
  • ...

;)

Correction: I did not see this were two, nested classes. Make the inner class static.

private static class Key

Otherwise the class Key holds a `ClickBot.this', and the recursion begins.

You are trying to use keyObject as a class member of ClickBot from the main method, which is of course static. So, while you are in main, you don't have a ClickBot instance, which means you can't use the class members.

So, to fix, either move the keyObject inside the main method (1), or make the keyObject static (2).

Here is how to do (1):

public static void main(String[] args) throws IOException, AWTException
{       
    // Move it to here:
    Key keyObject = new Key();

    JFrame.addKeyListener(keyObject);
    final Robot robot = new Robot();
    robot.delay(2000);

    while(keyObject.spacebarPressed())
    {
....

And here is (2):

// Make it static:
static Key keyObject = new Key();

public static void main(String[] args) throws IOException, AWTException
{       
    JFrame.addKeyListener(keyObject);
    final Robot robot = new Robot();
    robot.delay(2000);
....
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top