在java中写入unicode(Sindhi)的keyListener的实现问题 [英] issue with implementation of keyListener for writting unicode (Sindhi) in java
问题描述
我想通过这种方式在 jTextField
上实现keyListener来使用unicode:
I want to use unicode through the implementation of keyListener on jTextField
in this way :
textField.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent evt) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent evt) {
// TODO Auto-generated method stub
char var = evt.getKeyChar();
if(var == 'a'){
String values = urlTextField.getText() + Sindhi.ALIF;
urlTextField.setText(values);
}
}
});
但写入英文
字符 a
, unicode
字符 Sindhi.ALIF
。如何只获取用 jTextField写的
unicode
字符
but it writes English
charactera
with unicode
character Sindhi.ALIF
. how to get only the unicode
character written in jTextField
推荐答案
无论您当前的问题是什么,都不应该在JTextField中使用KeyListener。请改用DocumentListener或DocumentFilter。鉴于您的代码,我猜测DocumentFilter是您需要的,因为您希望在输入时以及在显示之前更改JTextField的文本。
Regardless of your current problem, you shouldn't be using a KeyListener in a JTextField. Use a DocumentListener or DocumentFilter instead. Given your code, I'm guessing that a DocumentFilter is what you need, since you wish to change the text of the JTextField as it is being entered and before it is being displayed.
例如,
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
public class SwapAForAleph {
// No idea of the correct unicode for this!!!
public static final char SINDHI_ALIF = '\u0623';
public static void main(String[] args) {
final JTextField textField = new JTextField(10);
textField.setFont(textField.getFont().deriveFont(32f));
PlainDocument doc = (PlainDocument) textField.getDocument();
doc.setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int offset, String text,
AttributeSet attr) throws BadLocationException {
text = filterText(text);
super.insertString(fb, offset, text, attr);
}
@Override
public void replace(FilterBypass fb, int offset, int length,
String text, AttributeSet attrs) throws BadLocationException {
text = filterText(text);
super.replace(fb, offset, length, text, attrs);
}
private String filterText(String text) {
return text.replace('a', SINDHI_ALIF);
}
});
JPanel panel = new JPanel();
panel.add(textField);
JOptionPane.showMessageDialog(null, panel);
}
}
或以其他方式看待......
Or looked at in another way...
import java.awt.ComponentOrientation;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
public class NonEnglishTextField {
public static final char ALEPH = '\u05D0';
public static void main(String[] args) {
final JTextField textField = new JTextField(20);
textField.setFont(textField.getFont().deriveFont(32f));
textField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
textField.setHorizontalAlignment(SwingConstants.RIGHT);
PlainDocument doc = (PlainDocument) textField.getDocument();
doc.setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int offset, String text,
AttributeSet attr) throws BadLocationException {
text = filterText(text);
super.insertString(fb, offset, text, attr);
}
@Override
public void replace(FilterBypass fb, int offset, int length,
String text, AttributeSet attrs) throws BadLocationException {
text = filterText(text);
super.replace(fb, offset, length, text, attrs);
}
private String filterText(String text) {
StringBuilder sb = new StringBuilder();
for (char c : text.toLowerCase().toCharArray()) {
if (c >= 'a' && c <= 'z') {
char newChar = (char) (c - 'a' + ALEPH);
sb.append(newChar);
} else {
sb.append(c);
}
}
return sb.toString();
}
});
JPanel panel = new JPanel();
panel.add(textField);
JOptionPane.showMessageDialog(null, panel);
}
}
这篇关于在java中写入unicode(Sindhi)的keyListener的实现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!