如何使 ActionEvent 和 KeyEvent 触发相同的操作?

发布于 2024-10-04 06:24:00 字数 521 浏览 4 评论 0原文

我正在编写一个小程序,想要弄清楚如何使按钮和按键事件覆盖相同的代码位。对于这个问题,我将这个按钮称为 fireButton。操作事件的代码当然会如下所示:

public void actionPerformed(ActionEvent e) {
   if (e.getSource() == fireButton) {
      //all the code that pressing button executes
   }  
}

现在,我想按“enter”键来执行操作事件处理的相同代码,但我不想在 中再次重写所有代码keyPressed 方法。

具体来说,我正在编写一个战舰程序,“Fire”按钮从两个文本字段获取输入,处理异常,并将输入作为参数传递给在网格中的特定方块处触发的方法。理想情况下,按 Enter 键的功能与按开机按钮的功能相同。有没有办法让某个方法调用 actionPerformed 方法?如果不是,那么解决该问题的优雅方法是什么?

I'm writing an applet and want to figure out how to make a button and a key event cover the same bit of code. For this question, I'll call this button fireButton. The code for the action event would of course look like this:

public void actionPerformed(ActionEvent e) {
   if (e.getSource() == fireButton) {
      //all the code that pressing button executes
   }  
}

Now, I want pressing the 'enter' key to perform the same code that the action event handles, but I do not want to rewrite all the code again in a keyPressed method.

To be specific, I'm writing a battleship program, and the 'Fire' button takes input from two textFields, handles exceptions, and passes the input as parameters to a method that fires at a particular square in the grid. Ideally, pressing the enter key would function the same way as if I had pressed the fire button. Is there a way to make a certain method call an actionPerformed method? If not, what would be an elegant solution to the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡笑忘祈一世凡恋 2024-10-11 06:24:00
  1. 创建操作
  2. 将操作添加到 JButton
  3. 使用键绑定将 Enter 键绑定到操作

阅读 Swing 教程。有以下部分:

  1. 如何使用操作
  2. 如何使用键绑定

如果您只是谈论使用 Enter 键调用“Fire”按钮,请查看 输入键和按钮有多种方法。

  1. Create an Action
  2. Add the Action to the JButton
  3. Use Key Bindings to bind the Enter key to the Action

Read the Swing tutorial. There are sections on:

  1. How to Use Actions
  2. How to Use Key Bindings

If you are just talking about invoking the "Fire" button with the enter key then check out Enter Key and Button for a couple of approaches.

星軌x 2024-10-11 06:24:00

我建议您将所有代码放在一个单独的方法中,该方法接收来自事件的所有相关数据(如果有)作为参数:

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == fireButton) {
        Object relevantData0 = new Object(); // e.getSomething();
        Object relevantData1 = new Object(); // e.getSomethingElse();
        handleFireAction(relevantData1, relevantData2);
    }
}  

public void actionPerformed(KeyEvent e) {
    if (e.getSource() == fireButton) {
        Object relevantData0 = new Object(); // e.getSomething();
        Object relevantData1 = new Object(); // e.getSomethingElse();
        handleFireAction(relevantData1, relevantData2);
    }
}  

private void handleFireAction(Object relevantData0, Object relevantData1) { // Object relevantDat2, and so on
    //all the code that pressing button executes
}  

如果您不需要来自事件的任何数据,那就更容易了;)
这样,您只需为这两个事件编写一次代码即可。这是一种通用的面向对象方法。
希望这有帮助。

I suggest you put all the code in a separate method that receives all the relevant data from the event (if any) as parameters:

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == fireButton) {
        Object relevantData0 = new Object(); // e.getSomething();
        Object relevantData1 = new Object(); // e.getSomethingElse();
        handleFireAction(relevantData1, relevantData2);
    }
}  

public void actionPerformed(KeyEvent e) {
    if (e.getSource() == fireButton) {
        Object relevantData0 = new Object(); // e.getSomething();
        Object relevantData1 = new Object(); // e.getSomethingElse();
        handleFireAction(relevantData1, relevantData2);
    }
}  

private void handleFireAction(Object relevantData0, Object relevantData1) { // Object relevantDat2, and so on
    //all the code that pressing button executes
}  

If you don't need any data from the event its even easier ;)
This way you just write your code once for both events. It's a general OO aproach.
Hope this helps.

落花随流水 2024-10-11 06:24:00

借用 MVC 我建议你有一个控制器类来处理这些类型的请求。然后您所要做的就是委托给每个事件处理程序中的控制器。

像这样:

public class BattleShipController {

   public void handleFireAction() {
   // ...
   }
}

//-- in your UI class(es)
private BattleShipController _controller = new BattleShipController();

//-- in event calls:
_controller.handleFireAction();

如果您发布相关代码,我可以提出进一步的建议。

Borrowing from MVC I would recommend you have a controller class which handles these sorts of requests. Then all you have to do is delegate to the controller in each event handler.

Like so:

public class BattleShipController {

   public void handleFireAction() {
   // ...
   }
}

//-- in your UI class(es)
private BattleShipController _controller = new BattleShipController();

//-- in event calls:
_controller.handleFireAction();

If you post relevant code I can make further suggestions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文