求大神解答
package testJsinner;import java.awt.BorderLayout;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.AbstractSpinnerModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerNumberModel;
/**
* 年份微调器....
* @author Administrator
*
*/
public class SinnerTest {
public static void main(String[] args) {
SpinnerFrame frame = new SpinnerFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class SpinnerFrame extends JFrame
{
public SpinnerFrame()
{
setTitle("SpinnerTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
JPanel buttonPanel = new JPanel();
okButton = new JButton("ok");
buttonPanel.add(okButton);
add(buttonPanel,BorderLayout.SOUTH);
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0,3));
add(mainPanel,BorderLayout.CENTER);
JSpinner defaultSpinner = new JSpinner();
addRow("default",defaultSpinner);
JSpinner boundedSpinner = new JSpinner(new SpinnerNumberModel(5,0,10,0.5));
addRow("Bounde",boundedSpinner);
String []fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
JSpinner reverseListSpinner = new JSpinner(new SpinnerListModel(fonts){
public Object getNextValue(){
return super.getPreviousValue();
}
public Object getPreviousValue(){
return super.getNextValue();
}
});
add("Reverse List",reverseListSpinner);
JSpinner dateSpinner = new JSpinner(new SpinnerDateModel());
addRow("Date",dateSpinner);
JSpinner betterDateSpinner = new JSpinner(new SpinnerDateModel());
String pattern = ((SimpleDateFormat)DateFormat.getDateInstance()).toPattern();
betterDateSpinner.setEditor(new JSpinner.DateEditor(betterDateSpinner,pattern));
addRow("Better Date",betterDateSpinner);
JSpinner timeSpinner = new JSpinner(new SpinnerDateModel(new GregorianCalendar(2000,Calendar.JANUARY,1,12,0,0).getTime(),null,null,Calendar.HOUR));
addRow("Time",timeSpinner);
JSpinner permSpinner = new JSpinner(new PermutationSpinnerModel("meat"));
addRow("word permutations",permSpinner);
}
public void addRow(String labelText,final JSpinner spinner){
mainPanel.add(new JLabel(labelText));
mainPanel.add(spinner);
final JLabel valueLabel = new JLabel();
mainPanel.add(valueLabel);
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
Object value= spinner.getValue();
valueLabel.setText(value.toString());
}
});
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 250;
private JPanel mainPanel;
private JButton okButton;
}
class PermutationSpinnerModel extends AbstractSpinnerModel{
public PermutationSpinnerModel(String w){
word = w;
}
public Object getValue(){
return word;
}
public void setValue(Object value){
if(!(value instanceof String))throw new IllegalArgumentException();
word = (String )value;
fireStateChanged();
}
public Object getNextValue(){
int[] codePoints = toCodePointArray(word);
for(int i=codePoints.length-1;i>0;i--){
if(codePoints[i-1]<codePoints[i]){
int j = codePoints.length-1;
while(codePoints[i-1]>codePoints[j])j--;
swap(codePoints,i-1,j);
reverse(codePoints,i,codePoints.length-1);
return new String(codePoints,0,codePoints.length);
}
}
reverse(codePoints,0,codePoints.length-1);
return new String(codePoints,0,codePoints.length);
}
public Object getPreviousValue(){
int[] codePoints = toCodePointArray(word);
for(int i=codePoints.length-1;i>0;i--){
if(codePoints[i-1]>codePoints[i]){
int j = codePoints.length-1;
while(codePoints[i-1]<codePoints[j])j--;
swap(codePoints,i-1,j);
reverse(codePoints,i,codePoints.length-1);
return new String(codePoints,0,codePoints.length);
}
}
reverse(codePoints,0,codePoints.length-1);
return new String(codePoints,0,codePoints.length);
}
private static int[]toCodePointArray(String str){
int []codePoints = new int[str.codePointCount(0,str.length())];
for(int i=0,j=0;i<str.length();i++,j++){
int cp = str.codePointAt(i);
if(Character.isSupplementaryCodePoint(cp)) i++;
codePoints[j] = cp;
}return codePoints;
}
private static void swap(int[]a,int i,int j){
int temp =a[i];
a[i] = a[j];
a[j] = temp;
}
private static void reverse(int []a,int i,int j){
while(i<j){
swap(a,i,j);
i++;
j++;
}
}
private String word;
}
//编译通过,但运行时出现错误
//Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: unknown constraint: Reverse List
--------------------编程问答--------------------
add("Reverse List", reverseListSpinner);改成
addRow("Reverse List", reverseListSpinner);--------------------编程问答--------------------
楼上正解
package anyPackage;
import java.awt.BorderLayout;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.AbstractSpinnerModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerNumberModel;
/**
* 年份微调器....
*
* @author Administrator
*
*/
public class SinnerTest {
public static void main(String[] args) {
SpinnerFrame frame = new SpinnerFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class SpinnerFrame extends JFrame {
public SpinnerFrame() {
setTitle("SpinnerTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JPanel buttonPanel = new JPanel();
okButton = new JButton("ok");
buttonPanel.add(okButton);
add(buttonPanel, BorderLayout.SOUTH);
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0, 3));
add(mainPanel, BorderLayout.CENTER);
JSpinner defaultSpinner = new JSpinner();
addRow("default", defaultSpinner);
JSpinner boundedSpinner = new JSpinner(new SpinnerNumberModel(5, 0, 10,
0.5));
addRow("Bounde", boundedSpinner);
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
JSpinner reverseListSpinner = new JSpinner(new SpinnerListModel(fonts) {
public Object getNextValue() {
return super.getPreviousValue();
}
public Object getPreviousValue() {
return super.getNextValue();
}
});
addRow("Reverse List", reverseListSpinner);
JSpinner dateSpinner = new JSpinner(new SpinnerDateModel());
addRow("Date", dateSpinner);
JSpinner betterDateSpinner = new JSpinner(new SpinnerDateModel());
String pattern = ((SimpleDateFormat) DateFormat.getDateInstance())
.toPattern();
betterDateSpinner.setEditor(new JSpinner.DateEditor(betterDateSpinner,
pattern));
addRow("Better Date", betterDateSpinner);
JSpinner timeSpinner = new JSpinner(
new SpinnerDateModel(new GregorianCalendar(2000,
Calendar.JANUARY, 1, 12, 0, 0).getTime(), null, null,
Calendar.HOUR));
addRow("Time", timeSpinner);
JSpinner permSpinner = new JSpinner(new PermutationSpinnerModel("meat"));
addRow("word permutations", permSpinner);
}
public void addRow(String labelText, final JSpinner spinner) {
mainPanel.add(new JLabel(labelText));
mainPanel.add(spinner);
final JLabel valueLabel = new JLabel();
mainPanel.add(valueLabel);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Object value = spinner.getValue();
valueLabel.setText(value.toString());
}
});
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 250;
private JPanel mainPanel;
private JButton okButton;
}
class PermutationSpinnerModel extends AbstractSpinnerModel {
public PermutationSpinnerModel(String w) {
word = w;
}
public Object getValue() {
return word;
}
public void setValue(Object value) {
if (!(value instanceof String))
throw new IllegalArgumentException();
word = (String) value;
fireStateChanged();
}
public Object getNextValue() {
int[] codePoints = toCodePointArray(word);
for (int i = codePoints.length - 1; i > 0; i--) {
if (codePoints[i - 1] < codePoints[i]) {
int j = codePoints.length - 1;
while (codePoints[i - 1] > codePoints[j])
j--;
swap(codePoints, i - 1, j);
reverse(codePoints, i, codePoints.length - 1);
return new String(codePoints, 0, codePoints.length);
}
}
reverse(codePoints, 0, codePoints.length - 1);
return new String(codePoints, 0, codePoints.length);
}
public Object getPreviousValue() {
int[] codePoints = toCodePointArray(word);
for (int i = codePoints.length - 1; i > 0; i--) {
if (codePoints[i - 1] > codePoints[i]) {
int j = codePoints.length - 1;
while (codePoints[i - 1] < codePoints[j])
j--;
swap(codePoints, i - 1, j);
reverse(codePoints, i, codePoints.length - 1);
return new String(codePoints, 0, codePoints.length);
}
}
reverse(codePoints, 0, codePoints.length - 1);
return new String(codePoints, 0, codePoints.length);
}
private static int[] toCodePointArray(String str) {
int[] codePoints = new int[str.codePointCount(0, str.length())];
for (int i = 0, j = 0; i < str.length(); i++, j++) {
int cp = str.codePointAt(i);
if (Character.isSupplementaryCodePoint(cp))
i++;
codePoints[j] = cp;
}
return codePoints;
}
private static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private static void reverse(int[] a, int i, int j) {
while (i < j) {
swap(a, i, j);
i++;
j++;
}
}
private String word;
}
补充:Java , Java SE