Java JFormattedTextField删除用户输入
我有以下代码:Java JFormattedTextField删除用户输入,java,swing,user-interface,jformattedtextfield,Java,Swing,User Interface,Jformattedtextfield,我有以下代码: public test() { setLayout(new FlowLayout()); JFormattedTextField price = new JFormattedTextField(new DecimalFormat("#,##0.00 \u00A4")); price.setValue(new Float(105.00)); add(price); add(new JButton("Ok")); pack();
public test() {
setLayout(new FlowLayout());
JFormattedTextField price = new JFormattedTextField(new DecimalFormat("#,##0.00 \u00A4"));
price.setValue(new Float(105.00));
add(price);
add(new JButton("Ok"));
pack();
setVisible(true);
}
如果我现在输入一个数字,例如“20”。文本字段再次输入“105.00”。
为什么它不接受我的输入并始终返回默认值?您的十进制格式工作不正常 尝试删除 在那之后20欧元就行了
<>但请注意,在编号 之后,您必须输入空白和符号。您的十进制格式不能正常工作。 尝试删除 在那之后20欧元就行了
胡:在数字 之后,你还必须输入空白和符号。从最简单的三个开始,结束与无用的,也许是废话,与第一和第二点比较。 使用NumberFormat.getCurrencyInstance;或NumberFormat.getCurrencyInstanceLocale;对于货币符号,则此值对具体的JTextField/JFormattedTextField/JSpinner/XxxCellRenderer/XxxCellEditor有效, 代码 应用自己的NavigationFilter,在JTextField/JFormattedTextField/JSpinner/XxxCellRenderer/XxxCellEditor中,固定位置可能位于位置偏差的开始或结束位置, 代码 使用InputVerifier创建自己的InputMask,对于JTextField/JFormattedTextField/JSpinner/XxxCellRenderer/XxxCellEditor中的每一个,解决方法可能/不可能不同,
与第一点和第二点相比,有三种方法以最简单开始,以无用结束,也许是胡说八道 使用NumberFormat.getCurrencyInstance;或NumberFormat.getCurrencyInstanceLocale;对于货币符号,则此值对具体的JTextField/JFormattedTextField/JSpinner/XxxCellRenderer/XxxCellEditor有效, 代码 应用自己的NavigationFilter,在JTextField/JFormattedTextField/JSpinner/XxxCellRenderer/XxxCellEditor中,固定位置可能位于位置偏差的开始或结束位置, 代码 使用InputVerifier创建自己的InputMask,对于JTextField/JFormattedTextField/JSpinner/XxxCellRenderer/XxxCellEditor中的每一个,解决方法可能/不可能不同,
当输入无效时,它将恢复为旧值。代码正确输入20$将为您提供20.00$,如果这是您想要的。。输入20而不输入$将使您恢复原始值我可以输入任何我想要的值20$、20$、20€、20€,该值始终恢复为旧值。输入无效时,它将恢复为旧值。代码正确输入20$将使您获得20.00$,如果您想要的话。。输入20而不输入$将使您恢复原始值我可以输入任何我想要的值20$、20$、20€、20€,该值始终恢复为旧值。
import java.awt.GridLayout;
import java.math.RoundingMode;
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.text.NumberFormatter;
public class MaskFormatterTest {
public static void main(String[] args) throws Exception {
//NumberFormat format = NumberFormat.getNumberInstance();
NumberFormat format = NumberFormat.getCurrencyInstance();
format.setMaximumFractionDigits(2);
format.setMinimumFractionDigits(2);
format.setParseIntegerOnly(true);
format.setRoundingMode(RoundingMode.HALF_UP);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setMaximum(1000.0);
formatter.setMinimum(0.0);
formatter.setAllowsInvalid(false);
formatter.setOverwriteMode(false);
JFormattedTextField tf = new JFormattedTextField(formatter);
tf.setColumns(10);
tf.setValue(123456789.99);
JFormattedTextField tf1 = new JFormattedTextField(formatter);
tf1.setValue(1234567890.99);
JFormattedTextField tf2 = new JFormattedTextField(formatter);
tf2.setValue(1111.1111);
JFormattedTextField tf3 = new JFormattedTextField(formatter);
tf3.setValue(-1111.1111);
JFormattedTextField tf4 = new JFormattedTextField(formatter);
tf4.setValue(-56);
JFrame frame = new JFrame("Test");
frame.setLayout(new GridLayout(5, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tf);
frame.add(tf1);
frame.add(tf2);
frame.add(tf3);
frame.add(tf4);
frame.pack();
frame.setVisible(true);
}
}
//@see & read http://stackoverflow.com/questions/7421337/limited-selection-in-a-jtextfield-jtextcomponent
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class NavigationFilterPrefixWithBackspace extends NavigationFilter {
private int prefixLength;
private Action deletePrevious;
public NavigationFilterPrefixWithBackspace(int prefixLength, JTextComponent component) {
this.prefixLength = prefixLength;
deletePrevious = component.getActionMap().get("delete-previous");
component.getActionMap().put("delete-previous", new BackspaceAction());
component.setCaretPosition(prefixLength);
}
@Override
public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
fb.setDot(Math.max(dot, prefixLength), bias);
}
@Override
public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
fb.moveDot(Math.max(dot, prefixLength), bias);
}
class BackspaceAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
JTextComponent component = (JTextComponent) e.getSource();
if (component.getCaretPosition() > prefixLength) {
deletePrevious.actionPerformed(null);
}
}
}
public static void main(String args[]) throws Exception {
JTextField textField = new JTextField("Prefix_", 20);
textField.setNavigationFilter(new NavigationFilterPrefixWithBackspace(7, textField));
JFrame frame = new JFrame("Navigation Filter Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(textField);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}