在网格面板中使用键盘箭头移动JButton

时间:2013-05-30 11:17:34

标签: java swing animation jbutton keylistener

我想创建一个可以在网格面板中使用键盘箭头移动的JButton。关于如何实现这一目标的任何想法?

这是我到目前为止所做的,但它并没有真正起作用

button.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();
                System.out.println("KEY ^$$#$#$@#$  " + key);
                int position = 0;
                int previousPosition = 0;
                int counter = 0;

                position = Integer.parseInt(button.getName());
                int nextPosition = position;
                if (key == KeyEvent.VK_LEFT) {



                    nextPosition = nextPosition - 1;
                    previousPosition = position;
                    System.out.println("PREVIOUS POSITION: " + position);
                    bt[1].setVisible(false);
                    System.out.println("POSITION: " + position);
                    button.setName("" + nextPosition);
                    bt[nextPosition].add(button);
                }
                else if (key == KeyEvent.VK_DOWN) {
                    counter++;
                    if (counter == 1) {
                         nextPosition = nextPosition + 6;
                    }
                    else if (counter == 2) {
                         nextPosition = nextPosition + 12;
                    }
                    else {
                        nextPosition = 18;
                    }
                    System.out.println("next position:" + nextPosition);
                    System.out.println("counter: " + counter);
                    System.out.println("DOWN ARROW");

                    System.out.println("DOWN ARROW");
                    System.out.println("POSITION VALUE: " + nextPosition);
                    bt[previousPosition].setVisible(false);
                    System.out.println("POSITION: " + nextPosition);
                    button.setName("" + nextPosition);
                    bt[nextPosition].add(button);
                    System.out.println("end");
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
    });

感谢您的帮助

2 个答案:

答案 0 :(得分:5)

嗯,这取决于。

我会建议使用Key Bindings而不是KeyListener,因为它不会受到焦点问题的影响。

这基本上使用GridLayout,只需更改按钮的z顺序......

您可以通过更多工作实现与GridBagLayout类似的功能,但这会带来更灵活的布局

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class MoveButtons {

    public static void main(String[] args) {
        new MoveButtons();
    }

    public MoveButtons() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JButton button;

        public TestPane() {
            setLayout(new GridLayout(4, 4));
            button = new JButton("...");
            add(button);
            for (int index = 0; index < (4 * 4) - 1; index++) {
                JPanel panel = new JPanel();
                panel.setBorder(new LineBorder(Color.GRAY));
                add(panel);
            }

            requestFocus();

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "down");

            am.put("left", new MoveAction(-1));
            am.put("right", new MoveAction(1));
            am.put("down", new MoveAction(4));
            am.put("up", new MoveAction(-4));
        }

        public class MoveAction extends AbstractAction {

            private int delta;

            public MoveAction(int delta) {
                this.delta = delta;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                int index = getComponentZOrder(button);
                index += delta;
                if (index < 0) {
                    index = 0;
                } else if (index >= getComponentCount()) {
                    index  = getComponentCount() - 1;
                }
                setComponentZOrder(button, index);
                revalidate();
            }
        }
    }
}

答案 1 :(得分:0)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MoveButton extends JFrame {

JButton button;
JPanel[] p = new JPanel[100];
int k = 0;

public MoveButton() {
    super();
    button = new JButton("G");
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
                    requestFocusInWindow();
        }
    });
    JPanel panel = new JPanel(new GridLayout(10, 10));
    for (int i = 0; i < 100; i++) {
        p[i] = new JPanel(new BorderLayout());
        p[i].setBorder(BorderFactory.createLineBorder(Color.RED));
        panel.add(p[i]);
    }

    p[0].add(button);

    setFocusable(true);
    requestFocusInWindow();
    addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
            try {
                if (k <= 99 && k >= 0) {
                    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                        if (k + 1 <= 99) {
                            p[++k].add(button);
                        }
                        repaint();
                    } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                        if (k - 1 >= 0) {
                            p[--k].add(button);
                        }
                        repaint();
                    } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                        if (k - 10 >= 0) {
                            p[k -= 10].add(button);
                        }
                        repaint();
                    } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                        if (k + 10 <= 99) {
                            p[k += 10].add(button);
                        }
                        repaint();
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }

    });
    setSize(600, 600);
    add(panel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

public static void main(String[] args) {
    new MoveButton();
}
}