Java Swing的日历控件谁有啊! 我现在需要一个Swing的日历控件,哪位有的话帮帮忙! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的JDK版本是多少, 我有, 我上传到我的资源, 自己去找 有开源的,http://sourceforge.net/projects/datepicker/ 我有jdk1.6的,自己写的。自己写也并不复杂啊。 MyEclipse中的matisse中有这个swing控件啊 看下这个吧package com.gui;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Calendar;import java.util.GregorianCalendar;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JPanel;public class MyRiLi extends JPanel { protected int yy; protected int mm, dd; /** 存放按钮 */ protected JButton labs[][]; /** 每个月份开头的几个空白日期 */ protected int leadGap = 0; /** 日期对象 */ Calendar calendar = new GregorianCalendar(); /** 年份 */ protected final int thisYear = calendar.get(Calendar.YEAR); /** 月份 */ protected final int thisMonth = calendar.get(Calendar.MONTH); /** 取消按钮高亮时以这个按钮为基准 */ private JButton b0; /** 存放月份 */ private JComboBox monthChoice; /** 存放年份 */ private JComboBox yearChoice; /** * 构造方法,获取今天的日期 */ MyRiLi() { super(); setYYMMDD(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); buildGUI(); recompute(); } MyRiLi(int year, int month, int today) { super(); setYYMMDD(year, month, today); buildGUI(); recompute(); } private void setYYMMDD(int year, int month, int today) { yy = year; mm = month; dd = today; } String[] months = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" }; private void buildGUI() { getAccessibleContext().setAccessibleDescription("日历未上载"); setBorder(BorderFactory.createEtchedBorder()); setLayout(new BorderLayout()); JPanel tp = new JPanel(); tp.add(monthChoice = new JComboBox()); for (int i = 0; i < months.length; i++) monthChoice.addItem(months[i]); monthChoice.setSelectedItem(months[mm]); monthChoice.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { int i = monthChoice.getSelectedIndex(); if (i >= 0) { mm = i; recompute(); } } }); monthChoice.getAccessibleContext().setAccessibleName("Months"); monthChoice.getAccessibleContext().setAccessibleDescription("选择一个月份"); tp.add(yearChoice = new JComboBox()); yearChoice.setEditable(true); for (int i = yy - 5; i < yy + 5; i++) yearChoice.addItem(Integer.toString(i)); yearChoice.setSelectedItem(Integer.toString(yy)); yearChoice.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { int i = yearChoice.getSelectedIndex(); if (i >= 0) { yy = Integer.parseInt(yearChoice.getSelectedItem() .toString()); recompute(); } } }); add(BorderLayout.CENTER, tp); JPanel bp = new JPanel(); bp.setLayout(new GridLayout(7, 7)); labs = new JButton[6][7]; // 星期 bp.add(b0 = new JButton("日")); bp.add(new JButton("一")); bp.add(new JButton("二")); bp.add(new JButton("三")); bp.add(new JButton("四")); bp.add(new JButton("五")); bp.add(new JButton("六")); ActionListener dateSetter = new ActionListener() { public void actionPerformed(ActionEvent e) { String num = e.getActionCommand(); if (!num.equals("")) { // 将当前日期高亮 setDayActive(Integer.parseInt(num)); } } }; // 创建所有的按钮 for (int i = 0; i < 6; i++) for (int j = 0; j < 7; j++) { bp.add(labs[i][j] = new JButton("")); labs[i][j].addActionListener(dateSetter); } add(BorderLayout.SOUTH, bp); } // 列出每个月的天数 public final static int dom[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /** 计算每个月份的日期,显示在面板上 */ protected void recompute() { if (mm < 0 || mm > 11) throw new IllegalArgumentException("月份 " + mm + " 必须在0-11之间"); clearDayActive(); calendar = new GregorianCalendar(yy, mm, dd); // 计算每个月份开头的几个空白日期 leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1; int daysInMonth = dom[mm]; if (isLeap(calendar.get(Calendar.YEAR)) && mm > 1) ++daysInMonth; for (int i = 0; i < leadGap; i++) { labs[0][i].setText(""); } // 填入日期数字 for (int i = 1; i <= daysInMonth; i++) { JButton b = labs[(leadGap + i - 1) / 7][(leadGap + i - 1) % 7]; b.setText(Integer.toString(i)); } for (int i = leadGap + 1 + daysInMonth; i < 6 * 7; i++) { labs[(i) / 7][(i) % 7].setText(""); } if (thisYear == yy && mm == thisMonth) setDayActive(dd); repaint(); } /** * 判断是否闰年 */ public boolean isLeap(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true; return false; } /** 设置年月日 */ public void setDate(int yy, int mm, int dd) { this.yy = yy; this.mm = mm; this.dd = dd; recompute(); } /** 取消按钮高亮 */ private void clearDayActive() { JButton b; if (activeDay > 0) { b = labs[(leadGap + activeDay - 1) / 7][(leadGap + activeDay - 1) % 7]; b.setBackground(b0.getBackground()); b.repaint(); activeDay = -1; } } private int activeDay = -1; /** 设置按钮高亮 */ public void setDayActive(int newDay) { clearDayActive(); if (newDay <= 0) dd = new GregorianCalendar().get(Calendar.DAY_OF_MONTH); else dd = newDay; Component square = labs[(leadGap + newDay - 1) / 7][(leadGap + newDay - 1) % 7]; square.setBackground(Color.red); square.repaint(); activeDay = newDay; } public static void main(String[] av) { JFrame f = new JFrame("MyRiLi"); Container c = f.getContentPane(); c.setLayout(new FlowLayout()); c.add(new MyRiLi()); f.pack(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }} 开车的人不应跑去修路 JAVA时间统计原点难道不是以1970-01-01? 跟抽象类有关的问题 ★★★请高手帮我分析一下,下面这段程序为什么会进入死循环?★★★ 使用Java如何在创建文件时,选择要创建文件的编码(注意:不是文件中内容的编码) 新手请教关于package的问题 想加一段代码 我都发三回了,没人理我! 怎么让Eclipse支持JDK1.5? JAVA服务器端的编译哪里有得下载??怎么安装。。。。 江湖救急啊!!刚编了个简单的Java程序 不知道为什么出错 大家看下啊 string转换int报错
MyEclipse中的matisse中有这个swing控件啊
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;public class MyRiLi extends JPanel { protected int yy; protected int mm, dd; /** 存放按钮 */
protected JButton labs[][]; /** 每个月份开头的几个空白日期 */
protected int leadGap = 0; /** 日期对象 */
Calendar calendar = new GregorianCalendar(); /** 年份 */
protected final int thisYear = calendar.get(Calendar.YEAR); /** 月份 */
protected final int thisMonth = calendar.get(Calendar.MONTH); /** 取消按钮高亮时以这个按钮为基准 */
private JButton b0; /** 存放月份 */
private JComboBox monthChoice; /** 存放年份 */
private JComboBox yearChoice; /**
* 构造方法,获取今天的日期
*/
MyRiLi() {
super();
setYYMMDD(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
buildGUI();
recompute();
} MyRiLi(int year, int month, int today) {
super();
setYYMMDD(year, month, today);
buildGUI();
recompute();
} private void setYYMMDD(int year, int month, int today) {
yy = year;
mm = month;
dd = today;
} String[] months = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月",
"十月", "十一月", "十二月" }; private void buildGUI() {
getAccessibleContext().setAccessibleDescription("日历未上载");
setBorder(BorderFactory.createEtchedBorder()); setLayout(new BorderLayout()); JPanel tp = new JPanel();
tp.add(monthChoice = new JComboBox());
for (int i = 0; i < months.length; i++)
monthChoice.addItem(months[i]);
monthChoice.setSelectedItem(months[mm]);
monthChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = monthChoice.getSelectedIndex();
if (i >= 0) {
mm = i;
recompute();
}
}
});
monthChoice.getAccessibleContext().setAccessibleName("Months");
monthChoice.getAccessibleContext().setAccessibleDescription("选择一个月份"); tp.add(yearChoice = new JComboBox());
yearChoice.setEditable(true);
for (int i = yy - 5; i < yy + 5; i++)
yearChoice.addItem(Integer.toString(i));
yearChoice.setSelectedItem(Integer.toString(yy));
yearChoice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int i = yearChoice.getSelectedIndex();
if (i >= 0) {
yy = Integer.parseInt(yearChoice.getSelectedItem()
.toString());
recompute();
}
}
});
add(BorderLayout.CENTER, tp); JPanel bp = new JPanel();
bp.setLayout(new GridLayout(7, 7));
labs = new JButton[6][7]; // 星期
bp.add(b0 = new JButton("日"));
bp.add(new JButton("一"));
bp.add(new JButton("二"));
bp.add(new JButton("三"));
bp.add(new JButton("四"));
bp.add(new JButton("五"));
bp.add(new JButton("六")); ActionListener dateSetter = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = e.getActionCommand();
if (!num.equals("")) {
// 将当前日期高亮
setDayActive(Integer.parseInt(num));
}
}
}; // 创建所有的按钮
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++) {
bp.add(labs[i][j] = new JButton(""));
labs[i][j].addActionListener(dateSetter);
} add(BorderLayout.SOUTH, bp);
} // 列出每个月的天数
public final static int dom[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 }; /** 计算每个月份的日期,显示在面板上 */
protected void recompute() {
if (mm < 0 || mm > 11)
throw new IllegalArgumentException("月份 " + mm + " 必须在0-11之间");
clearDayActive();
calendar = new GregorianCalendar(yy, mm, dd); // 计算每个月份开头的几个空白日期
leadGap = new GregorianCalendar(yy, mm, 1).get(Calendar.DAY_OF_WEEK) - 1; int daysInMonth = dom[mm];
if (isLeap(calendar.get(Calendar.YEAR)) && mm > 1)
++daysInMonth; for (int i = 0; i < leadGap; i++) {
labs[0][i].setText("");
} // 填入日期数字
for (int i = 1; i <= daysInMonth; i++) {
JButton b = labs[(leadGap + i - 1) / 7][(leadGap + i - 1) % 7];
b.setText(Integer.toString(i));
} for (int i = leadGap + 1 + daysInMonth; i < 6 * 7; i++) {
labs[(i) / 7][(i) % 7].setText("");
} if (thisYear == yy && mm == thisMonth)
setDayActive(dd); repaint();
} /**
* 判断是否闰年
*/
public boolean isLeap(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
return false;
} /** 设置年月日 */
public void setDate(int yy, int mm, int dd) {
this.yy = yy;
this.mm = mm;
this.dd = dd;
recompute();
} /** 取消按钮高亮 */
private void clearDayActive() {
JButton b; if (activeDay > 0) {
b = labs[(leadGap + activeDay - 1) / 7][(leadGap + activeDay - 1) % 7];
b.setBackground(b0.getBackground());
b.repaint();
activeDay = -1;
}
} private int activeDay = -1; /** 设置按钮高亮 */
public void setDayActive(int newDay) { clearDayActive(); if (newDay <= 0)
dd = new GregorianCalendar().get(Calendar.DAY_OF_MONTH);
else
dd = newDay;
Component square = labs[(leadGap + newDay - 1) / 7][(leadGap + newDay - 1) % 7];
square.setBackground(Color.red);
square.repaint();
activeDay = newDay;
} public static void main(String[] av) {
JFrame f = new JFrame("MyRiLi");
Container c = f.getContentPane();
c.setLayout(new FlowLayout()); c.add(new MyRiLi()); f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}