简体   繁体   English

Swing - MaskFormatter - 从文本字段的右侧输入数字

[英]Swing - MaskFormatter - Enter Numbers from Right side of the textfield

I'm new to Swing Java development. 我是Swing Java开发的新手。 Can some one help me on this. 有人可以帮我这个。

I have a jformattedtextfield with maskformatter. 我有一个带有maskformatter的jformattedtextfield。 it works just fine. 它工作得很好。 But only thing i would like to know is if we can make this to enter the numbers from right. 但我唯一想知道的是,如果我们能够从右边输入数字。 The below code works just fine to enter the numbers from left to right. 以下代码可以很好地从左到右输入数字。

Thank you for your time. 感谢您的时间。

Here is the java code i have: 这是我的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);
            }
        });
    }
}

你可以使用:

timeField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM