使用键盘在JButtons之间移动

时间:2013-07-08 09:50:15

标签: java swing focus flowlayout

我想使用JButtons或任何其他布局在JPanel中添加FlowLayout,并使用JButtonsUp DownJButton之间移动..由于我不知道public class abc{ List<ControlCenterButton> buttons=getButtons(); JPanel buttonPanel= new JPanel(); buttonPanel.setLayout(new FlowLayout) for (int i = 0; i < buttons.size(); i++) { ControlCenterButton button = buttons.get(i); if (button.getLayarID().equals(id)) { flag = true; button.addActionListener(this); button.setPosition(i); button.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent mouseEvent) { buttonPanel.requestFocusInWindow(); } }); buttonPanel.add(button); } buttonPanel.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent keyEvent) { final ControlCenterButton selectedControlCenterButton = (ControlCenterButton) Session.getSession().getAttribute("controlcenter.selectedbutton"); int position = selectedControlCenterButton.getPosition(); int keycode = keyEvent.getKeyCode(); switch (keycode) { case KeyEvent.VK_RIGHT: { if (position + 1 >= buttons.size()) { ControlCenterButton.selectButton(buttons.get(0)); } else ControlCenterButton.selectButton(buttons.get(position + )); break; } case KeyEvent.VK_UP: { break; } case KeyEvent.VK_DOWN: { } case KeyEvent.VK_ENTER: { selectedControlCenterButton.requestFocusInWindow(); new ControlCenterButton().openApplication(selectedControlCenterButton); break; } } } public class ControlCenterButton extends JButton implements ActionListener { private int position; public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } 的行和列,因此在上下移动时遇到问题。

{{1}}

1 个答案:

答案 0 :(得分:-1)

  1. 为所有按下向上向下键按下

  2. 的按钮添加一个按键监听器
  3. 将x,y坐标添加到按钮。 IE浏览器。您可以扩展JButton或者使用某种将坐标与JButton相关联的地图。

  4. 按下向上和向下然后计算焦点应落在哪里,然后在找出它应该是什么按钮之后。在按钮上调用 requestFocusInWindow()