在jOptionPane中从jSpinner获取值

时间:2015-05-27 04:29:41

标签: java swing actionlistener joptionpane jspinner

我需要在JSpinner中添加JOptionPane。这是我尝试过的:

public void actionPerformed(ActionEvent event) {

    SpinnerNumberModel Model = new SpinnerNumberModel(0.05, 0.00, 1.00, 0.01);
    JSpinner spinner1 = new JSpinner(Model);
    int option2 = JOptionPane.showOptionDialog(null, spinner1, "Input seuille", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
    if (option2 == JOptionPane.CANCEL_OPTION) {
        // user hit cancel                          
        m = 0.05;
    } else if (option2 == JOptionPane.OK_OPTION) {
        // user entered a number
        m = Double.parseDouble(spinner1.getValue().toString());
    }
}

我需要从jSpinner获取价值,但它不起作用

1 个答案:

答案 0 :(得分:1)

此...

SpinnerNumberModel Model = new SpinnerNumberModel(0.05, 0.00, 1.00, 0.01);
JSpinner spinner1 = new JSpinner(Model);
int option2 = JOptionPane.showOptionDialog(null, spinner1, "Input seuille", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
double m = 0;
if (option2 == JOptionPane.CANCEL_OPTION) {
    // user hit cancel                          
    m = 0.05;
} else if (option2 == JOptionPane.OK_OPTION) {
    // user entered a number
    m = (Double)spinner1.getValue();
}
System.out.println(m);

对我来说似乎很好。 JSpinner的值基于SpinnerModel。由于模型正在处理double值,因此您应该能够直接将其转换为double