如何为 JSpinner 使用 ChangeListener?
how to use ChangeListener for JSpinner?
private String[]fontsizelist = {"10","15","20"};
private SpinnerListModel spinmodel = new SpinnerListModel(fontsizelist);
private JSpinner fontsizeitem= new JSpinner(spinmodel);
//Constructor
some codes
fontsizeitem.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
for(int i=0;i<fontsizelist.length;i++){
fontsizeitem [i]=new JSpinner(fontsizelist[i]);<-shows error
}
}
});
此代码向我显示错误消息
需要数组,但 JSpinner 找到了
我知道问题出在哪里,但我不知道该如何解决。
我正在使用字符串数组,因为整数数组不适用于
SpinnerNumberModel 和 SpinnerListModel。
当 JSpinner 的状态为
时,字体大小应该会自动更改
改变了。我的情况应该怎么办?
你应该从 How to Use Spinners
开始
A SpinnerNumberModel
正确设置([=12= 的最小值],20
的最大值和 5
的步骤)会给你与 [=15] 相同的结果=]
基本思路是,JSpinner
会在值更改时通知您的 ChangeListener
,因此,您需要获取 JSpinner
的引用并获取它的当前值值,例如...
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
SpinnerNumberModel model = new SpinnerNumberModel(10, 10, 20, 5);
JSpinner spinner = new JSpinner(model);
add(spinner);
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSpinner spinner = (JSpinner) e.getSource();
int value = (int)spinner.getValue();
System.out.println("Value is " + value);
}
});
}
}
}
private String[]fontsizelist = {"10","15","20"};
private SpinnerListModel spinmodel = new SpinnerListModel(fontsizelist);
private JSpinner fontsizeitem= new JSpinner(spinmodel);
//Constructor
some codes
fontsizeitem.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
for(int i=0;i<fontsizelist.length;i++){
fontsizeitem [i]=new JSpinner(fontsizelist[i]);<-shows error
}
}
});
此代码向我显示错误消息
需要数组,但 JSpinner 找到了
我知道问题出在哪里,但我不知道该如何解决。
我正在使用字符串数组,因为整数数组不适用于
SpinnerNumberModel 和 SpinnerListModel。
当 JSpinner 的状态为
时,字体大小应该会自动更改改变了。我的情况应该怎么办?
你应该从 How to Use Spinners
开始A SpinnerNumberModel
正确设置([=12= 的最小值],20
的最大值和 5
的步骤)会给你与 [=15] 相同的结果=]
基本思路是,JSpinner
会在值更改时通知您的 ChangeListener
,因此,您需要获取 JSpinner
的引用并获取它的当前值值,例如...
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
SpinnerNumberModel model = new SpinnerNumberModel(10, 10, 20, 5);
JSpinner spinner = new JSpinner(model);
add(spinner);
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSpinner spinner = (JSpinner) e.getSource();
int value = (int)spinner.getValue();
System.out.println("Value is " + value);
}
});
}
}
}