为什么`mousePress`没有任何作用?

huangapple go评论74阅读模式
英文:

Why doesn't mousePress do anything?

问题

我是Java的新手正在尝试制作一个自动点击器它的工作原理是这样的当你点击一个按钮时应用程序开始点击也可以在按下s键时工作),当你按下"w"键时应用程序停止点击我目前遇到的主要问题是我无法让我的应用程序点击V我还有一个名为"main.java"的启动文件)。以下是我的代码

package copy;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Game implements ActionListener {

    JFrame frame;
    JLabel label;
    JButton button;
    Action ON;
    Action OFF;
    private static Robot bot;
    public static boolean status = false;

    Game() {
        ON = new statusON();
        OFF = new statusOFF();

        frame = new JFrame("Bullet Chicken Clicker");
        label = new JLabel();
        button = new JButton("turn on?");
        frame.setSize(400, 400);
        frame.setLocation(600, 150);
        frame.setVisible(true);
        frame.setAlwaysOnTop(true);
        frame.add(label);
        frame.add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        label.getInputMap().put(KeyStroke.getKeyStroke('w'), "OFF");
        label.getActionMap().put("OFF", OFF);

        label.getInputMap().put(KeyStroke.getKeyStroke('w'), "upAction");
        label.getActionMap().put("upAction", ON);
        label.getInputMap().put(KeyStroke.getKeyStroke('s'), "downAction");
        label.getActionMap().put("downAction", OFF);

        button.setPreferredSize(new Dimension(40, 40));
        button.setOpaque(true);
        button.setForeground(Color.BLACK);
        button.setBounds(125, 150, 150, 30);
        button.setVisible(true);
        button.addActionListener(this);
        button.setFocusable(false);
    }

    private void clicky() {
        while (status == true);
        bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        bot.delay(300);
        bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        bot.delay(300);
    }

    public static void robot() {
        try {
            bot = new Robot();
        } catch (AWTException e2) {
            e2.printStackTrace();
        }
    }

    public class statusON extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {
            status = true;
            System.out.print(status);
        }
    }

    public class statusOFF extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {
            status = false;
            System.out.print(status);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        status = true;
        System.out.print(status);
    }
}
英文:

I am new to Java and I'm trying to make an auto clicker. It works like this. when you click a button, the application starts clicking (also works when you press s), and when you press "w" the application stops clicking. My main issue currently is I can't manage to make my application click :V. (I also have a "main.java" for startup) Here's my code vvvvvvv

    package copy;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game 
implements ActionListener{
JFrame frame;
JLabel label;
JButton button;
Action ON;
Action OFF;
private static Robot bot;
public static boolean status = false;
Game(){
ON = new statusON();
OFF = new statusOFF();
frame = new JFrame("Bullet Chicken Clicker");
label = new JLabel();
button = new JButton("turn on?");
frame.setSize(400, 400);
frame.setLocation(600, 150);
frame.setVisible(true);	
frame.setAlwaysOnTop(true);
frame.add(label);
frame.add(button); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label.getInputMap().put(KeyStroke.getKeyStroke('w'), "OFF");
label.getActionMap().put("OFF", OFF);
label.getInputMap().put(KeyStroke.getKeyStroke('w'), "upAction");
label.getActionMap().put("upAction", ON);
label.getInputMap().put(KeyStroke.getKeyStroke('s'), "downAction");
label.getActionMap().put("downAction", OFF);
button.setPreferredSize(new Dimension(40, 40));
button.setOpaque(true);
button.setForeground(Color.BLACK);	 
button.setBounds(125, 150, 150, 30);
button.setVisible(true);
button.addActionListener(this);
button.setFocusable(false);
}
private void clicky() {
while (status == true);
bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
bot.delay(300);
bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
bot.delay(300);
}
public static void robot() {
try {
bot = new Robot();
} catch (AWTException e2) {
e2.printStackTrace();
}
}
public class statusON extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
status = true;
System.out.print(status);
}		
}
public class statusOFF extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
status = false;
System.out.print(status);
}		
}
@Override
public void actionPerformed(ActionEvent e) {
status = true;
System.out.print(status);
}
}

答案1

得分: 0

> 目前我主要的问题是我无法让我的应用程序点击 :V。

嗯,你没有给 "V" 键分配键绑定。你将 "W" 键的绑定定义了两次。

虽然如此,你的代码仍然不正确,并且将会在未来引发问题:

  1. 你向 BorderLayout.CENTER 添加了两个组件。
  2. 在将框架设为可见之前,应该向框架添加组件。
  3. 你使用了错误的 InputMap(输入映射)。

从上次问题中为你提供的教程中,有 3 个 InputMap。默认的 InputMap 仅在组件获得焦点时起作用。在你的错误示例中,恰好标签获得了焦点。然而,如果你添加更多组件,它很可能不会保持焦点。

对于游戏来说,确保游戏响应 KeyStroke 最简单的方法是将 KeyStroke 绑定到框架的 JRootPaneInputMap。然后,无论框架上的哪个组件获得焦点,都会调用该动作。

因此,你的代码应该类似于:

JRootPane rootPane = frame.getRootPane();
InputMap im = rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

im.put(KeyStroke.getKeyStroke('v'), "OFF");
rootPane.getActionMap().put("OFF", OFF);
im.put(KeyStroke.getKeyStroke('w'), "upAction");
rootPane.getActionMap().put("upAction", ON);
im.put(KeyStroke.getKeyStroke('s'), "downAction");
rootPane.getActionMap().put("downAction", OFF);

不需要 JLabel。

英文:

> My main issue currently is I can't manage to make my application click :V.

Well, you don't assign a key binding to the "V" key. You define the binding for "W" twice.

Having said that your code is still incorrect and will cause you problems in the future:

  1. you are adding two components to the BorderLayout.CENTER
  2. components should be added to the frame BEFORE the frame is made visible
  3. you are using the wrong InputMap

From the tutorial given to you in your last question there are 3 InputMaps. The default InputMap will only work when the component has focus. In your incorrect example is just happens that the label does have focus. However, if you add more components it will likely not retain focus.

In the case of a game the easiest way to make sure you game responds to the KeyStroke is to bind the KeyStroke to the InputMap of the JRootPane of the frame. Then it doesn't matter which component on the frame has focus, the Action will be invoked.

So your code should be something like:

JRootPane rootPane = frame.getRootPane();
InputMap im = rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke('v'), "OFF");
rootPane.getActionMap().put("OFF", OFF);
im.put(KeyStroke.getKeyStroke('w'), "upAction");
rootPane.getActionMap().put("upAction", ON);
im.put(KeyStroke.getKeyStroke('s'), "downAction");
rootPane.getActionMap().put("downAction", OFF);

There is no need for the JLabel.

huangapple
  • 本文由 发表于 2020年9月1日 13:16:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63681759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定