写的这个Java小例子不能响应键盘事件是怎么回事? 10
publicclassframeextendsJFrame{JLabellb=newJLabel("hello");publicframe(){this.setSize(...
public class frame extends JFrame{
JLabel lb = new JLabel("hello");
public frame() {
this.setSize(700, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(lb);
lb.addKeyListener(new kyeMonitor());
this.setVisible(true);
}
public static void main(String[] args) {
frame f = new frame();
}
private class kyeMonitor extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
lb.setText("Enter text " );
}
}
}
} 展开
JLabel lb = new JLabel("hello");
public frame() {
this.setSize(700, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(lb);
lb.addKeyListener(new kyeMonitor());
this.setVisible(true);
}
public static void main(String[] args) {
frame f = new frame();
}
private class kyeMonitor extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
lb.setText("Enter text " );
}
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询