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

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());

}

}

}

来源:考试大-Java认证

责编:oyshaoli  评论 纠错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值