Java 当输入的金额和土耳其里拉“TL”图标丢失时,JFormattedTextField未设置值
这是我的类,用于以正确的方式显示土耳其里拉金额,我的类没有设置输入的金额。例如,如果我输入1200,它应该是1200 TL,我是否错过了此处的步骤?我应该添加操作执行事件还是密钥释放事件Java 当输入的金额和土耳其里拉“TL”图标丢失时,JFormattedTextField未设置值,java,swing,jformattedtextfield,Java,Swing,Jformattedtextfield,这是我的类,用于以正确的方式显示土耳其里拉金额,我的类没有设置输入的金额。例如,如果我输入1200,它应该是1200 TL,我是否错过了此处的步骤?我应该添加操作执行事件还是密钥释放事件 public class TurkisliraFormatterDemo extends JPanel implements PropertyChangeListener { private double amount = 100000; private JFormattedT
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添加另一个组件并将焦点标记到其中 这意味着在编辑模式下,它将使用编辑器格式化程序,但在不使用时将使用显示格式化程序
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();
}
}
该字段具有焦点时将不会格式化,请尝试向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();
}
}
所以当我点击hello时,焦点会变成hello按钮?是的,如果你重新聚焦字段,它的格式会再次变成编辑器格式…我可以使用鼠标运动事件来影响我的字段吗?因为我不想把按钮放在我的GUI上,我们可以隐式点击不可见的按钮吗?在焦点丢失和设置1200 TL后,还有一个问题,如果我想获取值并存储到数据库中,是变量字符串还是大小数?所以当我单击hello时,焦点会更改为hello按钮?是的,如果重新聚焦字段,它的格式将再次更改为编辑器格式…我是否可以使用鼠标运动事件来获取字段影响,因为我不想将按钮置于我的GUI中?我们是否可以隐式单击不可见的按钮?在焦点丢失和设置1200 TL后,还有一个问题,如果我想获取值并存储到数据库中,是变量字符串还是大小数?