我正在尝试在带有java的对话框中显示一个只有小时和分钟的时间选择器。但是,它会使用完整的日期和时间格式进行渲染。
我的代码如下:
Date date = new Date();
SpinnerDateModel sm = new SpinnerDateModel(date, null, null, Calendar.HOUR_OF_DAY);
JSpinner spinner = new JSpinner(sm);
JSpinner.DateEditor de = new JSpinner.DateEditor(spinner, "HH:mm");
int option = JOptionPane.showOptionDialog(frame, spinner, "Create a new alarm", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
if (option == JOptionPane.CANCEL_OPTION) {
// user hit cancel
}
else if (option == JOptionPane.OK_OPTION){
// user entered a time
}
答案 0 :(得分:0)
我用它替换它并且它起作用:
SimpleDateFormat model = new SimpleDateFormat("HH:mm");
JSpinner spinner = new JSpinner(new SpinnerDateModel());
spinner.setEditor(new JSpinner.DateEditor(spinner, model.toPattern()));