首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java 日历?解决方案

2012-01-02 
java 日历?最近在努力写个日历代码,总遇到很多问题/我没调用日期类,只是用最古老的方法,循环输出,但遇到日

java 日历?
最近在努力写个日历代码,总遇到很多问题/
        我没调用日期类,只是用最古老的方法,循环输出,但遇到日期和星期不懂怎么安排,那位指点下?

[解决办法]
/**
*
*减少按钮的边框,使按钮变小,外观协调。
*
*/
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());


for (int i = 0; i < title.length; i++) {
JLabel cell = new JLabel(title[i]);
if (i == 0 || i == 6) {
cell.setForeground(Color.RED);
}
cell.setPreferredSize(new Dimension(20, 20));
calendarPanelTitleUp.add(cell,
new GridBagConstraints(i, 0, 1, 1, 0.0, 0.0
, GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 3, 0, 3), 0, 0)
);

}
JPanel calendarPanelTitle=new JPanel(new java.awt.BorderLayout());
calendarPanelTitle.setBackground(Color.WHITE);
calendarPanelTitle.add(calendarPanelTitleUp,java.awt.BorderLayout.CENTER);
calendarPanelTitle.add(calendarPanelTitleDown,java.awt.BorderLayout.SOUTH);

calendarPanel.add(calendarPanelTitle, java.awt.BorderLayout.NORTH);

//日历内容面板
calendarPanelContent = new JPanel(new java.awt.GridBagLayout());
calendarPanelContent.setBackground(Color.WHITE);
this.initDayButton();
this.flushWeekAndDayPanal(currentCalender);
calendarPanel.add(calendarPanelContent);

}

private void lostFocus() {
this.dispose();
}

/**
* 格式化选择时间
*/
private String formatCalender(Calendar calender) {
return " " + calender.get(Calendar.YEAR) + "年 " +
(calender.get(Calendar.MONTH) + 1) + "月 ";
}

/**
* 根据日期刷新面板
* @param c Calendar
*/
private void flushWeekAndDayPanal(Calendar c) {
int actionCommandId = 1;


// 设置一星期的第一天是哪一天
c.setFirstDayOfWeek(Calendar.SUNDAY);
//参数只传递了年份和月份,所以将日的字段设置为每个月的第一天
c.set(Calendar.DAY_OF_MONTH, 1);

//取得每个月的第一天是星期几,如果是星期天则返回1,星期一则返回2,星期六返回7
int firstdayofWeek = c.get(Calendar.DAY_OF_WEEK);

//返回月份中的最大天数,7月为31
int lastdayofMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);

JButton numberButton;
//刷新时清除面板中所有组件
calendarPanelContent.removeAll();
this.flushButtonColor();
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
numberButton = dayButton[i][j];
//取得按钮上的编号
actionCommandId=Integer.parseInt(numberButton.getActionCommand());
if (actionCommandId==today) {
numberButton.setBackground(currentDayColor);
}

if ((actionCommandId + firstdayofWeek - 2) % 7 == 6 ||
(actionCommandId + firstdayofWeek - 2) % 7 == 0) {
numberButton.setForeground(Color.RED);
}

if (actionCommandId <= lastdayofMonth) {
int y = 0;
if ((firstdayofWeek - 1) <=
(j + firstdayofWeek - 1) % 7) {
y = i;
} else {
y = i +1;
}

calendarPanelContent.add(numberButton,
new GridBagConstraints((j +
firstdayofWeek - 1) % 7, y, 1,1 , 0.0,
0.0
, GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 2, 0, 4), 0, 0)


);

}

}

}

}
[解决办法]
/**
* 初始化按钮颜色
*/
private void flushButtonColor(){
for (int i = 0; i <6; i++) {
for (int j = 0; j <7; j++) {
dayButton[i][j].setForeground(Color.BLACK);
}
}
}
/**
* 初始化按纽
*/
private void initDayButton() {
int actionCommandId = 1;
JButton numberButton;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
numberButton = new JButton();
numberButton.setBorder(BorderFactory.createEmptyBorder());
numberButton.setHorizontalAlignment(SwingConstants.CENTER);
numberButton.setActionCommand(String.valueOf(
actionCommandId));

numberButton.addMouseListener(this);

numberButton.setBackground(Color.WHITE);
numberButton.setPreferredSize(new Dimension(20, 20));
numberButton.setText(String.valueOf(actionCommandId));

dayButton[i][j] = numberButton;
actionCommandId++;
}
}
}

/**
*
* 实现监听器接口
*/

public void mouseClicked(MouseEvent e) {
JButton selectButton=(JButton)e.getSource();

currentCalender.set(Calendar.DAY_OF_MONTH,Integer.parseInt(selectButton.getActionCommand()));
SimpleDateFormat sdf=new SimpleDateFormat( "yyyy-MM-dd ");
jText.removeAll();
jText.setText(sdf.format(currentCalender.getTime()));
this.dispose();

}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {
JButton jbutton = (JButton) e.getSource();
jbutton.setBackground(Color.green);
}

public void mouseExited(MouseEvent e) {
JButton jbutton = (JButton) e.getSource();
int comm = Integer.parseInt(jbutton.getActionCommand());
if (comm == today) {
jbutton.setBackground(currentDayColor);
} else {
jbutton.setBackground(Color.WHITE);
}

}

public void actionPerformed(ActionEvent e) {

}

}

[解决办法]
厉害,编译通过了,问题是怎么使用这个控件

[解决办法]
建立一个 Frame 把下面的代码粘贴过去就可以了

import java.awt.BorderLayout;
import javax.swing.JFrame;

public class FrmTest extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
//实例化一个日期控件
private DatePanel date = new DatePanel();
public FrmTest() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);

getContentPane().add(date);//把 实例化的 日期控件添加到 容器里

this.setTitle( "测试控件如何使用 ");
this.setResizable(false);
this.setSize(400,300);
this.setLocationRelativeTo(null);


this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setVisible(true);

}
public static void main(String[] args) {
new FrmTest();
}
}

热点排行