JPanel中的键盘输入

时间:2014-04-23 19:33:03

标签: java keyboard focus jpanel keylistener

我在JPanel中收听键盘输入时遇到问题。我几乎尝试了一切!请帮帮我这个。我将非常感激。

这是我的代码:

public class SudokuBoardDisplay extends JPanel implements Printer, KeyListener{

private static final long serialVersionUID = 1L;
    private Dimension size = new Dimension(500,100);
    private SudokuBoard game;
    private List<JButton> fields = new ArrayList<JButton>();
    private int clickedX = -1;
    private int clickedY = -1;
    private int userValue = -1;

public SudokuBoardDisplay(SudokuBoard board) {
    this.game = board;

    this.setLayout(new GridBagLayout());

    JButton picLabel;

    for(int i=0; i<SudokuBoard.TAB_SIZE*SudokuBoard.TAB_SIZE; i++){
        picLabel = new JButton();
        picLabel.setPreferredSize(new Dimension(50,50));
        picLabel.setFont(new Font("Serif", Font.PLAIN, 40));
        picLabel.setBorder(BorderFactory.createDashedBorder(getForeground()));
        picLabel.setActionCommand(Integer.toString(i));
        fields.add(picLabel);
    }

    for(JButton button : fields){
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                for(int y=0; y<SudokuBoard.TAB_SIZE; y++){
                    for(int x=0; x<SudokuBoard.TAB_SIZE; x++){
                        if(e.getActionCommand().equals(Integer.toString(x+y*SudokuBoard.TAB_SIZE))){
                            clickedX=x;
                            clickedY=y;
                            draw();
                            revalidate();
                            repaint();
                        }
                    }
                }
            }
        });
    }


    this.addKeyListener(this);
    this.setFocusable(true);
    this.requestFocusInWindow();

    draw();

    this.setVisible(true);
}

public void keyPressed(KeyEvent e) {
    int userValue = Character.getNumericValue(e.getKeyChar());
    System.out.println(userValue);
    //((SudokuBoardDisplay)(printer)).setUserValue(userValue);
    //this.requestFocusInWindow();
}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

}

}

public class MainWindow extends JFrame{

public MainWindow(){
    this.setLayout(new FlowLayout());

    btnSolve = new JButton("Solve");
    printer = new SudokuBoardDisplay(game);
    dao = DaoFactory.createDao("WorkingDirectory/SAVE");
    dao.write(game);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(600,650);
    setResizable(false);

    btnSolve.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0) {
            SudokuSolver solver = new BacktrackingSudokuSolver();
            try {
                game = solver.solve(game);
            } catch (SolutionHasBeenFound e) {
            }
            printer.show(game);

        }
    });
    add(btnSolve);


    btnSave = new JButton("Save");
    btnSave.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent arg0) {
             dao.write(game);
        }

    });
    add(btnSave);

    btnLoad = new JButton("Load");
    btnLoad.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent arg0) {
             game = (SudokuBoard) dao.read();
             printer.show(game);
        }

    });
    add(btnLoad);

    btnNew = new JButton("New");

    btnNew.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0) {
            game = new SudokuBoard(5);
            printer.show(game);
        }

    });


    add(btnNew);

    add((JPanel) printer);

    setVisible(true);
}

}

我删除了不相关的代码片段。

即使我尝试请求专注于JPanel,焦点仍然是“解决”按钮。

2 个答案:

答案 0 :(得分:0)

致电((JPanel) printer).requestFocus();

setVisible(true);

之后

有关详细信息,请查看JComponent#requestFocus()

答案 1 :(得分:0)

考虑使用Key Bindings,它们可以更好地控制焦点级别要求,以便生成关键事件,这与KeyListener不同,{{1}}必须注册到既可以集中注意力又能集中注意力的组件命令它举起关键事件