2012-11-30 26 views
5

Tôi mới phát triển Swing Java. Có ai có thể giúp tôi với cái này.Swing - MaskFormatter - Nhập số từ bên phải của trường văn bản

Tôi có một jformattedtextfield với biểu tượng mặt nạ. nó hoạt động tốt. Nhưng điều duy nhất tôi muốn biết là nếu chúng ta có thể làm điều này để nhập các số từ bên phải. Mã dưới đây hoạt động tốt để nhập các số từ trái sang phải.

Cảm ơn bạn đã dành thời gian.

Đây là đoạn mã java i có:

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

Bạn đã thử các [ 'setComponentOrientation (ComponentOrientation o)'] (http://docs.oracle .com/javase/1.4.2/docs/api/javax/swing/JSpinner.html) phương pháp? – 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

Cảm ơn Brian và fireshadow52. Nó hoạt động tốt. Nhưng tôi thấy một vấn đề nhỏ. Vị trí caret khó hiểu chút ít. Tôi mong đợi để xem vị trí caret ở cuối tất cả các thời gian. Thay vào đó nó hiển thị ngay từ đầu. Ngoài ra tôi quan sát thấy định dạng số sau khi tôi nhập 3 chữ số 149 (tôi mong đợi để thấy rằng là 1:49 nhưng thay vào đó nó hiển thị nó là 14: 9) – Steve

Trả lời

4

Bạn có thể sử dụng:

timeField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
+0

Cảm ơn bạn Reimeus. Nó hoạt động tốt. Nhưng tôi thấy một vấn đề nhỏ. Vị trí caret khó hiểu chút ít. Tôi mong đợi để xem vị trí caret ở cuối tất cả các thời gian. Thay vào đó nó hiển thị ngay từ đầu. Ngoài ra tôi quan sát thấy định dạng số sau khi tôi nhập 3 chữ số 149 (tôi mong đợi để xem đó là 1:49 nhưng thay vào đó nó hiển thị nó là 14: 9) – Steve

+0

Đây là hành vi mặc định của 'JFormattedTextField'. Bạn có thể có một cái nhìn tại gọi [setCaretPosition] (http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#setCaretPosition%28int%29) từ bên trong một [ DocumentListener] (http://docs.oracle.com/javase/7/docs/api/javax/swing/event/DocumentListener.html) đã đăng ký với thành phần. – Reimeus

Các vấn đề liên quan