最近在努力写个日历代码,总遇到很多问题/
我没调用日期类,只是用最古老的方法,循环输出,但遇到日期和星期不懂怎么安排,那位指点下?
------解决方案--------------------
/**
*
*减少按钮的边框,使按钮变小,外观协调。
*
*/
class DateChooserButton extends JButton {
public DateChooserButton(String text) {
super(text);
}
public DateChooserButton(Icon icon) {
super(icon);
}
public Insets getInsets() {
return new Insets(3, 3, 1, 3);
}
public DateChooserButton() {
}
}
------解决方案--------------------
/**
* 初始化选择面板
*/
private void createChoosePanel(Calendar currentCalender) {
choosePanel = new JPanel(new java.awt.FlowLayout());
choosePanel.setBackground(java.awt.Color.pink);
upMonth = new DateChooserButton(new javax.swing.ImageIcon( "img/up.jpg "));
upMonth.setActionCommand( "up ");
downMonth = new DateChooserButton(new javax.swing.ImageIcon( "img/next.jpg "));
upMonth.setActionCommand( "down ");
choseLabel = new JLabel(this.formatCalender(currentCalender));
choosePanel.add(upMonth);
choosePanel.add(new JLabel( " "));
choosePanel.add(choseLabel);
choosePanel.add(new JLabel( " "));
choosePanel.add(downMonth);
downMonth.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooseButton_click(e);
}
});
upMonth.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooseButton_click(e);
}
});
}
/**
* 选择按纽处理事件
*/
private void chooseButton_click(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals( "down ")) {
currentCalender.add(Calendar.MONTH, -1);
} else {
currentCalender.add(Calendar.MONTH, 1);
}
choseLabel.setText(this.formatCalender(currentCalender));
//刷新日历
this.flushWeekAndDayPanal(currentCalender);
}
/**
* 初始化当前日期面板
*/
private void createcurrentDatePanel(Date currentDate) {
currentDatePanel = new JPanel(new java.awt.FlowLayout());
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"今天:yyyy年MM月dd日 E ");
JLabel lblCurrentDate = new JLabel();
lblCurrentDate.setForeground(Color.MAGENTA);
lblCurrentDate.setText(sdf.format(currentDate));
currentDatePanel.add(lblCurrentDate);
}
/**
* 初始化日历面板
*/
private void createCalendarPanel() {
calendarPanel = new JPanel(new java.awt.BorderLayout());
calendarPanel.setBackground(Color.WHITE);
/**
* 匿名内部类,画线
*/
JPanel calendarPanelTitleDown = new JPanel() {
public void paintComponent(Graphics g) {
super.paintChildren(g);
g.setColor(Color.BLUE);
g.drawLine(0, 0, this.getWidth(), 0);
}
};
JPanel calendarPanelTitleUp=new JPanel();
calendarPanelTitleUp.setBackground(Color.WHITE);
calendarPanelTitleUp.setLayout(new java.awt.GridBagLayout());