public class MyPanel extends JPanel implements MouseMotionListener,KeyListener{
int x;
int y;
char ch;
public MyPanel(){
setFocusable(true); //设置该组件是否可以获得焦点
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString(String.valueOf(ch),x, y);
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
ch=e.getKeyChar();
repaint();
}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
x=e.getX();
y=e.getY();
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
}
在鼠标位置显示字母 java
最新推荐文章于 2022-07-24 00:36:38 发布