Swing-MaskFormatter-从文本字段的右侧输入数字 [英] Swing - MaskFormatter - Enter Numbers from Right side of the textfield

查看:120
本文介绍了Swing-MaskFormatter-从文本字段的右侧输入数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swing Java开发的新手.有人可以帮我吗?

I'm new to Swing Java development. Can some one help me on this.

我有一个带maskformatter的jformattedtextfield.它工作正常.但是我唯一想知道的是,是否可以使它从右边输入数字.下面的代码可以很好地从左至右输入数字.

I have a jformattedtextfield with maskformatter. 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.

谢谢您的时间.

这是我拥有的Java代码:

Here is the java code i have:

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

这篇关于Swing-MaskFormatter-从文本字段的右侧输入数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆