为什么mousePress不执行任何操作?

问题描述

我是Java的新手,我正在尝试制作自动答题器。它是这样的。当您单击按钮时,应用程序开始单击(在按s时也有效),并且在您按“ w”时,应用程序停止单击。我目前的主要问题是我无法使应用程序单击:V。 (我还有一个用于启动的“ main.java”)这是我的代码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,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);
        
    }
    
}

解决方法

我目前的主要问题是我无法使应用程序单击:V。

好吧,您没有将键绑定分配给“ V”键。您为“ W”定义了两次绑定。

已经说过您的代码仍然不正确,将来会给您带来问题:

  1. 您要在BorderLayout.CENTER中添加两个组件
  2. 在框架可见之前,应将
  3. 组件添加到框架中
  4. 您使用了错误的InputMap

在上一个问题中给您的教程中,有3个InputMap。默认的InputMap仅在组件具有焦点时才起作用。在您不正确的示例中,标签确实具有焦点。但是,如果添加更多组件,则可能无法保持焦点。

对于游戏而言,确保您的游戏对KeyStroke做出响应的最简单方法是将KeyStroke绑定到InputMap的{​​{1}}帧。然后,框架上的哪个组件具有焦点都没有关系,将调用该动作。

因此您的代码应类似于:

JRootPane

不需要JLabel。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...