1

我想以jTextField这种方式通过 keyListener 的实现来使用 unicode:

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);
            }
        }
    });

但它用字符写English字符。如何只获取写入的字符aunicodeSindhi.ALIFunicodejTextField

4

2 回答 2

4

不管您当前的问题是什么,您都不应该在 JTextField 中使用 KeyListener。请改用 DocumentListener 或 DocumentFilter。鉴于您的代码,我猜想 DocumentFilter 是您所需要的,因为您希望在输入 JTextField 时和显示之前更改它的文本。

例如,

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);
   }
}

或者换个角度看...

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);
   }
}
于 2013-09-28T04:49:16.493 回答
2

使用文档过滤器。

当 KeyListener 接收到事件时,角色已经被添加到字段中。

KeyListener 也不会处理用户将内容粘贴到字段中

于 2013-09-28T04:50:08.787 回答