gpt4 book ai didi

java - JFormattedTextField 和 NumberFormat 与 Float 一起使用

转载 作者:行者123 更新时间:2023-12-04 06:06:54 26 4
gpt4 key购买 nike

我用 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);
}

}

}

}

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

我得到 java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Float in the line Float newValue = (Float)resistorValue.getValue()*1000;以及在线 Float newValue = (Float)evt.getNewValue();

最佳答案

有两个区域

1) 不是 Float但是 float ,

2) 我使用强制转换从 JFormattedTextField 获取值(value)

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com