英文:
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" 键的绑定定义了两次。
虽然如此,你的代码仍然不正确,并且将会在未来引发问题:
- 你向 BorderLayout.CENTER 添加了两个组件。
- 在将框架设为可见之前,应该向框架添加组件。
- 你使用了错误的 InputMap(输入映射)。
从上次问题中为你提供的教程中,有 3 个 InputMap。默认的 InputMap 仅在组件获得焦点时起作用。在你的错误示例中,恰好标签获得了焦点。然而,如果你添加更多组件,它很可能不会保持焦点。
对于游戏来说,确保游戏响应 KeyStroke
最简单的方法是将 KeyStroke
绑定到框架的 JRootPane
的 InputMap
。然后,无论框架上的哪个组件获得焦点,都会调用该动作。
因此,你的代码应该类似于:
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:
- you are adding two components to the BorderLayout.CENTER
- components should be added to the frame BEFORE the frame is made visible
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论