java键盘事件响应问题
初学Java,看到键盘响应事件这里出现了个小问题。这是书上一个动态时钟的小例子,在实现运动的时钟后我想试试键盘事件的响应,用上下左右键改变时钟的位置。但是响应不了键盘事件...
初学Java,看到键盘响应事件这里出现了个小问题。 这是书上一个动态时钟的小例子,在实现运动的时钟后我想试试键盘事件的响应,用上下左右键改变时钟的位置。但是响应不了键盘事件,求帮忙解答解答
// 这个是StillClock.java
public class StillClock extends JPanel
{
private int hour;
private int minute;
private int second;
int xCenter = 0;
int yCenter = 0;
public StillClock()
{
setCurrentTime();
}
public StillClock(int hour, int minute, int second)
{
this.hour = hour;
this.second = second;
this.minute = minute;
}
/*中间一些get和set方法省略*/
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
int Radius = (int)(Math.min(getWidth(), getHeight())* 0.8 * 0.5 );
xCenter = getWidth() / 2;
yCenter = getHeight() / 2;
/*这中间一些具体绘制时钟的代码省略*/
}
public void setCurrentTime()
{
Calendar cal = new GregorianCalendar();
this.hour = cal.get(Calendar.HOUR_OF_DAY);
this.minute = cal.get(Calendar.MINUTE);
this.second = cal.get(Calendar.SECOND);
}
}
//这个是MoveClock.java
public class MoveClock extends StillClock
{
public MoveClock()
{
addKeyListener(new KeyAdapter()
{
public void KeyPressed(KeyEvent e)
{
switch(e.getKeyCode())
{
case KeyEvent.VK_UP: xCenter -= 10;System.out.println("VK_UP"); break;
case KeyEvent.VK_RIGHT: xCenter += 10; break;
case KeyEvent.VK_LEFT: xCenter -= 10; break;
case KeyEvent.VK_DOWN: yCenter += 10; break;
default : break;
}
repaint();
}
});
Timer timer = new Timer(1000,new TimerListener());
timer.start();
}
private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
setCurrentTime();
repaint();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("Move Clock");
MoveClock mvk = new MoveClock();
frame.add(mvk);
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} 展开
// 这个是StillClock.java
public class StillClock extends JPanel
{
private int hour;
private int minute;
private int second;
int xCenter = 0;
int yCenter = 0;
public StillClock()
{
setCurrentTime();
}
public StillClock(int hour, int minute, int second)
{
this.hour = hour;
this.second = second;
this.minute = minute;
}
/*中间一些get和set方法省略*/
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
int Radius = (int)(Math.min(getWidth(), getHeight())* 0.8 * 0.5 );
xCenter = getWidth() / 2;
yCenter = getHeight() / 2;
/*这中间一些具体绘制时钟的代码省略*/
}
public void setCurrentTime()
{
Calendar cal = new GregorianCalendar();
this.hour = cal.get(Calendar.HOUR_OF_DAY);
this.minute = cal.get(Calendar.MINUTE);
this.second = cal.get(Calendar.SECOND);
}
}
//这个是MoveClock.java
public class MoveClock extends StillClock
{
public MoveClock()
{
addKeyListener(new KeyAdapter()
{
public void KeyPressed(KeyEvent e)
{
switch(e.getKeyCode())
{
case KeyEvent.VK_UP: xCenter -= 10;System.out.println("VK_UP"); break;
case KeyEvent.VK_RIGHT: xCenter += 10; break;
case KeyEvent.VK_LEFT: xCenter -= 10; break;
case KeyEvent.VK_DOWN: yCenter += 10; break;
default : break;
}
repaint();
}
});
Timer timer = new Timer(1000,new TimerListener());
timer.start();
}
private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
setCurrentTime();
repaint();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("Move Clock");
MoveClock mvk = new MoveClock();
frame.add(mvk);
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} 展开
3个回答
展开全部
The method KeyPressed(KeyEvent) from the type new KeyAdapter(){} is never used,
you make a mistake k for K,maybe a slip of the pen
the correct style is:
//-------------------------------------------------
@Override
public void keyPressed(KeyEvent e)
you make a mistake k for K,maybe a slip of the pen
the correct style is:
//-------------------------------------------------
@Override
public void keyPressed(KeyEvent e)
追问
改了之后还是没有响应啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你使用了集成开发环境了没有?
追问
用的eclipse
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询