- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经创建了 JFormattedTextField 的自定义组件 (NumberFormattedTextField)。这是我使用的格式化程序:
public static InternationalFormatter getDecimalIFormatter92() {
// For Buy & Sale Price
DecimalFormat numberFormat = (DecimalFormat) DecimalFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(2);
numberFormat.setMinimumFractionDigits(2);
numberFormat.setRoundingMode(RoundingMode.HALF_UP);
numberFormat.setGroupingUsed(false);
final InternationalFormatter formatter = new InternationalFormatter(numberFormat);
formatter.setAllowsInvalid(false);
formatter.setMinimum(0.00);
formatter.setMaximum(999999999.99);
return formatter;
}
当我创建 NumberFormattedTextField 实例时:
public RelationsPanel(CashParent parent) {
try {
initComponents(); // startBalTxt = new NumberFormattedTextField();
Utility.logInfo("initComponents OVER");
myParent = parent;
nameTxt.setSizeLimit(20);
System.out.println("startBalTxt.setFormatter GOING FOR IT");
this.startBalTxt.setFormatter(Utility.getDecimalIFormatter82()); // Must be throwing here
System.out.println("startBalTxt.setFormatter DONE");
this.currentBalTxt.setFormatter(Utility.getDecimalIFormatter82());
} catch (Exception e) {
Utility.logInfo("Failed to Initialize : " + e.getMessage());
e.printStackTrace();
}
}
NumberFormattedTextField 类代码:
public class NumberFormattedTextField extends JFormattedTextField implements java.io.Serializable, DocumentListener, FocusListener {
private DecimalFormat numberFormat;
private InternationalFormatter formatter;
private double MAX_VALUE = 999999.99;
private final Map attributes = (Utility.getTextFont()).getAttributes();
/**
* Creates a NumberFormattedTextField with 6 Integer & 2 Fractional digits.
* Minimum is set to 0.00 and Max to 999999.99 without any grouping used.
*/
public NumberFormattedTextField() {
super();
createFormatter(); // Creates a default formatter, used if formatter is not set
this.setValue(0.00);
init();
}
private void createFormatter() {
// Create standard DecimalFormat
numberFormat = (DecimalFormat) DecimalFormat.getNumberInstance();
numberFormat.setMaximumFractionDigits(2);
numberFormat.setMinimumFractionDigits(2);
numberFormat.setRoundingMode(RoundingMode.HALF_UP);
numberFormat.setGroupingUsed(false);
formatter = new InternationalFormatter(numberFormat);
formatter.setAllowsInvalid(false);
formatter.setMinimum(0.00);
formatter.setMaximum(999999.99);
this.setFormatterFactory(new AbstractFormatterFactoryImpl());
}
private void init() {
setFont(Utility.getTextFont());
this.getDocument().addDocumentListener(this);
this.addFocusListener(this);
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
setFocusLostBehavior(PERSIST);
}
public void setFormatter(InternationalFormatter format) {
super.setFormatter(format);
System.out.println("Class of Format = " + format.getFormat().getClass());
if (format.getFormat() instanceof java.text.DecimalFormat)
numberFormat = (DecimalFormat)format.getFormat();
else
numberFormat = (DecimalFormat)(NumberFormat) format.getFormat();
formatter = format;
// AbstractFormatterFactoryImpl returns formatter straight away
this.setFormatterFactory(new AbstractFormatterFactoryImpl());
calculateMaxValue();
}
private void calculateMaxValue() {
try {
if (formatter.getMaximum() != null) {
//System.out.println(" MAX ALlowed = " + formatter.getMaximum());
String no = formatter.valueToString(formatter.getMaximum());
char seperator = java.text.DecimalFormatSymbols.getInstance().getGroupingSeparator();
no = no.replace(String.valueOf(seperator), "");
System.out.println("MAX Number to PArse = " + no);
MAX_VALUE = Double.parseDouble(no); // HERE ITS THROWING EXCEPTION
}
} catch (ParseException ex) {
Logger.getLogger(NumberFormattedTextField.class.getName()).log(Level.SEVERE, null, ex);
}
}
上述代码在我的电脑上运行良好。但是在客户端的 PC 上它给出了错误: 初始化失败:对于输入字符串:“99999999,99”我得到的错误/日志:
startBalTxt.setFormatter GOING FOR IT
INFO: initComponents OVER
Class of Format = class java.text.DecimalFormat
MAX Number to PArse = 99999999,99
INFO: Failed to Initialize : For input string: "99999999,99"
java.lang.NumberFormatException: For input string: "99999999,99"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
at java.lang.Double.parseDouble(Double.java:540)
at cashaccountingapp.components.NumberFormattedTextField.calculateMaxValue(NumberFormattedTextField.java:184)
Class of Format = class java.text.DecimalFormat
at cashaccountingapp.components.NumberFormattedTextField.setFormatter(NumberFormattedTextField.java:173)
at cashaccountingapp.data.RelationsPanel.<init>(RelationsPanel.java:35)
我们两个的语言环境不同。
这个错误的原因是什么?在我的 NumberFormattedTextField 类中需要注意的不同区域设置或其他内容。我希望应用程序适应系统设置并相应地显示。
如何解决问题??
最佳答案
最有可能(不能完全确定,因为您没有显示初始设置并且仅将错误削弱到其消息中;-)不同的区域设置是原因:一个将句点作为小数点,另一个将逗号。因此,如果您使用表示另一个中的数字的字符串初始化一个,它将发生冲突:逗号未被解释为小数点分隔符,因此最大值太大而无法满足限制
关于Java : Does Locale affect the Formatter for JFormattedTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8635480/
我向我的记录器添加了两个具有不同格式化程序的处理程序。第一个需要继承 logging.Formatter 来进行自定义格式化。默认格式化程序将满足第二个处理程序的需要。 假设第一个格式化程序只是从消息
我尝试运行一个简单的 .feature 文件以生成 Step 文件所需的代码,但返回以下异常: 下面是pom文件: org.seleniumhq.selenium
userA 在法国,他们将某样东西的价格定为 10 欧元,我获取他们的货币信息,并使用 Locale.current.identifier 将其本地区域的副本记录到数据库,这给出了我 € 并将其保存到
我已经在 android 项目上集成了 checkstyle。但是我收到以下错误: > Unable to create listeners: formatters {[com.puppycrawl.
这个解决方案对我不起作用: Running a spec in RubyMine results in "cannot load such file -- teamcity/spec/runner/f
我正在尝试在 Eclipse 中设置我的第一个 Cucumber。我按照教程视频建议的方式在 Features 文件夹中设置了一个基本功能文件,但尝试作为 cucumber 功能执行时,我得到以下信息
操作系统:Arch Linux,Rails 版本:4,RubyMine:6.3 当我从 Tools - Run Rake Task - spec 运行规范时,我总是会收到此错误: /home/chyl
我正在尝试使用 formattable Awesome 包并获得一个表格,其中包含在多列上缩放的百分比和颜色。 这里是代码 set.seed(123) df % mutate_if(is.nume
我试图将字符串定位在一行上的某个起始位置,无论前一个文本在哪里结束。例如: Mandatory arguments to long options are mandatory for short op
因此,我将以下代码写入文件: Formatter output = ....... // Creating the formatter works, writes to appropriate fil
我知道有很多 HTML 格式化程序。我已经尝试了很多,但没有一个符合我的要求。请建议您是否遇到了可以: 1) 转换 BBCC 进入 BB
在本文中,我们将介绍 Java Formatter.format 方法。 java.util.Formatter 类用作 printf 样式格式字符串的解释器。 Formatter 可以执行布局对齐、
我有一个简单的验证器,可以根据正则表达式检查输入: .directive('test', function () { return { restrict: 'A', require:
假设您有一些表格数据,其中数据长度可能会有所不同,Formatter 类是否有自动调整填充的规定? 所以不是这个(注意 A 列): columnA columnB 1
我有一个这样注释的模型: [DataMember(EmitDefaultValue = false)] [JsonProperty("extension_{0}_name")] public stri
示例数据(修改自 formattable github 文档): df <- data.frame( id = 1:10, name = c("Bob", "Ashley", "James",
我编写了下面的代码,它创建了一个文件并完美地写入它,但我想在输出中查看文件的内容,但我只收到此消息:“java.io.BufferedWriter@140e19d”。我不明白!谁能向我解释一下为什么我
我有兴趣使用 formattable R 包中的工具,但我只想在表格中显示有更改的地方。也就是说,我想要通过 collapse_rows() 函数在 kableExtra 包中提供分层行标签。 例如,
我已经开始使用 Intellij (之前使用过 Eclipse),但我似乎无法模仿 Eclipse 格式 Eclipse Code Formatter 插件完成了大部分工作,但仍然存在一些差异。 例如
我是编码新手,有一个问题如何修复此代码以实际使用用户输入作为创建的文件的新名称。如您所见,我希望在格式化程序中使用字符串“文件名”(例如,输入是“Example.txt”,这应该在格式化程序的构造函数
我是一名优秀的程序员,十分优秀!