2015-04-29 43 views
1

我使用WindowBuilder创建GUI。 我有点新JFrame,我有一个问题:我添加了一个JSpinner,我想将我放在微调器中的数字保存到一个int中,以便稍后使用它。有人可以帮帮我吗?谢谢。将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); 
    } 

} 

回答

0

试试这个

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(); 
+0

谢谢! –

+0

@RaymondTimmermans您必须调用'commitEdit'才能获取最后一个值。否则,如果用户编辑了微调器,则不会得到更新的值,而是会得到旧的值;) – MCHAppy

0

要保存的价值和使用它,你可以设置一个字段变量来保存它。所以在你的fram1类中,为变量添加一个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()); 
      } 
     }); 
    } 
} 

那么当你需要的值,可以使用帧1的实例,你这么多调用此方法

frame1.getSpinnerValue();