我有一个KeyListener内部类,该类返回一个错误,指出“不是抽象的,并且不会覆盖抽象方法”

时间:2019-06-19 07:35:09

标签: java keylistener

我有一个包含大约10个JTextField的JPanel,我希望能够在键盘上单击向上或向下箭头时将焦点设置在特定的文本字段上。因此,如果将中间的文本字段选中并单击向上箭头,则焦点将移至其上方的文本字段;同样,如果单击向下箭头,则其焦点将位于其下方的文本字段。我创建了Key Listener的内部类,该类的模式类似于我想做的ActionListener类。当我说

时似乎出现了错误

panelEncodeMain.HandleKeyListener is not abstract and does not override abstract method keyReleased(KeyEvent) in KeyListener

有人可以帮助我解决问题吗?预先感谢。

public panelEncodemain()
{
    //setBackground(Color.WHITE);
    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    HandleControlButton control = new HandleControlButton();
    HandleKeyListener keycontrol = new HandleKeyListener();

    btn_testing = new JButton("Test Focus");
    btn_testing.addActionListener(control);

    lbl_page = new JLabel("Page :");
    lbl_line = new JLabel("Line :");
    lbl_refpage = new JLabel("Ref. Page :");
    lbl_refline = new JLabel("Ref. Line :");
    lbl_transdate = new JLabel("Transaction Date :");
    lbl_desc = new JLabel("Description :");
    lbl_accountcode = new JLabel("Account Code :");
    lbl_codetype = new JLabel("Code Type :");
    lbl_issuedate = new JLabel("Issue Date :");
    lbl_amount = new JLabel("Amount :");

    lbl_page.setFont(new Font("Arial", Font.PLAIN, 20));
    lbl_line.setFont(new Font("Arial", Font.PLAIN, 20));
    lbl_refpage.setFont(new Font("Arial", Font.PLAIN, 20));
    lbl_refline.setFont(new Font("Arial", Font.PLAIN, 20));
    lbl_transdate.setFont(new Font("Arial", Font.PLAIN, 20));
    lbl_desc.setFont(new Font("Arial", Font.PLAIN, 20));
    lbl_accountcode.setFont(new Font("Arial", Font.PLAIN, 20));
    lbl_codetype.setFont(new Font("Arial", Font.PLAIN, 20));
    lbl_issuedate.setFont(new Font("Arial", Font.PLAIN, 20));
    lbl_amount.setFont(new Font("Arial", Font.PLAIN, 20));

    text_page = new JTextField(7);
    text_line = new JTextField(5);
    text_refpage = new JTextField(40);
    text_refline = new JTextField(40);
    text_transdate = new JTextField(10);
    text_desc = new JTextField(40);
    text_desc.addKeyListener(keycontrol);
    text_accountcode = new JTextField(5);
    text_codetype = new JTextField(5);
    text_issuedate = new JTextField(10);
    text_amount = new JTextField(20);


    text_page.setFont(new Font("Arial", Font.PLAIN, 20));
    text_line.setFont(new Font("Arial", Font.PLAIN, 20));
    text_refpage.setFont(new Font("Arial", Font.PLAIN, 20));
    text_refline.setFont(new Font("Arial", Font.PLAIN, 20));
    text_transdate.setFont(new Font("Arial", Font.PLAIN, 20));
    text_desc.setFont(new Font("Arial", Font.PLAIN, 20));
    text_accountcode.setFont(new Font("Arial", Font.PLAIN, 20));
    text_codetype.setFont(new Font("Arial", Font.PLAIN, 20));
    text_issuedate.setFont(new Font("Arial", Font.PLAIN, 20));
    text_amount.setFont(new Font("Arial", Font.PLAIN, 20));


    //text_desc.setEditable(false);

    //first row

    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(5,0,0,10);
    c.anchor = GridBagConstraints.LINE_END;
    add(lbl_page, c);

    c.gridx = 1;
    c.anchor = GridBagConstraints.LINE_START;
    add(text_page, c);

    //second row

    c.gridy++;

    c.anchor = GridBagConstraints.LINE_END;
    c.gridx=0;
    add(lbl_line, c);

    c.gridx = 1;
    c.anchor = GridBagConstraints.LINE_START;
    add(text_line, c);

    //third row

    c.gridy++;

    c.anchor = GridBagConstraints.LINE_END;
    c.gridx=0;
    add(lbl_refpage, c);

    c.gridx = 1;
    c.anchor = GridBagConstraints.LINE_START;
    add(text_refpage, c);

    //fourth row

    c.gridy++;

    c.anchor = GridBagConstraints.LINE_END;
    c.gridx=0;
    add(lbl_refline, c);

    c.gridx = 1;
    c.anchor = GridBagConstraints.LINE_START;
    add(text_refline, c);

    //fifth row

    c.gridy++;

    c.anchor = GridBagConstraints.LINE_END;
    c.gridx=0;
    add(lbl_transdate, c);

    c.gridx = 1;
    c.anchor = GridBagConstraints.LINE_START;
    add(text_transdate, c);

    //sixth row

    c.gridy++;

    c.anchor = GridBagConstraints.LINE_END;
    c.gridx=0;
    add(lbl_desc, c);

    c.gridx = 1;
    c.anchor = GridBagConstraints.LINE_START;
    add(text_desc, c);

    //seventh row

    c.gridy++;

    c.anchor = GridBagConstraints.LINE_END;
    c.gridx=0;
    add(lbl_accountcode, c);

    c.gridx = 1;
    c.anchor = GridBagConstraints.LINE_START;
    add(text_accountcode, c);

    //eighth row

    c.gridy++;

    c.anchor = GridBagConstraints.LINE_END;
    c.gridx=0;
    add(lbl_codetype, c);

    c.gridx = 1;
    c.anchor = GridBagConstraints.LINE_START;
    add(text_codetype, c);

    //ninth row

    c.gridy++;

    c.anchor = GridBagConstraints.LINE_END;
    c.gridx=0;
    add(lbl_issuedate, c);

    c.gridx = 1;
    c.anchor = GridBagConstraints.LINE_START;
    add(text_issuedate, c);

    //tenth row

    c.gridy++;

    c.anchor = GridBagConstraints.LINE_END;
    c.gridx=0;
    add(lbl_amount, c);

    c.gridx = 1;
    c.anchor = GridBagConstraints.LINE_START;
    add(text_amount, c);

    //tenth row

    c.gridy++;

    c.gridx = 1;
    c.anchor = GridBagConstraints.LINE_END;
    add(btn_testing, c);

}

class HandleControlButton implements ActionListener
{
    public void actionPerformed (ActionEvent e)
    {
        Object source = e.getSource();

        if(source == btn_testing)
        {
                text_desc.requestFocus();    
        }
    }
}

class HandleKeyListener implements KeyListener
{
    public void keyTyped(KeyEvent x)
    {
        System.out.println(x);
    }
}
}

我希望能够使用键盘上的向上或向下按钮将焦点从一个文本字段转移到下一个文本字段。

1 个答案:

答案 0 :(得分:2)

就像注释中提到的那样,该错误表示您必须从KeyListener接口实现所有方法。它有三种方法

  • keyTyped(您已经实现)
  • keyPressed(未实现)
  • KeyReleased(未实现)

因此您可以为这些方法添加一个空的实现,但是在这种情况下,最简单的方法是扩展类KeyAdapter而不是实现KeyListener(因为KeyAdapter实现了KeyListener,并且仅实现了空方法,这正是您需要这里)

因此您可以将KeyListener类更改为此:

class HandleKeyListener extends KeyAdapter {
    public void keyTyped(KeyEvent x) {
        System.out.println(x);
    }
}

然后它应该工作。