3

我创建了一个二维 JPanel 表,我的第一个单元格上有一个 JButton。我想用箭头键将按钮移动到表的边界内。我必须将 KeyListener 放在每个单元格或按钮上?也许你会发现我的问题很愚蠢,但我需要一些关于 KeyListener 的帮助。谢谢!这是我必须创建的

这是我写的代码!

public class MyFrame extends JFrame {

JPanel [][] innerCells;

public MyFrame() {
    JFrame fr = new JFrame("Final Exams");
    fr.setSize(800, 600);

    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fr.setVisible(true);
    fr.setLocationRelativeTo(null);

    JPanel p = new JPanel(new GridLayout(10, 10));
    JButton b = new JButton("G");
    innerCells = new JPanel[10][10];

    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            innerCells[i][j] = new JPanel();
            innerCells[i][j].setBorder(BorderFactory.createLineBorder(Color.orange));
            p.add(innerCells[i][j]);
        }
    }
    innerCells[0][0].add(b);
    fr.add(p);

}
4

2 回答 2

1

Add a keylistener to the button (working with KeyBindings would be better).

Make two int variables for the indices of the current panel (e.g. x,y).

In the keyPressed(KeyEvent e) method remove the button from the current panel and add it to the new panel.

Note: The button must have focus, if you want to move it with the keys.

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

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

public class MyFrame extends JFrame {

    private JPanel [][] innerCells;
    private JButton b = new JButton("G");
    private int x=0;
    private int y=0;
    private final int size=10;

    public MyFrame() {
        JFrame fr = new JFrame("Final Exams");
        fr.setSize(800, 600);

        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.setVisible(true);
        fr.setLocationRelativeTo(null);

        JPanel p = new JPanel(new GridLayout(size, size));

        innerCells = new JPanel[size][size];

        for(int i=0;i<size;i++){
            for(int j=0;j<size;j++){
                innerCells[i][j] = new JPanel();
                innerCells[i][j].setBorder(BorderFactory.createLineBorder(Color.orange));
               p.add(innerCells[i][j]);
            }
        }
        innerCells[0][0].add(b);
        fr.add(p);  

        b.addKeyListener(new KListener());
    }

    private class KListener extends KeyAdapter{
        public void keyPressed(KeyEvent e){
            innerCells[x][y].remove(b);
            innerCells[x][y].repaint();
            int keyCode = e.getKeyCode();
            switch( keyCode ) { 
                case KeyEvent.VK_UP:
                    x= x-1;
                        break;

                case KeyEvent.VK_DOWN:
                    x= (x+1)%size;
                        break;

                case KeyEvent.VK_LEFT:
                    y= y-1;
                        break;
                case KeyEvent.VK_RIGHT :
                    y= (y+1)%size; 
                        break;
            }
            if(x<0) x=size-1;
            if(y<0)y=size-1;
            innerCells[x][y].add(b);

            innerCells[x][y].revalidate();
            b.requestFocus();
    }
   ´        }

    public static void main(String[] args){
            MyFrame f = new MyFrame();
    }
}
于 2013-06-05T10:37:55.810 回答
0

伙计们,我终于解决了这个问题!这是解决方案..我已经使用 KeyListener 完成了它,但我认为正如@MadProgrammer 所说,使用键绑定更容易!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Askhsh6 {

    int gr, st;
    Dimension dim;

    public Askhsh6() {
        final JFrame fr1 = new JFrame("a Title");
        fr1.setSize(800, 600);
        fr1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr1.setVisible(true);
        fr1.setResizable(false);

        final JButton b = new JButton("G");
        dim = new Dimension(70, 50);
        b.setSize(dim);
        b.setFocusable(false);
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                b.validate();
                b.repaint();
                boolean pop = b.isFocusOwner();

                pop = b.isFocusOwner();
                System.out.println("" + pop);
            }
        });
        final JPanel[][] p;
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(8, 6));

        p = new JPanel[8][6];

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 6; j++) {
                p[i][j] = new JPanel();
                p[i][j].setLayout(new BorderLayout());

                p[i][j].setBorder(BorderFactory.createLineBorder(Color.orange));
                p1.add(p[i][j]);

            }
        }
        p[0][0].add(b, BorderLayout.CENTER);
        gr = st = 0;

        fr1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent ke) {
                if (ke.getKeyCode() == KeyEvent.VK_DOWN) {
                    System.out.println("DOWN");
                    if (gr + 1 < 8) {
                        p[gr][st].remove(b);
                        gr = gr + 1;
                        p[gr][st].add(b, BorderLayout.CENTER);

                        fr1.repaint();
                    }
                }
                if (ke.getKeyCode() == KeyEvent.VK_UP) {
                    System.out.println("UP");
                    if (gr - 1 >= 0) {
                        p[gr][st].remove(b);
                        gr = gr - 1;
                        p[gr][st].add(b, BorderLayout.CENTER);

                        fr1.repaint();
                    }
                }
                if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
                    System.out.println("LEFT");
                    if (st - 1 >= 0) {
                        p[gr][st].remove(b);
                        st = st - 1;
                        p[gr][st].add(b, BorderLayout.CENTER);

                        fr1.repaint();
                    }
                }
                if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
                    System.out.println("RIGHT");
                    if (st + 1 < 6) {
                        p[gr][st].remove(b);
                        st = st + 1;
                        p[gr][st].add(b, BorderLayout.CENTER);

                        fr1.repaint();
                    }
                }
            }
        });
        fr1.add(p1);
        fr1.validate();
    }
}
于 2013-06-18T11:53:00.003 回答