Java JFormattedTextField和NumberFormat用于浮点运算

Java JFormattedTextField和NumberFormat用于浮点运算,java,swing,number-formatting,jformattedtextfield,Java,Swing,Number Formatting,Jformattedtextfield,我用JFormattedTextField创建了一个小对话框,输入一个介于0和10之间的浮点数,最多3个十进制数。我使用NumberFormat和PropertyChangeListener来验证值或返回旧值。但它不起作用: public class IRCompensationDialog extends JDialog{ private static final long serialVersionUID = 1L; private JDialog irDialog; private J

我用JFormattedTextField创建了一个小对话框,输入一个介于0和10之间的浮点数,最多3个十进制数。我使用NumberFormat和PropertyChangeListener来验证值或返回旧值。但它不起作用:

public class IRCompensationDialog extends JDialog{

private static final long serialVersionUID = 1L;

private JDialog irDialog;
private JButton cancelButton, okButton;
private JFormattedTextField resistorValue;
private INode nodo;

public IRCompensationDialog(int idNodo) throws BusinessException, ParseException{
    super(MainFrame.getInstance());
    this.irDialog = this;
    this.setResizable(false);
    this.setModal(true);
    this.setTitle("IR Compensation");
    this.nodo = new ServicesFactoryImpl().getNodesServices().getNode(idNodo);

    initComponents();

    this.pack();
    this.setLocationRelativeTo(null);
    this.setVisible(true);
}

private void initComponents() throws ParseException{
    NumberFormat numberFormat = NumberFormat.getInstance();
    numberFormat.setMaximumFractionDigits(3);
    resistorValue = new JFormattedTextField(numberFormat);
    resistorValue.addPropertyChangeListener("value", new IRValueChangeListener());
    float currentValue = nodo.getIR() / 1000;
    resistorValue.setValue(currentValue);

    JPanel botonera = new JPanel();
    cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            irDialog.setVisible(false);
        }
    });
    botonera.add(cancelButton);
    okButton = new JButton("OK");
    okButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Float newValue = (Float)resistorValue.getValue()*1000;
            nodo.setIR(newValue.intValue());
        }
    });
    botonera.add(okButton);

    this.setLayout (new BorderLayout());
    this.add (resistorValue, BorderLayout.CENTER);
    this.add (new JLabel("Enter resistor value (Ohms):"), BorderLayout.NORTH);
    this.add (botonera, BorderLayout.SOUTH);
}

private class IRValueChangeListener implements PropertyChangeListener{

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        JFormattedTextField field = (JFormattedTextField) evt.getSource();
        Float newValue = (Float)evt.getNewValue().toString();

        if(newValue>0 && newValue<=10000){
            JOptionPane.showMessageDialog(MainFrame.getInstance(), " Value must be between 0 and 10 Ohm", "Error", JOptionPane.ERROR_MESSAGE);
            Float oldValue = (Float) evt.getOldValue();
            field.setValue(oldValue);
        }

    }

}
public类IRCompensationDialog扩展JDialog{
私有静态最终长serialVersionUID=1L;
专用JDialog-irDialog;
私有JButton取消按钮,ok按钮;
私有JFormattedTextField resistorValue;
私人野田英德;
公共IRCompensationDialog(int idNodo)抛出BusinessException、ParseException{
super(MainFrame.getInstance());
this.irDialog=this;
此参数为.setresizeable(false);
此.setModal(true);
本条第2款(“IR补偿”);
this.nodo=newservicesfactoryimpl().getNodesServices().getNode(idNodo);
初始化组件();
这个包();
此.setLocationRelativeTo(空);
此.setVisible(true);
}
私有void initComponents()引发ParseException异常{
NumberFormat NumberFormat=NumberFormat.getInstance();
numberFormat.setMaximumFractionDigits(3);
resistorValue=新的JFormattedTextField(numberFormat);
addPropertyChangeListener(“value”,新的IRValueChangeListener());
float currentValue=nodo.getIR()/1000;
电阻值。设置值(当前值);
JPanel-botonera=新的JPanel();
cancelButton=新按钮(“取消”);
cancelButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
irDialog.setVisible(假);
}
});
添加(取消按钮);
OK按钮=新的JButton(“OK”);
okButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
Float newValue=(Float)resistorValue.getValue()*1000;
nodo.setIR(newValue.intValue());
}
});
添加(确定按钮);
this.setLayout(新的BorderLayout());
this.add(resistorValue,BorderLayout.CENTER);
添加(新JLabel(“输入电阻值(欧姆):”),BorderLayout.NORTH);
此.add(botonera,BorderLayout.SOUTH);
}
私有类IRValueChangeListener实现PropertyChangeListener{
@凌驾
公共作废属性更改(属性更改事件evt){
JFormattedTextField=(JFormattedTextField)evt.getSource();
Float newValue=(Float)evt.getNewValue().toString();
如果(newValue>0&&newValue有两个区域

1) 不是
Float
,而是
Float

2) 我使用强制转换从
JFormattedTextField

3)
float newValue=(((数字)resistorValue.getValue()).floatValue());