2012-11-30 19 views
5

私はSwing Java開発の新人です。私はこれを手伝ってくれますか?Swing - MaskFormatter - テキストフィールドの右側から数値を入力してください

私はmaskformatterでjformattedtextfieldを持っています。それはうまく動作します。しかし、私が知りたいのは、右から数字を入力することができるかどうかだけです。以下のコードは、数字を左から右に入力するのにうまく機能します。

ありがとうございます。ここで

は私が持っているJavaのコードは次のとおりです。

あなたが使用することができ
public class MaskFormattedTextExample extends JFrame { 

    private static final long serialVersionUID = -1212313123; 

    JFormattedTextField timeField; 

    public MaskFormattedTextExample() { 
     initComponents(); 
    } 

    private void initComponents() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(new Dimension(200, 200)); 
     getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT)); 

     MaskFormatter mask = null; 
     try { 
      mask = new MaskFormatter("##:##:##"); 
      mask.setPlaceholderCharacter('_'); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     timeField = new JFormattedTextField(mask); 
     timeField.setHorizontalAlignment(JTextField.RIGHT); 
     timeField.setCaretPosition(JTextField.RIGHT); 

     getContentPane().add(timeField); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 
       new MaskFormattedTextExample().setVisible(true); 
      } 
     }); 
    } 
} 
+0

あなたは(http://docs.oracle [ '' setComponentOrientation(ComponentOrientationを0)]みました.com/javase/1.4.2/docs/api/javax/swing/JSpinner.html)メソッド? – fireshadow52

+0

['Component.setComponentOrientation(ComponentOrientation)'](http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html#setComponentOrientation%28java.awt.ComponentOrientation%29)FTFY @ fireshadow52 :) – Brian

+0

ブライアンとfireshadow52ありがとうございます。それは良い作品です。しかし、小さな問題が1つあります。キャレットの位置はちょっと混乱しています。私はいつも最後にキャレットポジションを見たいと思う。代わりに最初に表示されます。また、3桁の数字を入力した後の数字のフォーマットは149です(1:49と表示されますが、14:9と表示されます) – Steve

答えて

4

timeField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
+0

Reimeusありがとうございます。それは良い作品です。しかし、小さな問題が1つあります。キャレットの位置はちょっと混乱しています。私はいつも最後にキャレットポジションを見たいと思う。代わりに最初に表示されます。また、3桁の数字を入力した後の数字の書式は149です(1:49と表示されますが、14:9と表示されます) – Steve

+0

これは 'JFormattedTextField'のデフォルト動作です。あなたは、[setCaretPosition](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#setCaretPosition%28int%29)を新しい[ DocumentListener](http://docs.oracle.com/javase/7/docs/api/javax/swing/event/DocumentListener.html)をコンポーネントに登録します。 – Reimeus

関連する問題