【问题标题】:getting value from jSpinner in jOptionPane从 jOptionPane 中的 jSpinner 获取价值
【发布时间】:2015-05-27 04:29:40
【问题描述】:

我需要将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 获取价值,但它不起作用

【问题讨论】:

    标签: java swing actionlistener joptionpane jspinner


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      相关资源
      最近更新 更多