gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:17:45 26 4
gpt4 key购买 nike

这是我的类(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com