作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含大约 10 个 JTextField 的 JPanel,我希望在单击键盘上的向上或向下箭头后能够将焦点设置在特定文本字段上。因此,如果中间的文本字段获得焦点,并且单击向上箭头,则焦点将转到其上方的文本字段,同样,如果单击向下箭头,焦点将转到其下方的文本字段。我创建了一个 Key Listener 的内部类,其模式类似于我的 ActionListener 类,我认为我可以做到。当我编译它时似乎出现错误
panelEncodeMain.HandleKeyListener 不是抽象的,不会重写 KeyListener 中的抽象方法 keyReleased(KeyEvent)
。
谁能帮我解决问题吗?提前致谢。
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);
}
}
}
我希望能够使用键盘上的向上或向下按钮将焦点从一个文本字段转移到下一个文本字段。
最佳答案
就像评论中提到的,该错误意味着您必须实现 KeyListener 接口(interface)中的所有方法。它有三个方法
因此,您可以为这些方法添加一个空实现,但在这种情况下,最简单的方法是扩展类 KeyAdapter而不是实现 KeyListener (因为 KeyAdapter 实现了 KeyListener 并且只实现了空方法,这正是您在这里需要的)
因此您可以将 KeyListener 类更改为:
class HandleKeyListener extends KeyAdapter {
public void keyTyped(KeyEvent x) {
System.out.println(x);
}
}
然后它应该可以工作。
关于java - 我有一个 KeyListener 的内部类,它返回一个错误,提示 "is not abstract and does not override abstract method",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56662378/
我是一名优秀的程序员,十分优秀!