JAVA drawString问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA drawString问题相关的知识,希望对你有一定的参考价值。
我想做一个这样的程序:
监听键盘按键,除了按上、下、左、右方向键外,按其他键,窗口上就显示其他键值。按方向键,就移动窗口上的字符
比如我打开程序,按了一个Z,然后窗口上就显示Z,我再按方向键,就可以移动这个Z
可是我现在发现,X、Y的坐标值是可以根据我按方向键而变化的,就是按了屏幕上没反应,总是显示程序一开始时默认的画面
程序代码如下
import java.awt.event.*;import javax.swing.*;import java.awt.*;public class Test extends JFrame public static void main(String[] args) JFrame frame=new JFrame("title"); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setSize(300,300); frame.setVisible(true); frame.addKeyListener(new KeyboardPanel()); frame.getContentPane().add(new KeyboardPanel()); class KeyboardPanel extends JPanel implements KeyListener private int x = 100; private int y = 100; private char keyChar='A'; public void keyRelessed(KeyEvent e) public void keyPressed(KeyEvent e) switch(e.getKeyCode()) case KeyEvent.VK_DOWN: y+=10;break; case KeyEvent.VK_UP: y-=10;break; case KeyEvent.VK_LEFT:x-=10;break; case KeyEvent.VK_RIGHT: x+=10;break; default:keyChar=e.getKeyChar(); protected void paintComponent(Graphics g) super.paintComponent(g); g.setFont(new Font("TimesRoman",Font.PLAIN,24)); g.setColor(Color.black); g.drawString(String.valueOf(keyChar), x, y); System.out.println("x="+x+" y="+y);//输出测试一下 @Override public void keyReleased(KeyEvent arg0) @Override public void keyTyped(KeyEvent arg0)
你的程序我帮你改好了,你看看吧.完整的程序如下:
import java.awt.event.*;import javax.swing.*;
import java.awt.*;
public class Test extends JFrame
public static void main(String[] args)
JFrame frame=new JFrame("title");
KeyboardPanel kp=new KeyboardPanel();
frame.getContentPane().add(kp);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
kp.requestFocus();//把焦点移到KeyboardPanel上
//frame.addKeyListener(new KeyboardPanel());//这里去掉,把监听加到KeyboardPanel上
class KeyboardPanel extends JPanel implements KeyListener
private int x = 100;
private int y = 100;
private char keyChar='A';
KeyboardPanel()
addKeyListener(this);//把监听加到KeyboardPanel上
public void keyRelessed(KeyEvent e)
public void keyPressed(KeyEvent e)
switch(e.getKeyCode())
case KeyEvent.VK_DOWN: y+=10;break;
case KeyEvent.VK_UP: y-=10;break;
case KeyEvent.VK_LEFT:x-=10;break;
case KeyEvent.VK_RIGHT: x+=10;break;
default:keyChar=e.getKeyChar();
repaint();//这里刷新KeyboardPanel
protected void paintComponent(Graphics g)
super.paintComponent(g);
g.setFont(new Font("TimesRoman",Font.PLAIN,24));
g.setColor(Color.black);
g.drawString(String.valueOf(keyChar), x, y);
System.out.println("x="+x+" y="+y);//输出测试一下
@Override
public void keyReleased(KeyEvent arg0)
@Override
public void keyTyped(KeyEvent arg0)
参考技术A case KeyEvent.VK_DOWN: y+=10;break;
case KeyEvent.VK_UP: y-=10;break;
case KeyEvent.VK_LEFT:x-=10;break;
case KeyEvent.VK_RIGHT: x+=10;break;
default:keyChar=e.getKeyChar();
坐标改变之后应该要调用一下repaint()方法
比如case KeyEvent.VK_DOWN: y+=10;repaint();break;
调用repaint方法之前可能还要擦除原图像。。
以上是关于JAVA drawString问题的主要内容,如果未能解决你的问题,请参考以下文章
Java AWT Graphics更改drawString()的背景颜色[重复]
java Graphics2D drawString()内容换行问题