java中如何实现日历控件的程序???? Java GUI 设计----java中如何实现日历控件的程序?谢谢赐教! 感激! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我有一个在别人代码基础上改写的例子,但是太大不适合在此贴源代码,需要的话,给我发邮件:[email protected] sourceforge上有很多实现这个功能的开源项目,去搜一搜吧 给我发一个吧,谢谢了! [email protected] canlender类是日历的单词不知道打的对不对,见鬼,好久不用 了 liujiwe79(专业做控件)给我发一个吧!让小弟也研究一下! [email protected]给我也发一个。学习学习 [email protected] 也给我发一个 挺感兴趣的 谢谢昂!!! 我也想要一份,学习学习,谢谢谢谢。:)[email protected] http://blog.csdn.net/diggywang/archive/2006/11/23/1406821.aspx我以前整理的 http://blog.csdn.net/diggywang/archive/2006/07/20/946981.aspx俺的经验 我用了一个Swing Lab里的,挺好用叫jdesktop,上google搜一下下下来就可以用了 可以发份给我吗? [email protected] 网上搜索一下OpenSwing吧,开源的,里面有日历控件的实现。是继承JComboBox搞的,不错 也给我一个哈:[email protected] eclipse 3.3 里面整合了日历控件. 给我发一个吧[email protected] 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 调用中国移动接口 实现往手机发短信 JAVA符号占位问题. 请教一个树形实例的算法 一个赋有挑战性的JAVA问题哈!(网友们帮帮忙啊!谢了哈) 请教一个nio的问题。 一个小问题,难道这是API里的一个bug?请高手解释一下 同一项目中怎样调用别一文件的类? 超级高手COME 给出满意答案给分1000分 程序动态加载时怎样传递参数? 不同包之间import的问题 散分200: spring, ejb的集群讨论,高手请进!
[email protected]
单词不知道打的对不对,见鬼,好久不用 了
给我也发一个。学习学习
也给我发一个 挺感兴趣的 谢谢昂!!!
[email protected]
我以前整理的
俺的经验
叫jdesktop,上google搜一下下下来就可以用了
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);
}}