不知道如何在JFormattedTextField上修复我的PropertyChangeListener

编程入门 行业动态 更新时间:2024-10-25 14:26:03
本文介绍了不知道如何在JFormattedTextField上修复我的PropertyChangeListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 import java.awt.EventQueue; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.text.NumberFormat; import javax.swing.JFormattedTextField; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.text.NumberFormatter; public class Test{ private JFormattedTextField input, input2; private NumberFormatter formatter; private PropertyChangeListener listener; public Test(){ formatter = new NumberFormatter(NumberFormat.getNumberInstance()); input = new JFormattedTextField(formatter); input2 = new JFormattedTextField(formatter); listener = new PropertyChangeListener(){ @Override public void propertyChange(PropertyChangeEvent evt) { convert(evt); } }; input.setColumns(4); input2.setColumns(4); input.addPropertyChangeListener("value", listener); input2.addPropertyChangeListener("value", listener); input.setValue(0.0); JPanel panel = new JPanel(); panel.add(input); panel.add(input2); JOptionPane.showMessageDialog(null, panel); } private void convert(PropertyChangeEvent evt){ if (evt.getSource()== input){ if (evt.getSource()!= null){ double temp; temp = converter((Double)evt.getNewValue()); input2.setValue(temp); } } } public static void main(String[] args) { EventQueue.invokeLater(new Runnable(){ public void run(){ new Test(); } }); } private double converter(double value){ value = value*2; return value; } }

堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double at test.Test.convert(Test.java:46)

我的想法

因为我有一个传递 double 的方法(这是 convert()),看似 evt.getNewValue()返回直接值,在输入时是技术上是一个长,它抛出了这个错误。

My thoughts

Because I have a method that passes in a double (it's convert()) , and seemingly evt.getNewValue() returns the direct value, which at the time of input is technically a long, it's throwing that error.

但每次尝试解析 我的 evt.getNewValue()到一个双无效。也许对我正在尝试用这个程序做的一点知识会有所帮助。

But every attempt at parsing my evt.getNewValue() to a double hasn't worked. Perhaps a little knowledge of what I'm trying to do with this program would help.

所以我有 JPanel (在 tabbedPane )有两个 JFormattedTextField 输入。这是一个转换应用程序。我的转换类方法传入 double 并返回 double 。我希望将字段链接在一起,换句话说,只要一个字段的输入被更改,另一个字段的输入就会被更改(就像它是转换的输出一样)。

So I've got a JPanel (in a tabbedPane) that has two JFormattedTextField inputs. It's a conversion application. My conversion class method passes in a double and returns a double. I'd like the fields to be linked together, or in other words, as soon as one field's input is changed the other changes with it (as in it's the output of the conversion).

我正在考虑废弃 PropertyChangListener 并选择 DocumentListener 相反,但选择先尝试前者,因为后者有3 可覆盖 方法我必须照顾,其中一个可能会导致一些意想不到的结果(突出显示和删除该字段会触发两个事件)。

I was considering scrapping the PropertyChangListener and going for a DocumentListener instead, but opted to try the former first as the latter has 3 overrideable methods I have to take care of, one of which might cause some unexpected results (highlighting and deleting the field would trigger two events for example).

有没有更好的方法来获得动态更新的双输入字段应用程序?在一个字段中输入一个数字,另一个字段的数字自动更新。

Is there a better way of getting a dynamically updating, dual input field application? Input one number into one field and the other field's number automatically updates.

仍然是Java的新手。

Still a novice at Java.

我找到了一个临时解决方案:拥有 DecimalFormat 作为 JFormattedTextField 中的格式。但是如果没有小数也可以工作我也很喜欢。

I've found a temporary solution: Have a DecimalFormat as the format in the JFormattedTextField. But if it could work without having a decimal as well I'd love it.

问题已解答,没意识到 evt.getNewValue()返回 Number 实例。

Question answered, didn't realize evt.getNewValue() was returning a Number instance.

推荐答案

所有你知道的是返回的对象evt.getNewValue()是一个 Number 对象。如果你利用这些信息并按照以下方式尝试,那该怎么办:

All you know for sure is that the object returned by evt.getNewValue() is a Number object. What if you use that information to your advantage and try something along these lines:

temp = ((Number)evt.getNewValue()).doubleValue();

更多推荐

不知道如何在JFormattedTextField上修复我的PropertyChangeListener

本文发布于:2023-10-24 13:32:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524082.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   JFormattedTextField   PropertyChangeListener

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!