java 日历? 最近在努力写个日历代码,总遇到很多问题/ 我没调用日期类,只是用最古老的方法,循环输出,但遇到日期和星期不懂怎么安排,那位指点下? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 请参考:http://community.csdn.net/Expert/TopicView.asp?id=5383638======================================== *日 一 二 三 四 五 六* |1月======================================== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31======================================== *日 一 二 三 四 五 六* |2月======================================== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28======================================== *日 一 二 三 四 五 六* |3月======================================== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31======================================== *日 一 二 三 四 五 六* |4月======================================== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30======================================== *日 一 二 三 四 五 六* |5月======================================== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31======================================== *日 一 二 三 四 五 六* |6月======================================== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30======================================== *日 一 二 三 四 五 六* |7月======================================== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31======================================== *日 一 二 三 四 五 六* |8月======================================== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31======================================== *日 一 二 三 四 五 六* |9月======================================== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30======================================== *日 一 二 三 四 五 六* |10月======================================== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31======================================== *日 一 二 三 四 五 六* |11月======================================== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30======================================== *日 一 二 三 四 五 六* |12月======================================== 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 一个封装好的日历控件 内容太长 分开发了 你自己专研一下好了import java.awt.BorderLayout;import javax.swing.JPanel;import javax.swing.JTextField;import java.awt.Rectangle;import javax.swing.JButton;import java.awt.GridBagLayout;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.*;import java.awt.Point;import java.awt.Dimension;import javax.swing.Icon;import javax.swing.JDialog;import javax.swing.JLabel;import java.util.Calendar;import java.util.Date;import javax.swing.BorderFactory;import javax.swing.SwingConstants;import java.text.SimpleDateFormat; public class DatePanel extends JPanel { public DatePanel() { try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } public String getDate(){ return jTextField1.getText(); } public void setDate(String date){ jTextField1.setText(date); } private void jbInit() throws Exception { this.setLayout(gridBagLayout1); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jTextField1.setMinimumSize(new Dimension(11, 16)); jTextField1.setPreferredSize(new Dimension(11, 16)); jButton1.setBorder(BorderFactory.createEtchedBorder()); this.add(jButton1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 8, 1)); this.add(jTextField1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 66, 1)); jTextField1.setBackground(Color.WHITE); jTextField1.setEditable(false); } JTextField jTextField1 = new JTextField(); DateChooserButton jButton1 = new DateChooserButton("\u25BC"); GridBagLayout gridBagLayout1 = new GridBagLayout(); private void jButton1_actionPerformed(ActionEvent e) { Point p = jTextField1.getLocationOnScreen(); new DPContent( new Point((int) p.getX(), (int) (p.getY() + jTextField1.getHeight())),jTextField1); }} /** * *减少按钮的边框,使按钮变小,外观协调。 * */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() { }} 不能通过编译啊!缺少DPContent类,自然new不了。 class DPContent extends JDialog implements ActionListener, MouseListener { Point p; //选择面板 private JPanel choosePanel = null; private JLabel choseLabel = null; //日历面板 private JPanel calendarPanel = null; private JPanel calendarPanelTitle = null; //日期头 private JPanel calendarPanelContent = null; //日历内容 //当前日期面板 private JPanel currentDatePanel = null; private DateChooserButton upMonth = null; private DateChooserButton downMonth = null; private Color currentDayColor = Color.YELLOW; //当前日期 private Calendar currentCalender = Calendar.getInstance(); int today = currentCalender.get(Calendar.DAY_OF_MONTH); //日历头 private String[] title = {"日", "一", "二", "三", "四", "五", "六"}; // private JButton[][] dayButton = new JButton[6][7]; //文本筐 private JTextField jText; public DPContent(Point p,JTextField jText) { try { this.jText=jText; this.p = p; jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { getContentPane().setLayout(new java.awt.BorderLayout()); this.createChoosePanel(currentCalender); getContentPane().add(choosePanel, java.awt.BorderLayout.NORTH); this.createcurrentDatePanel(currentCalender.getTime()); getContentPane().add(currentDatePanel, java.awt.BorderLayout.SOUTH); this.createCalendarPanel(); getContentPane().add(calendarPanel, java.awt.BorderLayout.CENTER); this.setResizable(false); this.setUndecorated(true); this.setLocation(p); this.setSize(200, 215); this.addWindowListener(new WindowAdapter() { public void windowDeactivated(WindowEvent e) { lostFocus(); } }); this.setVisible(true); } /** * 初始化选择面板 */ 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(); }} $ zz╭ ╮╭ ﹌╮. $$ z(o-.-o)(o-.-o) . $$ ┏~﹊︸ ̄~﹊︸ ̄~┓ $$ IT者-IT开发者的网站-- $$ 10万篇技术资料--天天更新 $$ -----www.itzhe.cn----- $ 请教gef内添加一个元素的调用流程 界面载入文件设计 谁能告诉我同步与异步的区别?? 高分求解(确认方法可行立即结帐) 关于rmic命令,急!!! 我刚接触java,想问各位,有没有java语法的什么帮助文件,比方说什么chm... 如何用JAVA做类似服务的一类程序? 何处有JAVA WEB SERVER FULL VERSION下载 我的泛型方法为何不支持int ArrayList实现了List接口,为什么没有重写其iterator();抽象方法???为什么 java怎么调用存储过程?希望给个完整的实例代码!!!!!! 如何刷新JList(我用的是netbeans的JList控件)
*日 一 二 三 四 五 六* |1月
========================================
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31========================================
*日 一 二 三 四 五 六* |2月
========================================
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28========================================
*日 一 二 三 四 五 六* |3月
========================================
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
========================================
*日 一 二 三 四 五 六* |4月
========================================
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30========================================
*日 一 二 三 四 五 六* |5月
========================================
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31========================================
*日 一 二 三 四 五 六* |6月
========================================
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
========================================
*日 一 二 三 四 五 六* |7月
========================================
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31========================================
*日 一 二 三 四 五 六* |8月
========================================
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31========================================
*日 一 二 三 四 五 六* |9月
========================================
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30========================================
*日 一 二 三 四 五 六* |10月
========================================
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31========================================
*日 一 二 三 四 五 六* |11月
========================================
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30========================================
*日 一 二 三 四 五 六* |12月
========================================
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
你自己专研一下好了
import java.awt.BorderLayout;import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.awt.Point;
import java.awt.Dimension;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.SwingConstants;
import java.text.SimpleDateFormat;
public DatePanel() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public String getDate(){
return jTextField1.getText();
}
public void setDate(String date){
jTextField1.setText(date);
} private void jbInit() throws Exception {
this.setLayout(gridBagLayout1); jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jTextField1.setMinimumSize(new Dimension(11, 16));
jTextField1.setPreferredSize(new Dimension(11, 16));
jButton1.setBorder(BorderFactory.createEtchedBorder());
this.add(jButton1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
, GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 8, 1));
this.add(jTextField1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
, GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 66, 1));
jTextField1.setBackground(Color.WHITE);
jTextField1.setEditable(false);
} JTextField jTextField1 = new JTextField();
DateChooserButton jButton1 = new DateChooserButton("\u25BC");
GridBagLayout gridBagLayout1 = new GridBagLayout();
private void jButton1_actionPerformed(ActionEvent e) {
Point p = jTextField1.getLocationOnScreen();
new DPContent(
new Point((int) p.getX(),
(int) (p.getY() + jTextField1.getHeight())),jTextField1); }
}
*
*减少按钮的边框,使按钮变小,外观协调。
*
*/
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() { }
}
Point p;
//选择面板
private JPanel choosePanel = null;
private JLabel choseLabel = null;
//日历面板
private JPanel calendarPanel = null;
private JPanel calendarPanelTitle = null; //日期头
private JPanel calendarPanelContent = null; //日历内容
//当前日期面板
private JPanel currentDatePanel = null;
private DateChooserButton upMonth = null;
private DateChooserButton downMonth = null; private Color currentDayColor = Color.YELLOW; //当前日期
private Calendar currentCalender = Calendar.getInstance();
int today = currentCalender.get(Calendar.DAY_OF_MONTH);
//日历头
private String[] title = {"日", "一", "二", "三", "四", "五", "六"};
//
private JButton[][] dayButton = new JButton[6][7]; //文本筐
private JTextField jText;
public DPContent(Point p,JTextField jText) { try {
this.jText=jText; this.p = p;
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception { getContentPane().setLayout(new java.awt.BorderLayout()); this.createChoosePanel(currentCalender);
getContentPane().add(choosePanel, java.awt.BorderLayout.NORTH); this.createcurrentDatePanel(currentCalender.getTime());
getContentPane().add(currentDatePanel, java.awt.BorderLayout.SOUTH); this.createCalendarPanel();
getContentPane().add(calendarPanel, java.awt.BorderLayout.CENTER); this.setResizable(false);
this.setUndecorated(true); this.setLocation(p);
this.setSize(200, 215);
this.addWindowListener(new WindowAdapter() {
public void windowDeactivated(WindowEvent e) {
lostFocus();
}
});
this.setVisible(true);
}
* 初始化选择面板
*/
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) { }}
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();
}
}
$ z(o-.-o)(o-.-o) . $
$ ┏~﹊︸ ̄~﹊︸ ̄~┓ $
$ IT者-IT开发者的网站-- $
$ 10万篇技术资料--天天更新 $
$ -----www.itzhe.cn----- $