2015-04-29 40 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();