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 评论 纠错