import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class TextKeyTest extends JPanel implements DocumentListener{
private JTextField text;
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.add(new TextKeyTest());
frame.setVisible(true);
}
public TextKeyTest(){
text = new JTextField();
text.getDocument().addDocumentListener(this);
this.setLayout(new BorderLayout());
this.add(text,BorderLayout.CENTER);
}
public void insertUpdate(DocumentEvent e) {
try{
String s=e.getDocument().getText(e.getOffset(),e.getLength());
System.out.println(s);
}catch(BadLocationException xe){
System.out.println(xe.getMessage());
}
}
public void removeUpdate(DocumentEvent e) {
}
public void changedUpdate(DocumentEvent e) {
}
}
我输入“你好”,输出:
这个拼音是怎么回事,怎么去掉,我输入“你好”,输出应该是“你”、“好”啊。
Java swing
------解决方案--------------------
System.setProperty("java.awt.im.style","below-the-spot");
可以解决这个问题。不过