简体   繁体   English

Java:帮助使用KeyAdapter

[英]Java: help using KeyAdapter

I'm using java trying to make a basic game but am having some trouble with a KeyAdapter. 我正在使用java尝试制作一个基本的游戏但是在使用KeyAdapter时遇到了一些麻烦。 I've used a very similar format before and thought I understood it, but this has me stumped. 我之前使用过非常相似的格式并认为我理解它,但这让我很难过。

Any help would be appreciated, here is the main code I'm working with 任何帮助将不胜感激,这是我正在使用的主要代码

public class Board extends JPanel implements ActionListener{

Timer timer;
Tank tank = new Tank();

boolean boardset;

public Board(){
setBackground(Color.BLACK);

ImageIcon alien1ii = new ImageIcon(this.getClass().getResource("si_Alien1.png"));
Image alien1 = alien1ii.getImage();
ImageIcon alien2ii = new ImageIcon(this.getClass().getResource("si_Alien2.png"));
Image alien2 = alien2ii.getImage();
ImageIcon alien3ii = new ImageIcon(this.getClass().getResource("si_Alien3.png"));
Image alien3 = alien3ii.getImage();

timer = new Timer(5, this);
timer.start();

addKeyListener(new TAdapter());

JButton button = new JButton(new AbstractAction("hello2"){
    @Override
    public void actionPerformed(ActionEvent e){
        boardset = false;
    }
}); 
this.add(button);

//actual game
setFocusable(true);
setDoubleBuffered(true);
}

public void paint(Graphics g){
    super.paint(g);
    g.setColor(Color.WHITE);

    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(tank.getTank(), tank.getx(), tank.getY(), this);

    g2d.drawLine(0, (tank.getY()+25), 400, (tank.getY()+25));

    Toolkit.getDefaultToolkit().sync();     
    g.dispose();
}

public class TAdapter extends KeyAdapter{
    public void keyPressed(KeyEvent e){
        tank.keyPressed(e);
        System.out.println("pressedddddddddddddddddd");
    }
    public void keyReleased(KeyEvent e){
        tank.keyReleased(e);
    }
}

public void setBoardset(boolean x){
    boardset = x;
}

public boolean getBoardset(){
    return boardset;
}

@Override
public void actionPerformed(ActionEvent e) {
        repaint();
        tank.move();
    }

}

Seems to me like this should be pretty straightforward, right now I'm using this print statement to see if the class is actually recognizing key strokes at all: public class TAdapter extends KeyAdapter{ public void keyPressed(KeyEvent e){ tank.keyPressed(e); 在我看来这应该是非常简单的,现在我正在使用这个print语句来查看该类是否实际上正在识别键击:公共类TAdapter扩展KeyAdapter {public void keyPressed(KeyEvent e){tank.keyPressed( E); System.out.println("pressedddddddddddddddddd"); 的System.out.println( “pressedddddddddddddddddd”); } }

However, there is no output. 但是,没有输出。 So I suspect it is not recognizing any keystrokes at all. 所以我怀疑它根本没有识别出任何击键。 But I can't figure out why. 但我无法弄清楚为什么。 If anybody has any suggestions I would appreciate it. 如果有人有任何建议我会很感激。 Obviously I have more code I can share if anybody thinks it would be useful in figuring out this bug. 显然,如果有人认为在找出这个bug时有用,我可以分享更多的代码。

1)Use KeyBindings KeyListener has 2 big issues,first you listen to all keys and second you have to have focus and be focusable. 1)使用KeyBindings KeyListener有两大问题,首先你要听所有的键,然后你需要有焦点并且可以专注。 Instead KeyBinding you bind for a key and you don't have to be in focus. 相反,KeyBinding绑定一个键,你不必成为焦点。

Simple Example: 简单示例:

AbstractAction escapeAction = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
         //code here example
         ((JComponent)e.getSource()).setVisible(Boolean.FALSE);
    }};
 String key = "ESCAPE";
 KeyStroke keyStroke = KeyStroke.getKeyStroke(key);
 component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, key);
 component.getActionMap().put(key, escapeAction);

You can use these JComponent constants 您可以使用这些JComponent常量

WHEN_ANCESTOR_OF_FOCUSED_COMPONENT 
WHEN_FOCUSED 
WHEN_IN_FOCUSED_WINDOW 

2) Don't use concrete inheritance if it isn't necessary at all. 2)如果根本不需要,不要使用具体的继承。

3) Don't implement ActionListener in top classes, see Single Responsability Principle Example Change this: 3)不要在顶级类中实现ActionListener,请参阅单一责任原则示例更改此:

public class Board extends JPanel implements ActionListener{

to: 至:

 public class Board{
   private JPanel panel;

    private class MyActionListener implements ActionListener{
       //code here
    }
  }

4) Don't use inheritance if it's just the same for example in your KeyAdapter , you don't add nothing to it, just use KeyAdapter (Now you are gonna to use keybinding so this is useless but to know :) ). 4)如果你的KeyAdapter只是相同的话,不要使用继承,你不要添加任何内容,只需使用KeyAdapter (现在你将使用键绑定,所以这没用,但要知道:))。

5) Add @Override annotation when you do overriding , also you should override paintComponent(..) instead of paint(..) in swing. 5)在覆盖时添加@Override注释,也应该在swing中覆盖paintComponent(..)而不是paint(..)

KeyListener suffers from focus issues. KeyListener受到焦点问题的困扰。 The component needs to be both focusable and have focus in order for the listener to be notified of key events. 该组件需要既可聚焦又具有焦点,以便向收听者通知关键事件。

A better solution would be to use Key Bindings which don't suffer from these constraints. 更好的解决方案是使用不受这些约束影响的密钥绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM