哪位大虾,看看我程序错在哪里,改了好几次都没弄出来(程序的目的是从键盘接受字符串并把它显示在面板上,回车键表示字符结束。当输入新字符串的时候,将它显示在面板上)。程序如下,请各位大虾指正:
import java.awt.*;
import java.lang.*;
import java.awt.event.*;
import javax.swing.*;
public class DisplayKeyString extends JFrame{
public DisplayKeyString(){
add(new KeyString());
}
public static void main(String[] args){
DisplayKeyString frame=new DisplayKeyString();
frame.setTitle("DisplayKeyString");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(220,80);
frame.setVisible(true);
}
private static class KeyString extends JPanel{
StringBuffer string=new StringBuffer("please press the keyborad!");
public KeyString(){
addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
if(e.getKeyCode()!=KeyEvent.VK_ENTER)string.append(e.getKeyChar());
else repaint();
}
});
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString(string.toString(),0,0);
string=new StringBuffer();
}
}
}
import java.awt.*;
import java.lang.*;
import java.awt.event.*;
import javax.swing.*;
public class DisplayKeyString extends JFrame{
public DisplayKeyString(){
add(new KeyString());
}
public static void main(String[] args){
DisplayKeyString frame=new DisplayKeyString();
frame.setTitle("DisplayKeyString");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(220,80);
frame.setVisible(true);
}
private static class KeyString extends JPanel{
StringBuffer string=new StringBuffer("please press the keyborad!");
public KeyString(){
addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
if(e.getKeyCode()!=KeyEvent.VK_ENTER)string.append(e.getKeyChar());
else repaint();
}
});
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString(string.toString(),0,0);
string=new StringBuffer();
}
}
}
这么多问题。
第一。字符串展示的位置~~太靠上了。y坐标应该是字符串所在区域的左下坐标,你写0的话,就跑到窗口外面去了,看不到。
第二,你需要给你的JPanel
setFocusable(true);如果没有这个,面板是无法接受键盘事件的,你家的监听也就无法触发。这是最关键的一点。
第三,你改写的方法是适配器中的keyTyped方法。改成keyPressed方法。不然对于事件的处理不对。keyTyped中e.getKeyCode都是0,Enter的code是10.你用这个方法无法立即刷新。需要用其他方式刷新才能看到效果,比如alt+tab。当然,pressed也有问题,比如按下功能键时候的处理。所以建议将处理方法分开到两个方法中。
以下属于无关紧要的:
第四,你改写的是paintComponent方法,一般这样的操作,改写paint(Graphics g)比较多。paintComponent方法是画组建用的
第五,public DisplayKeyString() {
add(new KeyString());
}
一般来说,应该是getContentPane().add(new KeyString());比较规范
public class DisplayKeyString extends JFrame{
public DisplayKeyString(){
KeyString ks=new KeyString();
add(ks);
addKeyListener(ks.ka);
}
public static void main(String[] args){
DisplayKeyString frame=new DisplayKeyString();
frame.setTitle("DisplayKeyString");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(220,80);
frame.setVisible(true);
}
private static class KeyString extends JPanel{
StringBuffer string=new StringBuffer("please press the keyborad!");
KeyAdapter ka=null;////////
public KeyString(){
ka=new KeyAdapter(){
public void keyTyped(KeyEvent e){
if(e.getKeyCode()!=KeyEvent.VK_ENTER)string.append(e.getKeyChar());
else repaint();
}
}
addKeyListener(ka);
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString(string.toString(),0,0);
string=new StringBuffer();
}
}
}
g.drawString(string.toString(),0,0);
这句的坐标需要调整,0,0是左上角,drawString出来的内容在这个上方,需要往下移动一点