有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Swing中定义格式化文本字段,以便在键入时应用格式限制?

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

  1. 将输入限制为小数点后两位数的数字 点
  2. 列出每三位数字分隔的数字,即1 234 567,89.
  3. 数字立即以正确的格式显示 打字时

我尝试使用JFormattedTextField:

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

但是,格式限制仅在焦点离开组件时适用,这是一个问题。我希望用户能够在字段中只键入数字和delimeter,并在字段中输入每个三维数字后放置一个空格delimeter

我可以在C#中通过指定字段的属性来实现这一点。在Swing中,什么是干净的方法? 多谢各位


共 (2) 个答案

  1. # 1 楼答案

    在字段中添加一个KeyListener并在键入键时重新格式化如何

  2. # 2 楼答案

    • 其中一种方法是使用InternationalFormatter,这是Number or DecimalFormatter

    • JFormattedTextField中的每个限制对用户输入都有副作用,例如CaretSelection等。

    • 适当的方法可以是使用组合DocumentFilter加上DocumentListener加上JFormattedTextField加上普通Number or DecimalFormatter,没有任何限制,所有的解决方法都将在DocumentFilter(指定用于Document内的更改)和DocumentListener(文档外)

    例如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();
                }
            });
        }
    }
    

    例如JSpinner (editor is JFormattedTextField) with very simple DocumentFilter