关于JSpinner的值的问题?

0xTonyWang 2003-08-23 11:35:43
我想用JSpinner组件,设置值为时间,比如显示初始时间为08:30,然后每次用上下箭头,均增加或减少一分钟。怎么实现,可否给出代码,谢谢!解决问题的,加分相送。
...全文
126 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
rekcah_ph 2003-08-30
  • 打赏
  • 举报
回复
我刚写了一个,比较粗糙,基本功能都实现了,你可以参考一下。
import javax.swing.*;
class SpinnerTimeModel extends AbstractSpinnerModel
{
int h=0;
int m=0;
public Object getNextValue()
{
System.out.println("getNexValue ");
m++;
if (m == 60)
{
m=0;
h++;
if (h == 24)
h=0;
}
return h+":"+m;
}
public Object getPreviousValue()
{
System.out.println("getPreValue ");
m--;
if (m == -1)
{
m=59;
h--;
if (h==-1)
{
h=23;
}
}
return h+":"+m;
}
public Object getValue()
{
System.out.println("getValue");
return h+":"+m;
}
public void setValue(Object value)
{
System.out.println("setValue " + value + " " + value.getClass());
if (value == null || !(value instanceof String))
{
System.out.println("not instanceof");
throw new IllegalArgumentException("invalid value");
}
else
{
String val = (String)value;
int separator = val.indexOf(':');
String sHour = val.substring(0, separator);
String sMinute = val.substring(separator+1);
try
{
int hour = Integer.parseInt(sHour);
int minute = Integer.parseInt(sMinute);
if (hour>=0 && hour<24 && minute>=0 && minute<60)
{
h = hour;
m = minute;
fireStateChanged();
}
else
{
System.out.println("not 0~23 or 0~59");
throw new IllegalArgumentException("invalid value");
}
}
catch(NumberFormatException nfe)
{
System.out.println("nfe Exception");
throw new IllegalArgumentException("invalid value");
}
}
}
}
0xTonyWang 2003-08-24
  • 打赏
  • 举报
回复
可否详细一点,谢谢
rekcah_ph 2003-08-24
  • 打赏
  • 举报
回复
修改SpinnerModel
关键这3个方法:
Object getNextValue()
Return the object in the sequence that comes after the object returned by getValue().
Object getPreviousValue()
Return the object in the sequence that comes before the object returned by getValue().
Object getValue()
The current element of the sequence.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧