使用箭头键浏览JButtons [英] Browsing JButtons with Arrow Keys

查看:126
本文介绍了使用箭头键浏览JButtons的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个 JButton 数组,代表卡片,其中有16个,4个4个。
如何浏览 JButton 键盘上的箭头而不是鼠标,如何通过按ENTER键而不是鼠标点击点击 JButton ?也许有另一种方法可以做到这一点,而不是使用 JButton s?

I've made a JButton array, which represent cards, there are 16 of them, 4 by 4. How can I browse among the JButton with the arrows on keyboard instead of the mouse, and how can I "click" on JButton by pressing ENTER instead of mouseclicking? Maybe there is another way of doing this instead of using JButtons?

祝你好运!

推荐答案

我创建了一个解决方案,允许您使用箭头键导航按钮并用空格激活它们并输入。

I have created a solution that will allow you to navigate buttons with the arrow keys and activate them with space and enter.

以下是所有代码。没有提供任何评论,如果您有任何问题,请告诉我。

Below is all of the code. No comments are provided, so let me know if you have any questions.

import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonPane extends JPanel {

   private JButton[][] buttons;

   public ButtonPane(int row, int col) {
      super(new GridLayout(row, col));
      buttons = new JButton[row][col];
      for (int i = 0; i < buttons.length; i++) {
         for (int j = 0; j < buttons[i].length; j++) {
            final int curRow = i;
            final int curCol = j;
            buttons[i][j] = new JButton(i + ", " + j);
            buttons[i][j].addKeyListener(enter);
            buttons[i][j].addKeyListener(new KeyAdapter() {
               @Override
               public void keyPressed(KeyEvent e) {
                  switch (e.getKeyCode()) {
                  case KeyEvent.VK_UP:
                     if (curRow > 0)
                        buttons[curRow - 1][curCol].requestFocus();
                     break;
                  case KeyEvent.VK_DOWN:
                     if (curRow < buttons.length - 1)
                        buttons[curRow + 1][curCol].requestFocus();
                     break;
                  case KeyEvent.VK_LEFT:
                     if (curCol > 0)
                        buttons[curRow][curCol - 1].requestFocus();
                     break;
                  case KeyEvent.VK_RIGHT:
                     if (curCol < buttons[curRow].length - 1)
                        buttons[curRow][curCol + 1].requestFocus();
                     break;
                  default:
                     break;
                  }
               }
            });
            add(buttons[i][j]);
         }
      }
   }

   private KeyListener enter = new KeyAdapter() {
      @Override
      public void keyTyped(KeyEvent e) {
         if (e.getKeyChar() == KeyEvent.VK_ENTER) {
            ((JButton) e.getComponent()).doClick();
         }
      }
   };

   public static void main(String[] args) {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.add(new ButtonPane(4, 4));
      f.pack();
      f.setVisible(true);
   }
}

要运行此功能,只需粘贴到名为<的类中code> ButtonPane 。

To run this, simply paste into a class called ButtonPane.

这里重要的是调用 requestFocus()在调用箭头键时,在正确的 JButton 上。当按下Enter键时,我还注册了额外的 KeyListener

The important bit here is calling requestFocus() on the correct JButton when an arrow key is called. I also registered an extra KeyListener for when the Enter key is pressed.

这篇关于使用箭头键浏览JButtons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆