我正在尝试使用JSpinner
将SpinnerDateModel
增加15,30和45。我试图在int calendarField
作为Calendar.MINUTE + 15
的第四个参数中传递构造函数中的值,但是这使得我的字段为int
。
java.lang.IllegalArgumentException:calendarField
无效
下面的Runnable类。
import java.awt.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class Spinner extends JPanel
{
static String[] columnNames = {
"Value"
};
static Object[][] data = {
{"07:00 AM"}
};
public static void main( String[] args ) throws ParseException {
JFrame frame = new JFrame();
JTable table = new JTable(data,columnNames);
TableColumnModel tcm = table.getColumnModel();
TableColumn tc = tcm.getColumn(0);
tc.setCellEditor(new NewClass.SpEditor());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(table);
frame.pack();
frame.setVisible(true);
}
private static class SpEditor extends DefaultCellEditor
{
JSpinner spinner;
JSpinner.DefaultEditor editor;
JTextField textField;
SimpleDateFormat date = new SimpleDateFormat("hh:mm a");
public SpEditor() throws ParseException
{
super(new JTextField());
spinner = new JSpinner();
spinner.setModel(new SpinnerDateModel(date.parse("07:00 AM"), date.parse("07:00 AM"), date.parse("07:00 PM"), Calendar.MINUTE + 15));
spinner.setEditor(new JSpinner.DateEditor(spinner, "hh:mm a"));
editor = ((JSpinner.DateEditor)spinner.getEditor());
textField = editor.getTextField();
}
// Prepares the spinner component and returns it.
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column
) {
return spinner;
}
// Returns the spinners current value.
@Override
public Object getCellEditorValue() {
return spinner.getValue();
}
}
}
答案 0 :(得分:1)
无效calendarField 的详细信息位于SpinnerDateModel.class
内:
if (!calendarFieldOK(calendarField)) {
throw new IllegalArgumentException("invalid calendarField");
}
其实现方式为:
switch(calendarField) {
case Calendar.ERA:
case Calendar.YEAR:
case Calendar.MONTH:
case Calendar.WEEK_OF_YEAR:
case Calendar.WEEK_OF_MONTH:
case Calendar.DAY_OF_MONTH:
case Calendar.DAY_OF_YEAR:
case Calendar.DAY_OF_WEEK:
case Calendar.DAY_OF_WEEK_IN_MONTH:
case Calendar.AM_PM:
case Calendar.HOUR:
case Calendar.HOUR_OF_DAY:
case Calendar.MINUTE:
case Calendar.SECOND:
case Calendar.MILLISECOND:
return true;
default:
return false;
}
我希望这可以解释您获得的例外情况。由于实现中没有27
(MINUTE + 15)的情况。