这是我的班级以正确的方式显示土耳其里拉金额,我的班级没有设置输入的金额,例如,如果我输入1200它应该是1,200 TL,我是否会错过这里的步骤?我应该添加动作执行事件还是密钥释放事件?
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();
}
}
答案 0 :(得分:4)
该字段在具有焦点时不会被格式化,尝试向UI添加另一个组件并将焦点标记为它。
这意味着在"编辑"模式,它将使用编辑器格式化程序,但是当它不是
时将使用显示格式化程序
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();
}
}