Java中的KeyListener没有响应

时间:2014-12-11 19:05:50

标签: java swing keylistener

我必须使用JCheckBoxes和JButton开发一些简单的Java应用程序。为了完成,我必须控制每当我按下数字键(0 - 9)以便JChechBox改变其状态(未选择<>选择)。

我在这里附上我的代码。我已完成窗口,但是当我按下按键时,没有任何反应......

HELP !!!

public class Window extends JFrame {

    public static JCheckBox check1;
    private static JCheckBox check2;
    private static JCheckBox check3;
    private static JCheckBox check4;
    private static JCheckBox check5;
    private static JCheckBox check6;
    private static JCheckBox check7;
    private static JCheckBox check8;
    private static JCheckBox check9;
    private static JCheckBox check10;

    public static void main( String[] args ){
        JFrame frame = new JFrame( "Window");
        frame.setSize( 600, 600 );
        frame.setVisible( true );
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JButton open = new JButton("OPEN"); 
        open.setLocation( 330, 340 );
        open.setSize( 85, 30 );
        frame.add( open ); 

        JButton save = new JButton("SAVE"); 
        save.setLocation( 480, 340 );
        save.setSize( 85, 30 );
        frame.add( save); 

        check1 = new JCheckBox("option1");
        check1.setLocation( 25, 60 );
        check1.setSize( 146, 30 );
        frame.add( check1 );


        check2 = new JCheckBox("option2"); 
        check2.setLocation( 25, 90 );
        check2.setSize( 146, 30 );
        frame.add( check2 );

        check3 = new JCheckBox("option3");
        check3.setLocation( 25, 120 );
        check3.setSize( 146, 30 );
        frame.add( check3 );

        check4 = new JCheckBox("option4");
        check4.setLocation( 25, 150 );
        check4.setSize( 146, 30 );
        frame.add( check4 );        

        check5 = new JCheckBox("option5");
        check5.setLocation( 25, 180 );
        check5.setSize( 146, 30 );
        frame.add( check5 );

        check6 = new JCheckBox("option6");
        check6.setLocation( 25, 210);
        check6.setSize( 146, 30 );
        frame.add( check6 );        

        check7 = new JCheckBox("option7");
        check7.setLocation( 25, 240 );
        check7.setSize( 146, 30 );
        frame.add( check7 );

        check8 = new JCheckBox("option8");
        check8.setLocation( 25, 270 );
        check8.setSize( 146, 30 );
        frame.add( check8 );

        check9 = new JCheckBox("option9");
        check9.setLocation( 25, 300 );
        check9.setSize( 146, 30 );
        frame.add( check9 );

        check10 = new JCheckBox("option10");
        check10.setLocation( 25, 330);
        check10.setSize( 146, 30 );
        frame.add( check10 );

        TextArea area = new TextArea();
        area.setLocation( 320, 150 );
        area.setSize(250, 180);
        frame.add( area );

        frame.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyReleased(KeyEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyPressed(KeyEvent arg0) {
                int keyCode = arg0.getKeyCode();

                switch (keyCode) {
                case KeyEvent.VK_1:
                    check1.setSelected(true);
                    break;
                case KeyEvent.VK_2:
                    check2.setSelected(true);
                    break;
                case KeyEvent.VK_3:
                    check3.setSelected(true);
                    break;
                case KeyEvent.VK_4:
                    check4.setSelected(true);
                    break;
                case KeyEvent.VK_5:
                    check5.setSelected(true);
                    break;
                case KeyEvent.VK_6:
                    check6.setSelected(true);
                    break;
                case KeyEvent.VK_7:
                    check7.setSelected(true);
                    break;
                case KeyEvent.VK_8:
                    check8.setSelected(true);
                    break;
                case KeyEvent.VK_9:
                    check9.setSelected(true);
                    break;
                case KeyEvent.VK_0:
                    check10.setSelected(true);
                    break;
                }               
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

最简单的方法是使用JCheckBox.setMneumonic,但是用户必须按alt-1,alt-2等。如果您的要求是严格的,我建议使用KeyBoardFocusManager并实现一个KeyEventDispatcher来指导按键点击复选框

答案 1 :(得分:0)

JavaDocs说:

  

要触发键盘事件,组件必须具有键盘焦点。

在您的情况下,键盘焦点位于TextArea。因此,您必须将KeyListener添加到Textarea

area.addKeyListener(new KeyListener(){
       //Your code
}

您可以使用JFrame将注意力设置在frame.setFocusable(true)上,并且只要您将注意力转移到TextArea上就可以正常工作。