将按钮单击事件传递给按键时执行的操作

发布于 2024-12-29 04:35:27 字数 1074 浏览 3 评论 0原文

我正在处理必须使用 AWT 完成的 Java 作业。我想要一个按钮,通过在按钮处于焦点状态时按 Enter 键来触发。我想出了如何在 Swing 中使用 doClick() 方法执行此操作,但这似乎在 AWT 中不起作用。到目前为止,我正在尝试这样做:

button.addActionListener(this); // Passes value from a TextBox to actionPerformed() 

button.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
         if(e.getKeyCode()==KeyEvent.VK_ENTER) {
              actionPerformed(null);
         }
    } 
});

public void actionPerformed (ActionEvent e) {
     try {  
          if (e.getSource() == button) {
               // Stuff I want to happen
          } else if (e.getSource() == anotherButton) {
               // Other Stuff
          } else {     //third button
               // More stuff
          }
     } catch (NumberFormatException nfe) { 
          // Null argument in keyPressed triggers this
          // catches empty string exception from TextBox
     }
 }

正如我在评论中提到的,空参数将触发捕获。有谁知道按下按钮的争论可能是什么,或者也许是一种更简单的方法来解决这个问题?谢谢。

编辑 - 澄清:actionPerformed() 根据单击三个按钮中的哪一个,使用来自 TextBox 的输入执行三件事之一。 try/catch 是捕获空字符串/格式异常。

I'm working on a Java assignment that has to be done using AWT. I want a button to trigger by pushing the enter key while the button is in focus. I figured out how to do this in Swing with the doClick() method, but this doesn't seem to work in AWT. So far I'm trying this:

button.addActionListener(this); // Passes value from a TextBox to actionPerformed() 

button.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
         if(e.getKeyCode()==KeyEvent.VK_ENTER) {
              actionPerformed(null);
         }
    } 
});

public void actionPerformed (ActionEvent e) {
     try {  
          if (e.getSource() == button) {
               // Stuff I want to happen
          } else if (e.getSource() == anotherButton) {
               // Other Stuff
          } else {     //third button
               // More stuff
          }
     } catch (NumberFormatException nfe) { 
          // Null argument in keyPressed triggers this
          // catches empty string exception from TextBox
     }
 }

As I mentioned with the comments, the null argument will trigger the catch. Does anyone have any idea what that argument might be for the button press or perhaps an altogether easier way to go about this? Thanks.

Edit - clarification: actionPerformed() does one of three things with input from a TextBox depending on which of three buttons is clicked. The try/catch is to catch empty string/format exceptions.

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

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

发布评论

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

评论(1

柏拉图鍀咏恒 2025-01-05 04:35:28

您始终可以拥有一个名为 onButtonPress() 的方法,您的 actionPerformed 以及您的 keyPressed 都可以调用该方法。

  button.addActionListener(this);

    button.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
         if(e.getKeyCode() == KeyEvent.VK_ENTER) {
              onButtonPress();
         }
    } 
 });

public void actionPerformed (ActionEvent e) {
    if (e.getSource() == button){
       onButtonPress();
    } 
 }

private void onButtonPress(){
    // do something
}

You can always have a method called something like onButtonPress(), which your actionPerformed can call, as well as your keyPressed.

  button.addActionListener(this);

    button.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
         if(e.getKeyCode() == KeyEvent.VK_ENTER) {
              onButtonPress();
         }
    } 
 });

public void actionPerformed (ActionEvent e) {
    if (e.getSource() == button){
       onButtonPress();
    } 
 }

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