如何在Swing中定义格式化的文本字段,以便在键入时应用格式限制? [英] How to define a formatted text field in Swing so that the format restrictions are applied as you type?

查看:257
本文介绍了如何在Swing中定义格式化的文本字段,以便在键入时应用格式限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JFrame上需要一个这样的数字文本字段

I need such a numeric text field on JFrame that

  1. 用十进制后两位数字限制输入 点
  2. 布局每三位数字分隔的数字,即1 234 567,89.
  3. 该号码立即以正确的格式显示 在输入过程中
  1. restricts the input by a number with two digits after decimal point
  2. lays out the number separating every three digits i.e. 1 234 567,89.
  3. the number is displayed in correct format right away during typing

我尝试使用JFormattedTextField:

I tried using JFormattedTextField:

    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMaximumFractionDigits(2);
    NumberFormatter numFormater = new NumberFormatter(nf);
    field1 = new javax.swing.JFormattedTextField(numFormater);

但是,格式限制仅在焦点离开组件时才适用,这是一个问题.我希望用户只能在字段中键入数字和分度符,并在字段中输入每个3 d数字后立即放置空格分度符.

However, format restricitions apply only when focus leaves the component, which is a problem. I want user to be able to type only digits and delimeter in the field, and a space delimeter be placed right after each 3-d digit is entered in the field.

我只需指定字段的属性就可以在C#中做到这一点.在Swing中执行此操作的干净方法是什么? 谢谢.

I can do this in C# just by specifing properties of the field. What is a clean way to do this in Swing? Thank you.

推荐答案

  • 方法之一是使用InternationalFormatter,这是Number or DecimalFormatter

    每个限制都会给用户输入带来副作用,例如CaretSelection

    every restriction in JFormattedTextField has side effects for users input, e.g. Caret, Selection, etc.

    正确的方法可以是将DocumentFilterDocumentListener的组合添加到具有普通Number or DecimalFormatterJFormattedTextField中,而没有任何限制,所有变通办法都将设置在两个DocumentFilter中(指定用于更改) Document内部)和DocumentListener(文档外部)

    proper of ways could be usage of combinations DocumentFilter with DocumentListener added to JFormattedTextField with plain Number or DecimalFormatter, without any restrictions, all workaround will be set in both DocumentFilter(designated for changes inside Document) with DocumentListener(outside of Document)

    例如提到的InternationalFormatter

    import java.awt.*;
    import java.math.*;
    import java.text.*;
    import javax.swing.*;
    import javax.swing.JFormattedTextField.AbstractFormatter;
    import javax.swing.JFormattedTextField.AbstractFormatterFactory;
    import javax.swing.text.InternationalFormatter;
    
    public class DocumentListenerAdapter {
    
        public DocumentListenerAdapter() {
            JFrame frame = new JFrame("AbstractTextField Test");
            final JFormattedTextField textField1 = new JFormattedTextField(new Float(10.01));
            textField1.setFormatterFactory(new AbstractFormatterFactory() {
                @Override
                public AbstractFormatter getFormatter(JFormattedTextField tf) {
                    NumberFormat format = DecimalFormat.getInstance();
                    format.setMinimumFractionDigits(2);
                    format.setMaximumFractionDigits(2);
                    format.setRoundingMode(RoundingMode.HALF_UP);
                    InternationalFormatter formatter = new InternationalFormatter(format);
                    formatter.setAllowsInvalid(false);
                    formatter.setMinimum(0.0);
                    formatter.setMaximum(1000.00);
                    return formatter;
                }
            });
            NumberFormat numberFormat = NumberFormat.getNumberInstance();
            numberFormat.setMaximumFractionDigits(2);
            numberFormat.setMaximumFractionDigits(2);
            numberFormat.setRoundingMode(RoundingMode.HALF_UP);
            final JFormattedTextField textField2 = new JFormattedTextField(numberFormat);
            textField2.setValue(new Float(10.01));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(textField1, BorderLayout.NORTH);
            frame.add(textField2, BorderLayout.SOUTH);
            frame.setVisible(true);
            frame.pack();
        }
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new DocumentListenerAdapter();
                }
            });
        }
    }
    

    例如具有非常简单的DocumentFilter的JSpinner(编辑器是JFormattedTextField)

    这篇关于如何在Swing中定义格式化的文本字段,以便在键入时应用格式限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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