java - JFormattedTextField 和 NumberFormat 与 Float 一起使用

标签 java swing number-formatting jformattedtextfield

我用 JFormattedTextField 创建了一个小对话框,用于输入 0 到 10 之间的 float 字,最多 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);
        }

    }

}

}

INode 是我创建的一个类,它存储我使用 getIR() 方法获取的 int 值,并使用 setIR(int) 方法更新它。

我在行 Float newValue = (Float)resistorValue.getValue()*1000; 中收到 java.lang.ClassCastException: java.lang.Long 无法转换为 java.lang.Float; 以及 Float newValue = (Float)evt.getNewValue();

最佳答案

有两个区域

1) 不是Float而是float

2) 我使用强制转换从 JFormattedTextField

获取值

3) float newValue = (((Number)sistorValue.getValue()).floatValue());

关于java - JFormattedTextField 和 NumberFormat 与 Float 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8254093/

相关文章:

java - JTextPane 中的双列表项插入

java - 我想将所有联系人保存并检索到 Firebase Firestore,需要结构来保存和检索联系人

java - 使用 readLine() 不断收到此异常

java - 如何解决这个错误,如果我使用一个类会导致其他类接口(interface)也被感染

javascript - 用户键入时将输入值转换为货币格式

java - 找不到更新日志 changelog.groovy

java - 打开新框架时出现 NullPointerException

用于 swing GUI 编程的 Java 类体系结构

flutter - Dart/Flutter 中 Intl 包的 NumberFormat 的反直觉结果

CSS - 10000 表示为 10 000?