java - 当输入金额和土耳其里拉 "TL"图标丢失时,JFormattedTextField 不设置值

标签 java swing jformattedtextfield

这是我的类(class),用于以正确的方式显示土耳其里拉金额,我的类(class)没有设置输入的金额,例如,如果我输入 1200,它应该是 1,200 里拉,我会错过这里的步骤吗?我应该添加 Action 执行事件还是按键释放事件?

public class TurkisliraFormatterDemo extends JPanel
        implements PropertyChangeListener {

    private double amount = 100000;
    private JFormattedTextField amountField;

    private NumberFormat amountDisplayFormat;
    private NumberFormat amountEditFormat;

    public TurkisliraFormatterDemo() {
        super(new BorderLayout());
        setUpFormats();

        amountField = new JFormattedTextField(
                new DefaultFormatterFactory(
                        new NumberFormatter(amountDisplayFormat),
                        new NumberFormatter(amountDisplayFormat),
                        new NumberFormatter(amountEditFormat)));
        amountField.setValue(new Double(amount));
        amountField.setColumns(10);
        amountField.addPropertyChangeListener("value", this);

        JPanel fieldPane = new JPanel(new GridLayout(0, 1));
        fieldPane.add(amountField);

        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        add(fieldPane, BorderLayout.LINE_END);
    }

    public void propertyChange(PropertyChangeEvent e) {
        Object source = e.getSource();
        if (source == amountField) {
            amount = ((Number) amountField.getValue()).doubleValue();
            amountField.setValue(amount);
        }

    }

    private static void createAndShowGUI() {

        JFrame frame = new JFrame("FormatDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(new TurkisliraFormatterDemo());

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                UIManager.put("windows", Boolean.FALSE);
                createAndShowGUI();
            }
        });
    }

    private void setUpFormats() {
        amountDisplayFormat = NumberFormat.getCurrencyInstance(new Locale("tr", "TR"));
        amountDisplayFormat.setMinimumFractionDigits(0);
        amountEditFormat = NumberFormat.getNumberInstance();

    }
}

最佳答案

该字段在具有焦点时不会被格式化,尝试将另一个组件添加到 UI 并将焦点切换到它。

这意味着在“编辑”模式下,它将使用编辑器格式化程序,但在不使用时将使用显示格式化程序

Field

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

public class TurkisliraFormatterDemo extends JPanel
                implements PropertyChangeListener {

    private double amount = 100000;
    private JFormattedTextField amountField;

    private NumberFormat amountDisplayFormat;
    private NumberFormat amountEditFormat;

    public TurkisliraFormatterDemo() {
        super(new BorderLayout());
        setUpFormats();

        amountField = new JFormattedTextField(
                        new DefaultFormatterFactory(
                                        new NumberFormatter(amountDisplayFormat),
                                        new NumberFormatter(amountDisplayFormat),
                                        new NumberFormatter(amountEditFormat)));
        amountField.setValue(new Double(amount));
        amountField.setColumns(10);
        amountField.addPropertyChangeListener("value", this);

        JPanel fieldPane = new JPanel(new GridLayout(0, 1));
        fieldPane.add(amountField);

        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        add(fieldPane, BorderLayout.LINE_END);
        add(new JButton("Hello"), BorderLayout.SOUTH);
    }

    public void propertyChange(PropertyChangeEvent e) {
        Object source = e.getSource();
        if (source == amountField) {
            amount = ((Number) amountField.getValue()).doubleValue();
            System.out.println("amount = " + amount);
//            amountField.setValue(amount);
        }

    }

    private static void createAndShowGUI() {

        JFrame frame = new JFrame("FormatDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(new TurkisliraFormatterDemo());

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                UIManager.put("windows", Boolean.FALSE);
                createAndShowGUI();
            }
        });
    }

    private void setUpFormats() {
        amountDisplayFormat = NumberFormat.getCurrencyInstance(new Locale("tr", "TR"));
        System.out.println(amountDisplayFormat.format(1200));
        amountDisplayFormat.setMinimumFractionDigits(0);
        amountEditFormat = NumberFormat.getNumberInstance();

    }
}

关于java - 当输入金额和土耳其里拉 "TL"图标丢失时,JFormattedTextField 不设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25500167/

相关文章:

Java - JFormattedTextField 不允许首次尝试输入

java - jformattedtextfield 四舍五入数字

java - 如何使 JTextField (Eclipse Window Builder) 只读

java - Swing 图形不会显示 - Java

java - 绘制形状时 JPanel 中 Canvas 的清除按钮

java - 将图像包装到 Jframe

java - 有人可以帮我制作一个适用于我的代码的 JFormattedTextField 吗?

java - Google Play 服务(RESULT_LICENSE_FAILED)

java - TreeMap 自定义比较器

java - 在 JSP 中显示包含 csv 数据的 java 对象的最佳方式?