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);
}
}
謝謝! –
@RaymondTimmermans您必須調用'commitEdit'才能獲取最後一個值。否則,如果用戶編輯了微調器,則不會得到更新的值,而是會得到舊的值;) – MCHAppy