简体   繁体   English

java awt 键盘输入的首选方式是什么

[英]what is the preferred way to java awt keyboard input

I seen many conflicting recommendations on the inte.net including here on how to handle input with awt and swing and several people have worked on my code and its a mess.我在 inte.net 上看到许多相互矛盾的建议,包括此处关于如何使用 awt 和 swing 处理输入的建议,并且有几个人研究过我的代码,但它一团糟。

options选项

  • implement KeyListener or extend KeyAdapter实现 KeyListener 或扩展 KeyAdapter
  • ^to the application's main class, use an anonymous class, use a private class or use an external input managing class. ^到应用程序的主要 class,使用匿名 class,使用私有 class 或使用外部输入管理 class。
  • to send the event object to each object that needs to know input, to send an array of keys pressed, or to make every class a listener and add it to the main class.将事件 object 发送给需要知道输入的每个 object,发送一组按下的键,或者让每个 class 成为监听器并将其添加到主 class。

So I could have所以我可以

public class Board extends JPanel implements KeyListener{
    public Board(){addKeyListener(this);}}

or要么

public Board(){addKeyListener( new KeyListener(){...});}

or要么

public class Board extends JPanel {
    private class PrivateListener implements KeyListener{...} 
    public Board(){addKeyListener(new PrivateListener());

or要么

public class PublicListener implements KeyAdapter{...}
public class Board extends JPanel {
    public Board(){addKeyListener(new PublicListener());

or要么

addKeyListener(this);
addKeyListener(obj1);
addKeyListener(obj2);

and implements KeyListener can be replaced with extends KeyAdapter but I won't do that because java only allows for one parent class.implements KeyListener可以替换为extends KeyAdapter但我不会这样做,因为 java 只允许一个父 class。

then there is which I don't know how this got into my code然后有一个我不知道这是怎么进入我的代码的

private boolean [] keys = new boolean[256];
public void keyPressed(KeyEvent e) {keys[e.getKeyCode()] = true;}
public void keyReleased(KeyEvent e) {keys[e.getKeyCode()] = false;}
public boolean isKeyDown(int k) {return keys[k];}

or要么

public void keyPressed(KeyEvent e) {
    obj1.keyPressed(e);
    obj2.keyPressed(e);
}

Truly, what is the best implementation of awt keyboard input?真的,awt 键盘输入的最佳实现是什么?

This is a solid "it depends" situation, and this answer is open to plenty of academic discussion.这是一个可靠的“视情况而定”的情况,这个答案可以进行大量的学术讨论。

My Oppinion:我的意见:

This is definately just an oppinion, but I would recomend talking to your coders and agreeing on two things.这绝对只是一个意见,但我建议与您的编码人员交谈并就两件事达成一致。 Make your decision based on the answers, and put it in the README at the base of your code:根据答案做出决定,并将其放在代码底部的 README 中:

  1. Anonymous classes, or not?匿名类,与否?
  2. Mega classes, or not?大型课程,或不?

Assuming prefering small, named classes I would go for the following.假设我更喜欢小型的命名类,我会为以下内容提供 go。

public class PublicListener implements KeyAdapter{...}
public class Board extends JPanel {
    public Board(){addKeyListener(new PublicListener());

Using KeyAdapter you will only have to override the functions you care about, and other programmers can see quite easily that this is where the input is handled as it is in its own class.使用 KeyAdapter,您只需覆盖您关心的功能,其他程序员可以很容易地看到这是处理输入的地方,因为它是在它自己的 class 中。

TLDR TLDR

Firstly, wanting to work with low level classes is a valid desire.首先,想要与低级别课程一起工作是一个有效的愿望。 In production code I would not recomend it, but AWT is still the basis of Swing, and wanting to learn is not to be discouraged, If nobody learns the low level stuff, then one day the open source community will have a big problem being built on top of code.在生产代码中我不会推荐它,但是AWT仍然是Swing的基础,想学也不要气馁,如果底层的东西没人学,那么有一天开源社区的建设会出大问题在代码之上。 which nobody has learned.没有人学到过。

While the comments on Swing and Key Bindings are valid, in the documentation about Swing Key Bindings, there is the quote:虽然对 Swing 和键绑定的评论是有效的,但在有关 Swing 键绑定的文档中,引用如下:

An alternative to key bindings is using key listeners.键绑定的替代方法是使用键侦听器。 Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code.按键侦听器作为键盘输入的低级接口有自己的位置,但对于响应单个按键,按键绑定更合适,并且往往会导致更易于维护的代码。 So if you are in it to learn some low level stuff, go for it, but take note of the maintainability warnings!因此,如果您在其中学习一些底层知识,请拨打 go,但请注意可维护性警告!

The rest comes down to two main point. rest归结为两个要点。 Coding Style and Application Architechture.编码风格和应用程序架构。

Coding Style:编码风格:

Consistency across a code base is often more important than having the perfect implementation, so if you have a codebase with an existing style, then stick to that style.代码库之间的一致性通常比拥有完美的实现更重要,因此如果您的代码库具有现有风格,请坚持使用该风格。 For example, if your codebase already uses lots of Anonymous Classes in other places for other things, then adding a new KeyListener(){...} is fine.例如,如果您的代码库已经在其他地方使用了很多匿名类来处理其他事情,那么添加一个new KeyListener(){...}就可以了。 That said, in my personal projects I avoid anonymous classes, because personal preference...也就是说,在我的个人项目中,我避免使用匿名类,因为个人偏好...

The decision for (what I call) Mega and Mini classes is also important in coding style.决定(我所说的)Mega 和 Mini 类在编码风格上也很重要。 The very clear recomendation is to prefer small (mini) classes, with specific roles.非常明确的建议是更喜欢具有特定角色的小型(迷你)班级。 This would support implementing a dedicated class which extends KeyAdapter.这将支持实现扩展 KeyAdapter 的专用 class。 Mini classes rarely struggle from multiple inheritance problems, allowing you to rely on default implementations (like KeyAdapter), further reducing your code complexity.迷你类很少遇到多个 inheritance 问题,允许您依赖默认实现(如 KeyAdapter),进一步降低代码复杂性。 Future coders can then very easily see what that class is for.未来的编码人员可以很容易地看到 class 的用途。 Mega classes centralise all of the code into one java file, but then you will be implementing lots of interfaces, and also having to provide default implementations for everything.巨型类将所有代码集中到一个 java 文件中,但随后您将实现大量接口,并且还必须为所有内容提供默认实现。 Compairing KeyListener to KeyAdapter this means implementing three functions instead of overriding one.将 KeyListener 与 KeyAdapter 进行比较意味着实现三个函数而不是覆盖一个函数。

Mega classes have their place because multiple inheritance. A "Player" can be a Human, "PositionProvider", a "Drawable" etc. But in all seriousness, this is just not recommended.超级类有它们的位置,因为多个 inheritance。“玩家”可以是人类、“位置提供者”、“可绘制对象”等。但严肃地说,这是不推荐的。

Architechture:架构:

Games programming was mentioned, so this is also an important consideration.提到了游戏编程,所以这也是一个重要的考虑因素。 The architechture of a game can be very different to a dektop GUI application.游戏的架构可能与桌面 GUI 应用程序截然不同。 You need to pick an architechture, and make code which sticks to it.您需要选择一种架构,并编写遵循它的代码。 Eg are you planning to let several components listen to the AWT events and act independantly (entities), or do you plan to have a seperate "InputManager" class, which abstracts input from eg keyboard and joypad?例如,您是否计划让多个组件监听 AWT 事件并独立行动(实体),或者您是否计划拥有一个单独的“InputManager”class,它从键盘和游戏手柄等抽象输入?

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

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