javax.swing.JSpinner滚动选择_上下调节器的用法
类定义 , 是 JComponent 的子类
public class JSpinner extends JComponent implements Accessible
用法示例2205261935
代码
package jcomponent;
import java.awt.GridLayout;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class JSpinner滚动选择_上下调节器的用法2205261915 extends JFrame{
private static final long serialVersionUID = -3887866455809683828L;
{
setTitle(this.getClass().getSimpleName());
setBounds(100, 50, 1600, 900);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLayout(new GridLayout(0, 2, 5, 5));
setForeground(Color.blue);
for(int c=0;c<6;c++)add(Box.createVerticalBox());
JSpinner jSpinnerTemp = null;
Container containerTemp = null;
SpinnerModel model = null;
int i=0;
jSpinnerTemp = new JSpinner(); containerTemp=((Container)getContentPane().getComponent(i++));
jSpinnerTemp.setFont(new Font("",Font.BOLD,66));
containerTemp.add(new JLabel("默认的Model")); containerTemp.add(jSpinnerTemp);
jSpinnerTemp.setToolTipText("hello");
model = new SpinnerNumberModel(66.6, -16.8, 365.5, 0.3);
jSpinnerTemp = new JSpinner(model); containerTemp=((Container)getContentPane().getComponent(i++));
jSpinnerTemp.setFont(new Font("",Font.BOLD,66));
containerTemp.add(new JLabel(jSpinnerTemp.getModel().getClass().getSimpleName())); containerTemp.add(jSpinnerTemp);
model = new SpinnerDateModel();
jSpinnerTemp = new JSpinner(model); containerTemp=((Container)getContentPane().getComponent(i++));
jSpinnerTemp.setFont(new Font("",Font.BOLD,66));
containerTemp.add(new JLabel(jSpinnerTemp.getModel().getClass().getSimpleName())); containerTemp.add(jSpinnerTemp);
model = new SpinnerListModel(new Object[] {"你好","世界","hello","world"});
jSpinnerTemp = new JSpinner(model); containerTemp=((Container)getContentPane().getComponent(i++));
jSpinnerTemp.setFont(new Font("",Font.BOLD,66));
containerTemp.add(new JLabel(jSpinnerTemp.getModel().getClass().getSimpleName())); containerTemp.add(jSpinnerTemp);
model = new AbstractSpinnerModel() {
private static final long serialVersionUID = 4773227324614643342L;
int v=6;
private transient ChangeEvent changeEvent = null;
@Override
public void setValue(Object value) {
v=Integer.parseInt(value+"");
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -=2 ) {
if (listeners[i] == ChangeListener.class) {
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
}
}
}
@Override
public Object getValue() {
return v;
}
@Override
public Object getPreviousValue() {
return --v;
}
@Override
public Object getNextValue() {
return ++v;
}
};
jSpinnerTemp = new JSpinner(model); containerTemp=((Container)getContentPane().getComponent(i++));
jSpinnerTemp.setFont(new Font("",Font.BOLD,66));
containerTemp.add(new JLabel("实现抽象类 AbstractSpinnerModel")); containerTemp.add(jSpinnerTemp);
for(int h=0; h<getContentPane().getComponentCount(); h++) {
if( ((Container)getContentPane().getComponent(h)).getComponentCount()>0 )
((Container)getContentPane().getComponent(h)).getComponent(0).setFont(new Font("",Font.BOLD,30));
}
setVisible(true);
}
public static void main(String...arguments) throws Exception{
Class.forName(Thread.currentThread().getStackTrace()[1].getClassName()).getDeclaredConstructor().newInstance();
}
}
效果截图