我想使用JButtons
或任何其他布局在JPanel
中添加FlowLayout
,并使用JButtons
和Up
Down
在JButton
之间移动..由于我不知道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}}
答案 0 :(得分:-1)
为所有按下向上和向下键按下
将x,y坐标添加到按钮。 IE浏览器。您可以扩展JButton或者使用某种将坐标与JButton相关联的地图。
按下向上和向下然后计算焦点应落在哪里,然后在找出它应该是什么按钮之后。在按钮上调用 requestFocusInWindow()。