首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swing - MaskFormatter -从文本字段的右侧输入数字

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

Stack Overflow用户
提问于 2012-12-01 01:11:08
回答 1查看 1.7K关注 0票数 5

我是Swing Java开发的新手。有人能帮我这个忙吗。

我有一个带有maskformatter的jformattedtextfield文本字段。它工作得很好。但我唯一想知道的是,我们是否可以从正确的位置输入数字。下面的代码可以很好地从左到右输入数字。

谢谢您抽时间见我。

下面是我的java代码:

代码语言:javascript
复制
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);
            }
        });
    }
}
EN

回答 1

Stack Overflow用户

发布于 2012-12-01 01:16:51

您可以使用:

代码语言:javascript
复制
timeField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13649810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档