- 浏览: 458201 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (538)
- C/C++ Primer (69)
- Objective-C Primer (102)
- Python Primer (19)
- JavaScript Primer (1)
- Java Primer (37)
- PHP Primer (17)
- 泛 Linux (37)
- Shell Script (21)
- APUE (21)
- UNP__1&2 (19)
- NetWork (7)
- Oracle周边 (38)
- Mysql里边 (6)
- Windows技 (9)
- 简单算法 & 数据结构 (14)
- 设计模式 (6)
- GTK历程 (12)
- 工具使用 (25)
- 杂事 (23)
- 一些概念 (17)
- Web方面 (10)
- myCodeTools (9)
- ^未 竟$ (13)
- 硬件通信 (2)
- Games (1)
最新评论
这个日历是改出来的。。。
JPanel版的
JDialog版
JPanel版的
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; import xserver.inventory.res.DateTools; public class CalendarControl extends JPanel implements ActionListener { public static void main(String[] av) { JFrame f = new JFrame("Cal"); Container c = f.getContentPane(); c.setLayout(new FlowLayout()); c.add(new CalendarControl()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); } private JButton b0; protected JButton btnDays[][]; private JComboBox monthChoice = new JComboBox(); private JComboBox yearChoice = new JComboBox(); protected int yy, mm, dd; protected int firstDayWeek = 0; private int activeDay = -1; private Calendar calendar = new GregorianCalendar(); protected final int thisYear = calendar.get(Calendar.YEAR); protected final int thisMonth = calendar.get(Calendar.MONTH);// 从0开始 CalendarControl() { super(); setYYMMDD(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar .get(Calendar.DAY_OF_MONTH)); buildGUI(); recompute(); } CalendarControl(int year, int month, int today) { super(); setYYMMDD(year, month, today); buildGUI(); recompute(); } private void buildGUI() { getAccessibleContext().setAccessibleDescription("Calendar not accessible yet. Sorry!"); setBorder(BorderFactory.createEtchedBorder()); setLayout(new BorderLayout()); yearsPanel(); daysPanel(); } private void setYYMMDD(int year, int month, int today) { yy = year; mm = month; dd = today; } public String getDateString() { return yy + "-" + mm + "-" + dd; } private void yearChoice() { for (int i = yy - 5; i < yy + 20; i++) yearChoice.addItem(Integer.toString(i)); yearChoice.setSelectedItem(Integer.toString(yy)); yearChoice.addActionListener(this); } private void monthChoice() { String[] months = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }; for (int i = 0; i < months.length; i++) monthChoice.addItem(months[i]); monthChoice.setSelectedItem(months[mm]); yearChoice.addActionListener(this); monthChoice.getAccessibleContext().setAccessibleName("Months"); monthChoice.getAccessibleContext().setAccessibleDescription("Choose a month of the year"); } private void yearsPanel() { JPanel ymPanel = new JPanel();// 年月面板 yearChoice(); monthChoice(); ymPanel.add(yearChoice); ymPanel.add(monthChoice); add(BorderLayout.CENTER, ymPanel); monthChoice.addActionListener(this); } public void actionPerformed(ActionEvent e) // 监听年月 { JComboBox comtobox = (JComboBox) e.getSource(); if (comtobox == yearChoice) { int i = yearChoice.getSelectedIndex(); if (i >= 0) { yy = Integer.parseInt(yearChoice.getSelectedItem().toString()); recompute(); System.out.println("xxx"); } return; } if (comtobox == monthChoice) { int i = monthChoice.getSelectedIndex(); if (i >= 0) { mm = i;// 月分下标由0开始 recompute(); } return; } } private void daysPanel() { JPanel daysPanel = new JPanel();// 日期面板 daysPanel.setLayout(new GridLayout(7, 7)); btnDays = new JButton[6][7]; // first row is days daysPanel.add(b0 = new JButton("日")); daysPanel.add(new JButton("一")); daysPanel.add(new JButton("二")); daysPanel.add(new JButton("三")); daysPanel.add(new JButton("四")); daysPanel.add(new JButton("五")); daysPanel.add(new JButton("六")); ActionListener dateSetter = new ActionListener()// 内部类,监听 { public void actionPerformed(ActionEvent e) { String day = e.getActionCommand(); if (!day.equals("")) { setDayActive(Integer.parseInt(day));// 设置选中的高亮 dd = Integer.parseInt(day); System.out.println(getDateString()); } } }; for (int i = 0; i < 6; i++) for (int j = 0; j < 7; j++) { daysPanel.add(btnDays[i][j] = new JButton("")); btnDays[i][j].addActionListener(dateSetter); } add(BorderLayout.SOUTH, daysPanel); } public final static int MONTH_DAYS[] = { // 平年2月为28天 31, 28, 31, 30, /* jan feb mar apr */ 31, 30, 31, 31, /* may jun jul aug */ 30, 31, 30, 31 /* sep oct nov dec */ }; public int getOneDayWeek(int yy, int mm, int dd) { Calendar calendar = new GregorianCalendar(yy, mm, dd); return calendar.get(Calendar.DAY_OF_WEEK) - 1; } /** Compute which days to put where, in the Cal panel */ protected void recompute() { if (mm < 0 || mm > 11) { throw new IllegalArgumentException("Month " + mm + " bad, must be 0-11"); } clearDayActive(); calendar = new GregorianCalendar(yy, mm, dd); firstDayWeek = DateTools.getMohtnFirstDayWeek(yy, mm); for (int i = 0; i < firstDayWeek; i++)// 一个月的一号之前的那几个空天 { btnDays[0][i].setText(""); } int daysCount = MONTH_DAYS[mm]; if (DateTools.isLeap(calendar.get(Calendar.YEAR)) && mm == 1) ++daysCount; // 闰年二月多一天 for (int i = 1; i <= daysCount; i++)// 画一个月 { JButton b = btnDays[(firstDayWeek + i - 1) / 7][(firstDayWeek + i - 1) % 7]; b.setText(Integer.toString(i)); System.out.println(i); } for (int i = firstDayWeek + daysCount; i < 6 * 7; i++)// 剩余空格 { btnDays[(i) / 7][(i) % 7].setText(""); } if (thisYear == yy && mm == thisMonth) setDayActive(dd); // shade the box for today repaint(); } public void setDayActive(int newDay)// 设置高亮 { clearDayActive(); if (newDay <= 0) dd = new GregorianCalendar().get(Calendar.DAY_OF_MONTH); else dd = newDay; Component square = btnDays[(firstDayWeek + newDay - 1) / 7][(firstDayWeek + newDay - 1) % 7]; square.setBackground(Color.red); square.repaint(); activeDay = newDay; } private void clearDayActive() // 取消高亮 { JButton b; if (activeDay > 0) { b = btnDays[(firstDayWeek + activeDay - 1) / 7][(firstDayWeek + activeDay - 1) % 7]; b.setBackground(b0.getBackground()); b.repaint(); activeDay = -1; } } }
JDialog版
package xserver.inventory.res; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calendar; import java.util.GregorianCalendar; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JPanel; public class CalendarDialog extends JDialog implements ActionListener { private JButton b0; private JButton btnDays[][]; private JComboBox monthChoice = new JComboBox(); private JComboBox yearChoice = new JComboBox(); private int yy, mm, dd; private int firstDayWeek = 0; private int activeDay = -1; private Calendar calendar = new GregorianCalendar(); private final int thisYear = calendar.get(Calendar.YEAR); private final int thisMonth = calendar.get(Calendar.MONTH);// 从0开始 public CalendarDialog(JFrame parent) { super(parent, "日历", true); setYYMMDD(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar .get(Calendar.DAY_OF_MONTH)); buildGUI(); recompute(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int high = (int) screenSize.getHeight()/ 4; int width = (int) screenSize.getWidth()/ 2; setLocation(width, high); setDefaultCloseOperation(DISPOSE_ON_CLOSE); pack(); setVisible(true); } private void buildGUI() { getAccessibleContext().setAccessibleDescription("Calendar not accessible yet. Sorry!"); setLayout(new BorderLayout()); yearsPanel(); daysPanel(); } private void setYYMMDD(int year, int month, int today) { yy = year; mm = month; dd = today; } public String getDateString() { return yy + "-" + (mm + 1) + "-" + dd; } private void yearChoice() { for (int i = yy - 5; i < yy + 20; i++) yearChoice.addItem(Integer.toString(i)); yearChoice.setSelectedItem(Integer.toString(yy)); yearChoice.addActionListener(this); } private void monthChoice() { String[] months = { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" }; for (int i = 0; i < months.length; i++) monthChoice.addItem(months[i]); monthChoice.setSelectedItem(months[mm]); yearChoice.addActionListener(this); monthChoice.getAccessibleContext().setAccessibleName("Months"); monthChoice.getAccessibleContext().setAccessibleDescription("Choose a month of the year"); } private void yearsPanel() { JPanel ymPanel = new JPanel();// 年月面板 yearChoice(); monthChoice(); ymPanel.add(yearChoice); ymPanel.add(monthChoice); add(BorderLayout.CENTER, ymPanel); monthChoice.addActionListener(this); } public void actionPerformed(ActionEvent e) { try { JComboBox comtobox = (JComboBox) e.getSource(); if (comtobox == yearChoice) { int i = yearChoice.getSelectedIndex(); if (i >= 0) { yy = Integer.parseInt(yearChoice.getSelectedItem().toString()); recompute(); } return; } if (comtobox == monthChoice) { int i = monthChoice.getSelectedIndex(); if (i >= 0) { mm = i;// 月分下标由0开始 recompute(); } return; } } catch (ClassCastException cce) { } String day = e.getActionCommand(); if (!day.equals("")) { setDayActive(Integer.parseInt(day));// 设置选中的高亮 dd = Integer.parseInt(day); setVisible(false); System.out.println(this.getDateString()); } } private void daysPanel() { JPanel daysPanel = new JPanel();// 日期面板 daysPanel.setLayout(new GridLayout(7, 7)); btnDays = new JButton[6][7]; // first row is days daysPanel.add(b0 = new JButton("日")); daysPanel.add(new JButton("一")); daysPanel.add(new JButton("二")); daysPanel.add(new JButton("三")); daysPanel.add(new JButton("四")); daysPanel.add(new JButton("五")); daysPanel.add(new JButton("六")); for (int i = 0; i < 6; i++) for (int j = 0; j < 7; j++) { daysPanel.add(btnDays[i][j] = new JButton("")); btnDays[i][j].addActionListener(this); } add(BorderLayout.SOUTH, daysPanel); } private final static int MONTH_DAYS[] = { // 平年2月为28天 31, 28, 31, 30, /* jan feb mar apr */ 31, 30, 31, 31, /* may jun jul aug */ 30, 31, 30, 31 /* sep oct nov dec */ }; public int getOneDayWeek(int yy, int mm, int dd) { Calendar calendar = new GregorianCalendar(yy, mm, dd); return calendar.get(Calendar.DAY_OF_WEEK) - 1; } /** Compute which days to put where, in the Cal panel */ private void recompute() { if (mm < 0 || mm > 11) { throw new IllegalArgumentException("Month " + mm + " bad, must be 0-11"); } clearDayActive(); calendar = new GregorianCalendar(yy, mm, dd); firstDayWeek = DateTools.getMohtnFirstDayWeek(yy, mm); for (int i = 0; i < firstDayWeek; i++)// 一个月的一号之前的那几个空天 { btnDays[0][i].setText(""); } int daysCount = MONTH_DAYS[mm]; if (DateTools.isLeap(calendar.get(Calendar.YEAR)) && mm == 1) ++daysCount; // 闰年二月多一天 for (int i = 1; i <= daysCount; i++)// 画一个月 { JButton b = btnDays[(firstDayWeek + i - 1) / 7][(firstDayWeek + i - 1) % 7]; b.setText(Integer.toString(i)); } for (int i = firstDayWeek + daysCount; i < 6 * 7; i++)// 剩余空格 { btnDays[(i) / 7][(i) % 7].setText(""); } if (thisYear == yy && mm == thisMonth) setDayActive(dd); // shade the box for today repaint(); } private void setDayActive(int newDay)// 设置高亮 { clearDayActive(); if (newDay <= 0) dd = new GregorianCalendar().get(Calendar.DAY_OF_MONTH); else dd = newDay; Component square = btnDays[(firstDayWeek + newDay - 1) / 7][(firstDayWeek + newDay - 1) % 7]; square.setBackground(Color.red); square.repaint(); activeDay = newDay; } private void clearDayActive() // 取消高亮 { JButton b; if (activeDay > 0) { b = btnDays[(firstDayWeek + activeDay - 1) / 7][(firstDayWeek + activeDay - 1) % 7]; b.setBackground(b0.getBackground()); b.repaint(); activeDay = -1; } } }
发表评论
-
Java__single process of file lock
2011-01-31 09:42 809import java.io.File; import ... -
C++__Read Section FiIe
2010-10-29 16:09 763#ifndef __SEC_FILE_H__ #defi ... -
C__Package Unix Socket
2010-10-27 10:15 872参考AUPE封的 #include "xsock ... -
C__time
2010-10-22 10:25 803http://blog.chinaunix.net/space ... -
Java os PhysicalAddress
2010-09-15 16:11 600import java.io.BufferedRead ... -
Java_String_Sort
2010-09-14 17:43 1675import java.util.ArrayList; ... -
JPanel image Background
2010-09-06 11:31 1209在这里找到的。 http://topic.csdn.net/u ... -
随机数rand
2010-06-07 14:54 706#include <stdlib.h> #i ...
相关推荐
"calendar_swing_java_character1wy_" 标题中的"日历控件"指的是一个特定的Swing组件,设计用于展示和选择日期。这个组件可能由开发者character1wy创建或改良,以提供更加美观和易用的日期选择体验。 日历控件在...
A Java calendar program using Swing GUI components. It includes an executable jar file.Calendars from 1583 onwards can be generated. An Easter date calculation algorithm is includes.The full source ...
这个名为"java_calendar_with_alert.rar_memo_定时提醒"的压缩包文件提供了一个结合了Java日历功能和Memo提醒功能的解决方案。下面将详细探讨这两个主要知识点。 一、Java日历(Calendar)类 Java的`java.util....
Java中的`Calendar`类是处理日期和时间的核心组件,它是一个抽象类,提供了对日期和时间的全面操作。在Java编程中,`Calendar`控件是用于创建、修改和查询日期的重要工具。这个控件的功能非常强大,可以满足各种复杂...
开发者可能使用了Java Swing或JavaFX来创建用户界面,或者使用RESTful API与服务器进行交互。 4. **会议管理(Meeting Management)**: 会议管理通常涉及到创建、编辑和删除会议,还包括提醒设置。开发者可能使用...
在旧版的Java中,我们通常使用`java.util.Date`和`java.util.Calendar`类来操作日期。然而,从Java 8开始,推荐使用`java.time`包中的类,如`LocalDate`,因为它们提供了更直观和强大的API。 对于这个“DateDiff_...
- 引入相应的库,如`javax.swing.JDateChooser`或第三方库`com.toedter.calendar.JCalendar`。 - 创建控件实例,如`JDateChooser dateChooser = new JDateChooser();`或`JCalendar calendar = new JCalendar();` - ...
在开发像CalendarPad这样的桌面应用时,Java的Swing或JavaFX库可以提供丰富的图形用户界面(GUI)组件,用于构建日历和日记编辑器等界面元素。 在【压缩包子文件的文件名称列表】中,我们看到一个名为"CalendarPad....
Java Swing 是Java GUI(图形用户界面)开发的一个重要库,它提供了一系列丰富的组件,用于创建桌面应用程序。在Swing中,DatePicker是一个非常实用的控件,允许用户方便地选择日期,通常用于输入或显示日期相关的...
Java Calendar 是Java编程语言中处理日期和时间的核心类库,它是`java.util.Calendar`类的实例,用于创建、操作和格式化日期。在Java中,日期和时间的管理是通过`Calendar`类以及相关的辅助类如`Date`、`...
在Java Swing库中,DatePicker是一个常用的用户界面组件,它允许用户选择日期。Swing本身并不直接提供一个标准的DatePicker组件,但可以通过第三方库或者自定义组件实现。在本篇文章中,我们将深入探讨如何在Java ...
JavaSwing日历控件宝典 JavaSwing日历控件宝典是Java Swing编程中一个非常实用的控件,旨在提供一个美观易用的日期选择器控件。下面是关于该控件的详细说明。 控件概述 JavaSwing日历控件是一个继承自JComponent...
例如,你可以使用`new JCalendar()`创建一个日历实例,然后添加到你的Swing容器中,如`add(new JScrollPane(calendar))`。 2. **JSpinner日期选择器**: Swing内置的JSpinner可以用来创建一个日期选择器。JSpinner...
Java Swing 是Java GUI(图形用户界面)库的一部分,用于创建桌面应用程序。在Swing中,日期控件主要用于显示和选择日期,为用户提供直观的日期选择界面。本篇文章将深入探讨Java Swing中的日期控件,包括其功能、...
总的来说,"Calendar万年历.rar"这个资源包为我们提供了一种学习和实践Java日期处理,特别是使用`Calendar`类和Swing构建GUI应用的机会。通过理解和应用其中的代码,开发者可以提升在Java日期操作和GUI编程方面的...
Java中的CalendarPad是一款实用的日历记事提醒工具,它整合了日历和记事本的功能,帮助用户高效地管理日常事务。在这个项目中,我们主要关注的是如何利用Java编程语言来实现这样的功能。 首先,我们要了解Java中的...
在Java编程领域,Swing库是用于创建图形用户界面(GUI)的重要工具。"Swing编写的日历和时钟"这个主题涉及到如何利用Swing组件来构建一个实用的日历和时钟应用。下面我们将详细探讨相关的知识点。 1. **Swing基础**...
Swing日历插件`datepicker.jar`是Java Swing框架中用于日期选择的一种实用工具,它为用户界面提供了直观且易于使用的日历组件。这个插件允许开发者在应用程序中添加日期选择功能,支持选择年、月、日、小时、分钟和...
Java Swing 是Java GUI(图形用户界面)库的一部分,它提供了丰富的组件来构建桌面应用程序。在Swing中,处理日期选择的控件主要是JCalendar和JDatePicker。这两个组件可以帮助用户在界面上选择日期,并且可以方便地...
通过研究这个例子,开发者可以学习到如何使用Java Swing创建复杂的用户界面,理解组件交互,以及掌握与日期处理相关的Java类,如java.util.Calendar和java.time.LocalDate。此外,还能了解到如何将抽象的GUI设计转化...