如何设置jSpinner的默认值? [英] how to set jSpinner default value?

查看:891
本文介绍了如何设置jSpinner的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jSpinner以格式HH:MM设置时间,它工作正常。

I'm using a jSpinner to set the time with format "HH:MM" and it works fine.

我的问题是我想将jSpinner设置为激活窗口时00:00,单击jSpinner后,我希望将值更改为HH:MM格式。我不能自己做,jSpinner值不会改变它仍然是00:00。
请帮帮我。提前谢谢。

My problem is I want to set my jSpinner to "00:00" when the window is activated, and once jSpinner is clicked I want the value to be changed to the "HH:MM" format. And I can't do it myself, the jSpinner value doesn't change it is still "00:00". Please help me. Thank you in advance.

推荐答案

假设您使用的是 SpinnerDateModel ,然后 JSpinner 管理的值是 java.util.Date 。你需要创建一个 Date 实例,它的小时/分钟字段设置为午夜

Assuming that your are using a SpinnerDateModel, then the value that the JSpinner is managing is java.util.Date. You need to create a Date instance which has it's hour/minute fields set to midnight

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);

Date date = cal.getTime();
spinner.setValue(date);

JSpinner ,你可以使用 SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String value = sdf.format(spinner.getValue());




但如何设置为00:00窗口已激活

but how can I set it with "00:00" when the window is activated

如果没有更多信息,我建议使用 WindowListener 某种,假设你不是每次都创建窗口。

Without more information, I'd suggest using a WindowListener of some kind, assuming that you're not creating the window each time.

看看如何编写焦点监听器如何编写窗口监听器


点击后更改了值?

And changed the value once clicked?

这有点不清楚,但你应该从 JSpinner 当时您需要知道它是什么,并根据您的需要进行格式化。 JSpinner 应该在更新值时自动格式化值(根据您的设置)。

That's a little unclear, but you should just get the value from the JSpinner at the time you need to know what it is and format based on your needs. The JSpinner should be taking care of formatting the value automatically (based on your settings) when the values is updated.

话虽如此,我确实需要在设置模型时使开始(或最低值) null ,这似乎允许微调器更新时间值

Having said that, I did need to make the "start" (or lowest value) null when I set up the model, which seemed to allow the spinner to update the time value

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);

Date startTime = cal.getTime();
cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);
Date endTime = cal.getTime();

System.out.println(startTime);
System.out.println(endTime);

// The Calendar field seems to get ignored and overriden 
// by the UI delegate based on what part of the field
// the cursor is current on (hour or minute)
SpinnerDateModel model = new SpinnerDateModel(startTime, null, endTime, Calendar.MINUTE);
JSpinner spinner = new JSpinner(model);
spinner.setEditor(new JSpinner.DateEditor(spinner, "HH:mm"));

spinner.setValue(startTime);

这篇关于如何设置jSpinner的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆