2013-09-28 87 views
1

書面方式的Unicode(信德),我想通過KeyListener的實施上jTextField以這種方式使用unicode:問題與執行的KeyListener的在Java

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

,但它與unicode字符Sindhi.ALIF寫道:English字符a。如何獲得unicode字符jTextField

回答

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

確定這對我的作品非常感謝:-) –

+0

但如何比較和替換其他字符? –

+0

@ArshadAliSoomro:你寫的代碼來做到這一點。拍,我已經展示了兩種不同的方式在上面的代碼示例來做到這一點!退房'filterText(...)'方法的兩種不同的實現。 –

2

使用某個DocumentFilter。

通過的KeyListener收到
事件的時候,性格已經被添加到該領域。

另外的KeyListener不會處理與用戶的內容粘貼到現場