频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

JAVA基础应用:日期时间选择控件(代码)

来源:赛迪网技术社区 作者: 出处:巧巧读书 2007-10-05 进入讨论组
上一页 1 2 3 

private JPanel createWeekAndDayPanal() {
String colname[] = {"日","一","二","三","四","五","六"};
JPanel result = new JPanel();
//设置固定字体,以免调用环境改变影响界面美观
result.setFont(new Font("宋体", Font.PLAIN, 12));
result.setLayout(new GridLayout(7,7));
result.setBackground(Color.white);
JLabel cell ;

for(int i=0;i<7;i++) {
cell = new JLabel(colname[i]);
cell.setHorizontalAlignment(JLabel.RIGHT);
if (i==0 || i==6) cell.setForeground(weekendFontColor) ;
else cell.setForeground(weekFontColor) ;
result.add(cell) ;
}

int actionCommandId = 0 ;
for(int i = 0; i < 6; i++)
for(int j = 0; j < 7; j++) {
JButton numberButton = new JButton();
numberButton.setBorder(null) ;
numberButton.setHorizontalAlignment(SwingConstants.RIGHT);
numberButton.setActionCommand(String.valueOf(actionCommandId)) ;
numberButton.addActionListener(this) ;
numberButton.setBackground(palletTableColor);
numberButton.setForeground(dateFontColor) ;
if (j==0 || j==6) numberButton.setForeground(weekendFontColor) ;
else numberButton.setForeground(dateFontColor) ;
daysButton[i][j] = numberButton;
result.add(numberButton) ;
actionCommandId ++ ;
}

return result;
}

private JDialog createDialog(Frame owner ) {
JDialog result = new JDialog(owner,"日期时间选择",true) ;
result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
result.getContentPane().add(this,BorderLayout.CENTER) ;
result.pack() ;
result.setSize(width, height);
return result ;
}

void showDateChooser(Point position ) {
Frame owner =(Frame)SwingUtilities.getWindowAncestor(DateChooserJButton.this) ;
if (dialog==null || dialog.getOwner()!= owner) dialog = createDialog(owner ) ;
dialog.setLocation(getAppropriateLocation(owner ,position ) ) ;
flushWeekAndDay() ;
dialog.show();
}

Point getAppropriateLocation(Frame owner ,Point position ) {
Point result = new Point(position) ;
Point p = owner.getLocation() ;
int offsetX = (position.x+width) - (p.x + owner.getWidth() ) ;
int offsetY = (position.y+height) - (p.y + owner.getHeight() ) ;

if (offsetX >0 ) {
result.x -= offsetX ;
}

if (offsetY >0 ) {
result.y -= offsetY ;
}

return result ;

}

private Calendar getCalendar() {
Calendar result = Calendar.getInstance();
result.setTime(getDate()) ;
return result ;
}

private int getSelectedYear() {
return ((Integer)yearSpin.getValue()).intValue() ;
}

private int getSelectedMonth() {
return ((Integer)monthSpin.getValue()).intValue() ;
}

private int getSelectedHour() {
return ((Integer)hourSpin.getValue()).intValue() ;
}

private void dayColorUpdate(boolean isOldDay) {
Calendar c = getCalendar() ;
int day = c.get(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH,1);
int actionCommandId =day-2+c.get(Calendar.DAY_OF_WEEK) ;
int i = actionCommandId/7;
int j = actionCommandId%7;
if (isOldDay) daysButton[i][j].setForeground(dateFontColor) ;
else daysButton[i][j].setForeground(todayBackColor) ;
}

private void flushWeekAndDay() {
Calendar c = getCalendar() ;
c.set(Calendar.DAY_OF_MONTH,1);
int maxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int dayNo = 2 - c.get(Calendar.DAY_OF_WEEK) ;
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 7; j++) {
String s="" ;
if (dayNo>=1 && dayNo<=maxDayNo ) s = String.valueOf(dayNo) ;
daysButton[i][j].setText(s) ;
dayNo ++ ;
}
}
dayColorUpdate(false) ;
}

public void stateChanged(ChangeEvent e) {
JSpinner source =(JSpinner)e.getSource() ;
Calendar c = getCalendar() ;
if (source.getName().equals("Hour")) {
c.set(Calendar.HOUR_OF_DAY, getSelectedHour());
setDate(c.getTime());
return ;
}

dayColorUpdate(true) ;

if (source.getName().equals("Year"))
c.set(Calendar.YEAR, getSelectedYear());
else
// (source.getName().equals("Month"))
c.set(Calendar.MONTH, getSelectedMonth()-1);
setDate(c.getTime());
flushWeekAndDay() ;
}

public void actionPerformed(ActionEvent e) {
JButton source =(JButton)e.getSource() ;
if (source.getText().length()==0) return ;
dayColorUpdate(true) ;
source.setForeground(todayBackColor) ;
int newDay = Integer.parseInt(source.getText());
Calendar c = getCalendar() ;
c.set(Calendar.DAY_OF_MONTH,newDay);
setDate(c.getTime());
}

}

}

专题:http://www.qqread.com/java/2007/04/w306259.html 更多文章 更多内容请看Java环境安装配置Java编程开发手册Java应用开发篇专题,或进入讨论组讨论。
上一页 1 2 3 
收藏此文】【 】【打印】【关闭
较早的文章:JPA重整ORM山河

较新的文章:java计算磁盘空间的大小
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章