将 JSpinner 值转换为新的 int

Convert JSpinner value to a new int

我正在使用 WindowBuilder 创建 GUI。 我是 JFrame 的新手,我遇到了一个问题:我添加了一个 JSpinner,我想将我放入微调器中的数字保存到一个 int 中,以便以后使用。有人可以帮帮我吗?谢谢

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class frame1 {

    private JFrame frame;
    private JTextField txtHoeveelEuroWil;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame1 window = new frame1();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public frame1() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JSpinner spinner = new JSpinner();
        spinner.setModel(new SpinnerNumberModel(20, 20, 500, 20));
        spinner.setBounds(116, 100, 200, 50);
        frame.getContentPane().add(spinner);

        int value;

        JButton btnNewButton = new JButton("OK");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                //value=Integer.parseint ???

                //what should I type here to save the number that I entered
                //in the spinner?


            }
        });
        btnNewButton.setBounds(172, 177, 89, 32);
        frame.getContentPane().add(btnNewButton);
    }

}

试试这个

 try {
        spinner.commitEdit();
    }
    catch (ParseException pe) {{
        // Edited value is invalid, spinner.getValue() will return
        // the last valid value, you could revert the spinner to show that:
        JComponent editor = spinner.getEditor()
        if (editor instanceof DefaultEditor) {
            ((DefaultEditor)editor).getTextField().setValue(spinner.getValue());
        }
        // reset the value to some known value:
        spinner.setValue(fallbackValue);
        // or treat the last valid value as the current, in which
        // case you don't need to do anything.
    }
    int value = (Integer)spinner.getValue();

要保存值并使用它,您可以设置一个字段变量来保存它。因此,在您的 fram1 class 中,为变量添加一个 getter 和 setter。然后在按钮点击时设置变量。

public class frame1 {

    //default to -1
    private int spinnerValue = -1;

    private void setSpinnerValue(aValue) {
      spinnerValue = aValue;
    }

    public int getSpinnerValue() {
      return spinnerValue;
    }

    private void initialize() {

        // blah blah

        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // here set a value
                setSpinnerValue((Integer) spinner.getValue());
            }
        });
    }
}

然后当你需要这个值的时候,你可以使用frame1的实例调用这个方法

frame1.getSpinnerValue();