java日历界面源代码 javaswing日历窗口

怎样用java编写日历

import java.awt.BorderLayout;

成都创新互联公司长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为凤翔企业提供专业的成都网站制作、成都网站建设,凤翔网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.SystemColor;

import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

import java.awt.event.MouseEvent;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.Locale;

import java.util.Date;

import java.util.StringTokenizer;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.JToggleButton;

import javax.swing.SwingConstants;

import javax.swing.UIManager;

/**

* pTitle: Swing日历/p

* pDescription: 操作日期/p

* @author duxu2004

* @version 1.0.1

*/

class JCalendar extends JPanel{

//动态表示年月日

private int year=0;

private int month=0;

private int day=0;

//主面板

private JPanel Main = new JPanel();

//日面板

private JPanel jPanelDay = new JPanel();

//月面板

private JPanel jPanelMonth = new JPanel();

//年的输入位置

private JTextField Year = new JTextField();

//月的输入位置

private JTextField Month = new JTextField();

//减少月份

private JButton MonthDown = new JButton();

//增加月份

private JButton MonthUp = new JButton();

private JPanel jPanelButton = new JPanel();

//减少年份

private JButton YearDown = new JButton();

//增加年份

private JButton YearUp = new JButton();

//显示日期的位置

private JLabel Out = new JLabel();

//中国时区,以后可以从这里扩展可以设置时区的功能

private Locale l=Locale.CHINESE;

//主日历

private GregorianCalendar cal=new GregorianCalendar(l);

//星期面板

private JPanel weekPanel=new JPanel();

//天按钮组

private JToggleButton[] days=new JToggleButton[42];

//天面板

private JPanel Days = new JPanel();

//标示

private JLabel jLabel1 = new JLabel();

private JLabel jLabel2 = new JLabel();

private JLabel jLabel3 = new JLabel();

private JLabel jLabel4 = new JLabel();

private JLabel jLabel5 = new JLabel();

private JLabel jLabel6 = new JLabel();

private JLabel jLabel7 = new JLabel();

//当前选择的天数按钮

private JToggleButton cur=null;

//月份天数数组,用来取得当月有多少天

// 1 2 3 4 5 6 7 8 9 10 11 12

private int[] mm={31,28,31,30,31,30,31,31,30,31,30,31};

//空日期构造函数

public JCalendar() {

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

//带日期设置的构造函数

public JCalendar(int year, int month, int day) {

cal.set(year, month, day);

try {

jbInit();

}

catch (Exception e) {

e.printStackTrace();

}

}

//带日历输入的构造函数

public JCalendar(GregorianCalendar calendar) {

cal=calendar;

try {

jbInit();

}

catch (Exception e) {

e.printStackTrace();

}

}

//带日期输入的构造函数

public JCalendar(Date date) {

cal.setTime(date);

try {

jbInit();

}

catch (Exception e) {

e.printStackTrace();

}

}

//初始化组件

private void jbInit() throws Exception {

//初始化年、月、日

iniCalender();

this.setLayout(new BorderLayout());

this.setBorder(BorderFactory.createRaisedBevelBorder());

this.setMaximumSize(new Dimension(200, 200));

this.setMinimumSize(new Dimension(200, 200));

this.setPreferredSize(new Dimension(200, 200));

Main.setLayout(new BorderLayout());

Main.setBackground(SystemColor.info);

Main.setBorder(null);

Out.setBackground(Color.lightGray);

Out.setHorizontalAlignment(SwingConstants.CENTER);

Out.setMaximumSize(new Dimension(100, 19));

Out.setMinimumSize(new Dimension(100, 19));

Out.setPreferredSize(new Dimension(100, 19));

jLabel1.setForeground(Color.red);

jLabel1.setHorizontalAlignment(SwingConstants.CENTER);

jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel1.setText("日");

jLabel2.setForeground(Color.blue);

jLabel2.setHorizontalAlignment(SwingConstants.CENTER);

jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel2.setText("六");

jLabel3.setHorizontalAlignment(SwingConstants.CENTER);

jLabel3.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel3.setText("五");

jLabel4.setHorizontalAlignment(SwingConstants.CENTER);

jLabel4.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel4.setText("四");

jLabel5.setHorizontalAlignment(SwingConstants.CENTER);

jLabel5.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel5.setText("三");

jLabel6.setBorder(null);

jLabel6.setHorizontalAlignment(SwingConstants.CENTER);

jLabel6.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel6.setText("二");

jLabel7.setBackground(Color.lightGray);

jLabel7.setForeground(Color.black);

jLabel7.setBorder(null);

jLabel7.setHorizontalAlignment(SwingConstants.CENTER);

jLabel7.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel7.setText("一");

weekPanel.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));

weekPanel.setBorder(BorderFactory.createEtchedBorder());

weekPanel.setLayout(new GridLayout(1,7));

weekPanel.add(jLabel1, null);

weekPanel.add(jLabel7, null);

weekPanel.add(jLabel6, null);

weekPanel.add(jLabel5, null);

weekPanel.add(jLabel4, null);

weekPanel.add(jLabel3, null);

weekPanel.add(jLabel2, null);

MonthUp.setAlignmentX((float) 0.0);

MonthUp.setActionMap(null);

jPanelMonth.setBackground(SystemColor.info);

jPanelMonth.setLayout(new BorderLayout());

jPanelMonth.setBorder(BorderFactory.createEtchedBorder());

Month.setBorder(null);

Month.setHorizontalAlignment(SwingConstants.CENTER);

Month.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(MouseEvent e) {

Month_mouseClicked(e);

}

});

Month.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(KeyEvent e) {

Month_keyPressed(e);

}

});

MonthDown.setBorder(null);

MonthDown.setText("\u25C4");

MonthDown.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

MonthDown_actionPerformed(e);

}

});

MonthUp.setBorder(null);

MonthUp.setText("\u25BA");

MonthUp.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

MonthUp_actionPerformed(e);

}

});

jPanelButton.setLayout(null);

jPanelButton.setBorder(null);

jPanelButton.addComponentListener(new java.awt.event.ComponentAdapter() {

public void componentResized(java.awt.event.ComponentEvent evt) {

jPanelButtonComponentResized(evt);

}

});

Year.setBorder(BorderFactory.createEtchedBorder());

Year.setMaximumSize(new Dimension(80, 25));

Year.setMinimumSize(new Dimension(80, 25));

Year.setPreferredSize(new Dimension(80, 25));

Year.setHorizontalAlignment(SwingConstants.CENTER);

Year.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(MouseEvent e) {

Year_mouseClicked(e);

}

});

Year.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(KeyEvent e) {

Year_keyPressed(e);

}

});

YearDown.setBorder(null);

YearDown.setMaximumSize(new Dimension(16, 16));

YearDown.setMinimumSize(new Dimension(16, 16));

YearDown.setPreferredSize(new Dimension(16, 16));

YearDown.setSize(new Dimension(16, 16));

YearDown.setText("▼");

YearDown.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

YearDown_actionPerformed(e);

}

});

YearUp.setBorder(null);

YearUp.setMaximumSize(new Dimension(16, 16));

YearUp.setMinimumSize(new Dimension(16, 16));

YearUp.setPreferredSize(new Dimension(16, 16));

YearUp.setSize(new Dimension(16, 16));

YearUp.setText("▲");

YearUp.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

YearUp_actionPerformed(e);

}

});

jPanelDay.setLayout(new BorderLayout());

Days.setLayout(new GridLayout(6,7));

Days.setBackground(SystemColor.info);

for(int i=0;i42;i++){

days[i]=new JToggleButton();

days[i].setBorder(null);

days[i].setBackground(SystemColor.info);

days[i].setHorizontalAlignment(SwingConstants.CENTER);

days[i].setHorizontalTextPosition(SwingConstants.CENTER);

//days[i].setSize(l,l);

days[i].addActionListener(new java.awt.event.ActionListener(){

public void actionPerformed(ActionEvent e) {

day=Integer.parseInt(((JToggleButton)e.getSource()).getText());

showDate();

showDays();

}

});

Days.add(days[i]);

}

this.add(Main, BorderLayout.NORTH);

this.add(jPanelDay, BorderLayout.CENTER);

this.add(jPanelMonth, BorderLayout.SOUTH);

Main.add(Year, BorderLayout.CENTER);

Main.add(Out, BorderLayout.WEST);

Main.add(jPanelButton, BorderLayout.EAST);

jPanelButton.add(YearUp);

jPanelButton.add(YearDown);

jPanelDay.add(weekPanel,BorderLayout.NORTH);

jPanelDay.add(Days, BorderLayout.CENTER);

jPanelMonth.add(Month, BorderLayout.CENTER);

jPanelMonth.add(MonthDown, BorderLayout.WEST);

jPanelMonth.add(MonthUp, BorderLayout.EAST);

showMonth();

showYear();

showDate();

showDays();

}

//自定义重画年选择面板

void jPanelButtonComponentResized(java.awt.event.ComponentEvent evt){

YearUp.setLocation(0,0);

YearDown.setLocation(0,YearUp.getHeight());

jPanelButton.setSize(YearUp.getWidth(),YearUp.getHeight()*2);

jPanelButton.setPreferredSize(new Dimension(YearUp.getWidth(),YearUp.getHeight()*2));

jPanelButton.updateUI();

}

//测试用

public static void main(String[] args){

JFrame f=new JFrame();

f.setContentPane(new JCalendar());

f.pack();

//f.setResizable(false);

f.show();

}

//增加年份

void YearUp_actionPerformed(ActionEvent e) {

year++;

showYear();

showDate();

showDays();

}

//减少年份

void YearDown_actionPerformed(ActionEvent e) {

year--;

showYear();

showDate();

showDays();

}

//减少月份

void MonthDown_actionPerformed(ActionEvent e) {

month--;

if(month0) {

month = 11;

year--;

showYear();

}

showMonth();

showDate();

showDays();

}

//增加月份

void MonthUp_actionPerformed(ActionEvent e) {

month++;

if(month==12) {

month=0;

year++;

showYear();

}

showMonth();

showDate();

showDays();

}

//初始化年月日

void iniCalender(){

year=cal.get(Calendar.YEAR);

month=cal.get(Calendar.MONTH);

day=cal.get(Calendar.DAY_OF_MONTH);

}

//刷新月份

void showMonth(){

Month.setText(Integer.toString(month+1)+"月");

}

//刷新年份

void showYear(){

Year.setText(Integer.toString(year)+"年");

}

//刷新日期

void showDate(){

Out.setText(Integer.toString(year)+"-"+Integer.toString(month+1)+"-"+Integer.toString(day));

}

//重画天数选择面板

void showDays() {

cal.set(year,month,1);

int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

int n=mm[month];

if(cal.isLeapYear(year)month==1) n++;

int i=0;

for(;ifirstDayOfWeek-1;i++){

days[i].setEnabled(false);

days[i].setSelected(false);

days[i].setText("");

}

int d=1;

for(;d=n;d++){

days[i].setText(Integer.toString(d));

days[i].setEnabled(true);

if(d==day) days[i].setSelected(true);

else days[i].setSelected(false);;

i++;

}

for(;i42;i++){

days[i].setEnabled(false);

days[i].setSelected(false);

days[i].setText("");

}

}

//单击年份面板选择整个年份字符串

void SelectionYear(){

Year.setSelectionStart(0);

Year.setSelectionEnd(Year.getText().length());

}

//单击月份面板选择整个月份字符串

void SelectionMonth(){

Month.setSelectionStart(0);

Month.setSelectionEnd(Month.getText().length());

}

//月份面板响应鼠标单击事件

void Month_mouseClicked(MouseEvent e) {

//SelectionMonth();

inputMonth();

}

//检验输入的月份

void inputMonth(){

String s;

if(Month.getText().endsWith("月"))

{

s=Month.getText().substring(0,Month.getText().length()-1);

}

else s=Month.getText();

month=Integer.parseInt(s)-1;

this.showMe();

}

//月份面板键盘敲击事件响应

void Month_keyPressed(KeyEvent e) {

if(e.getKeyChar()==10)

inputMonth();

}

//年份面板响应鼠标单击事件

void Year_mouseClicked(MouseEvent e) {

//SelectionYear();

inputYear();

}

//年份键盘敲击事件响应

void Year_keyPressed(KeyEvent e) {

//System.out.print(new Integer(e.getKeyChar()).byteValue());

if(e.getKeyChar()==10)

inputYear();

}

//检验输入的年份字符串

void inputYear() {

String s;

if(Year.getText().endsWith("年"))

{

s=Year.getText().substring(0,Year.getText().length()-1);

}

else s=Year.getText();

year=Integer.parseInt(s);

this.showMe();

}

//以字符串形式返回日期,yyyy-mm-dd

public String getDate(){return Out.getText();}

//以字符串形式输入日期,yyyy-mm-dd

public void setDate(String date){

if(date!=null){

StringTokenizer f = new StringTokenizer(date, "-");

if(f.hasMoreTokens())

year = Integer.parseInt(f.nextToken());

if(f.hasMoreTokens())

month = Integer.parseInt(f.nextToken());

if(f.hasMoreTokens())

day = Integer.parseInt(f.nextToken());

cal.set(year,month,day);

}

this.showMe();

}

//以日期对象形式输入日期

public void setTime(Date date){

cal.setTime(date);

this.iniCalender();

this.showMe();

}

//返回日期对象

public Date getTime(){return cal.getTime();}

//返回当前的日

public int getDay() {

return day;

}

//设置当前的日

public void setDay(int day) {

this.day = day;

cal.set(this.year,this.month,this.day);

this.showMe();

}

//设置当前的年

public void setYear(int year) {

this.year = year;

cal.set(this.year,this.month,this.day);

this.showMe();

}

//返回当前的年

public int getYear() {

return year;

}

//返回当前的月

public int getMonth() {

return month;

}

//设置当前的月

public void setMonth(int month) {

this.month = month;

cal.set(this.year,this.month,this.day);

this.showMe();

}

//刷新

public void showMe(){

this.showDays();

this.showMonth();

this.showYear();

this.showDate();

}

}

public class TestJCalendar {

public static void main(String[] args) {

JFrame f=new JFrame();

f.setContentPane(new JCalendar());

f.pack();

//f.setResizable(false);

f.show();

}

}

java核心技术的日历程序,模仿着写出来发现一个星期的第一天是星期日,试了各种方法都改不了,附代码

你的问题就是把一周的第一天设置为星期一?

那你贴这么多代码干嘛,一句话的事儿吗不是。建议你多看一下api

Calendar里面的方法就有啊, setFirstDayOfWeek

JAVA日历的看法心得体会…原代码如下

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.Panel;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import java.util.Calendar;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class DateCalendar extends JFrame implements ItemListener{

private static final long serialVersionUID = 1L;

private JComboBox year;

private JComboBox month;

private JLabel l[];

Calendar c=Calendar.getInstance();

public DateCalendar() {

year=new JComboBox();

for (int i = 1990; i 2020; i++) {

year.addItem(i+"");

}

month=new JComboBox();

for (int i = 1; i =12; i++) {

month.addItem(i+"");

}

JPanel p=new JPanel();

p.add(new JLabel("请 选 择:"));

p.add(year);

p.add(new JLabel("年 "));

p.add(month);

p.add(new JLabel("日 "));

JPanel p1=new JPanel();

l=new JLabel[49];

for (int i = 0; i l.length; i++) {

l[i]=new JLabel("");

l[i].setHorizontalAlignment(JLabel.CENTER);

p1.add(l[i]);

}

l[0].setText("日");

l[1].setText("一");

l[2].setText("二");

l[3].setText("三");

l[4].setText("四");

l[5].setText("五");

l[6].setText("六");

year.addItemListener(this);

month.addItemListener(this);

p1.setLayout(new GridLayout(7,7));

this.init();

this.getContentPane().add(p1);

this.getContentPane().add(p,BorderLayout.NORTH);

this.setSize(200, 300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.pack();

this.setLocation(400, 200);

this.setVisible(true);

}

//第一次实例化对象时,把面板信息显示为当前月的信息

public void init(){

nian=c.get(Calendar.YEAR);

yue=c.get(Calendar.MONTH)+1;

ri=c.get(Calendar.DAY_OF_MONTH);

year.setSelectedItem(nian+"");

month.setSelectedItem(yue+"");

this.setCalendar();//调用显示面板信息设置

}

int nian,yue,ri;

//下拉事件

public void itemStateChanged(ItemEvent e) {

c.set(Calendar.YEAR,Integer.parseInt(year.getSelectedItem().toString()));

c.set(Calendar.MONTH,Integer.parseInt(month.getSelectedItem().toString())-1);

this.setCalendar();

}

//设置显示面板的信息

public void setCalendar(){

for (int i = 7; i l.length; i++) {

l[i].setText("");

l[i].setForeground(Color.BLACK);

}

c.set(Calendar.DAY_OF_MONTH, 1);

for (int i = 1; i =this.getDay(c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1); i++) {

l[5+i+c.get(Calendar.DAY_OF_WEEK)].setText(i+"");

if(nian==c.get(Calendar.YEAR)yue==c.get(Calendar.MONTH)+1ri==i){

l[5+i+c.get(Calendar.DAY_OF_WEEK)].setForeground(Color.BLUE);

}

}

}

//判断天数

public int getDay(int y,int m){

if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){

return 31;

}

if(m==4||m==6||m==9||m==11){

return 30;

}

if(y%4==0y%100!=0||y%400==0){

return 29;

}else{

return 28;

}

}

public static void main(String[] args){

new DateCalendar().setTitle("日历显示");

}

}

java swing 日历控件怎么实现 最好是源码

源代码:

//DatePicker.java

package com.kxsoft.component;

import java.awt.*;

import java.awt.event.*;

import java.util.GregorianCalendar;

import java.util.Date;

import java.util.Calendar;

import java.text.DateFormat;

import java.text.FieldPosition;

import javax.swing.*;

import javax.swing.plaf.BorderUIResource;

public final class DatePicker extends JPanel {

private static final long serialVersionUID = 1L;

private static final int startX = 10;

private static final int startY = 60;

private static final Font smallFont = new Font("Dialog", Font.PLAIN, 10);

private static final Font largeFont = new Font("Dialog", Font.PLAIN, 12);

private static final Insets insets = new Insets(2, 2, 2, 2);

private static final Color highlight = new Color(255, 255, 204);

private static final Color white = new Color(255, 255, 255);

private static final Color gray = new Color(204, 204, 204);

private Component selectedDay = null;

private GregorianCalendar selectedDate = null;

private GregorianCalendar originalDate = null;

private boolean hideOnSelect = true;

private final JButton backButton = new JButton();

private final JLabel monthAndYear = new JLabel();

private final JButton forwardButton = new JButton();

private final JLabel[] dayHeadings = new JLabel[]{

new JLabel("日"),

new JLabel("一"),

new JLabel("二"),

new JLabel("三"),

new JLabel("四"),

new JLabel("五"),

new JLabel("六")};

private final JLabel[][] daysInMonth = new JLabel[][]{

{new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel()},

{new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel()},

{new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel()},

{new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel()},

{new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel()},

{new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel(),

new JLabel()}

};

private final JButton todayButton = new JButton();

private final JButton cancelButton = new JButton();

public DatePicker() {

super();

selectedDate = getToday();

init();

}

public DatePicker(final Date initialDate) {

super();

if (null == initialDate)

selectedDate = getToday();

else

(selectedDate = new GregorianCalendar()).setTime(initialDate);

originalDate = new GregorianCalendar(

selectedDate.get(Calendar.YEAR),

selectedDate.get(Calendar.MONTH),

selectedDate.get(Calendar.DATE));

init();

}

public boolean isHideOnSelect() {

return hideOnSelect;

}

public void setHideOnSelect(final boolean hideOnSelect) {

if (this.hideOnSelect != hideOnSelect) {

this.hideOnSelect = hideOnSelect;

initButtons(false);

}

}

public Date getDate() {

if (null != selectedDate)

return selectedDate.getTime();

return null;

}

private void init() {

setLayout(new AbsoluteLayout());

this.setMinimumSize(new Dimension(161, 226));

this.setMaximumSize(getMinimumSize());

this.setPreferredSize(getMinimumSize());

this.setBorder(new BorderUIResource.EtchedBorderUIResource());

backButton.setFont(smallFont);

backButton.setText("");

backButton.setMargin(insets);

backButton.setDefaultCapable(false);

backButton.addActionListener(new ActionListener() {

public void actionPerformed(final ActionEvent evt) {

onBackClicked(evt);

}

});

add(backButton, new AbsoluteConstraints(10, 10, 20, 20));

monthAndYear.setFont(largeFont);

monthAndYear.setHorizontalAlignment(JTextField.CENTER);

monthAndYear.setText(formatDateText(selectedDate.getTime()));

add(monthAndYear, new AbsoluteConstraints(30, 10, 100, 20));

forwardButton.setFont(smallFont);

forwardButton.setText("");

forwardButton.setMargin(insets);

forwardButton.setDefaultCapable(false);

forwardButton.addActionListener(new ActionListener() {

public void actionPerformed(final ActionEvent evt) {

onForwardClicked(evt);

}

});

add(forwardButton, new AbsoluteConstraints(130, 10, 20, 20));

int x = startX;

for (int ii = 0; ii  dayHeadings.length; ii++) {

dayHeadings[ii].setOpaque(true);

dayHeadings[ii].setBackground(Color.LIGHT_GRAY);

dayHeadings[ii].setForeground(Color.WHITE);

dayHeadings[ii].setHorizontalAlignment(JLabel.CENTER);

add(dayHeadings[ii], new AbsoluteConstraints(x, 40, 21, 21));

x += 20;

}

x = startX;

int y = startY;

for (int ii = 0; ii  daysInMonth.length; ii++) {

for (int jj = 0; jj  daysInMonth[ii].length; jj++) {

daysInMonth[ii][jj].setOpaque(true);

daysInMonth[ii][jj].setBackground(white);

daysInMonth[ii][jj].setFont(smallFont);

daysInMonth[ii][jj].setHorizontalAlignment(JLabel.CENTER);

daysInMonth[ii][jj].setText("");

daysInMonth[ii][jj].addMouseListener(new MouseAdapter() {

public void mouseClicked(final MouseEvent evt) {

onDayClicked(evt);

}

});

add(daysInMonth[ii][jj], new AbsoluteConstraints(x, y, 21, 21));

x += 20;

}

x = startX;

y += 20;

}

initButtons(true);

calculateCalendar();

}

private void initButtons(final boolean firstTime) {

if (firstTime) {

final Dimension buttonSize = new Dimension(68, 24);

todayButton.setText("今天");

todayButton.setMargin(insets);

todayButton.setMaximumSize(buttonSize);

todayButton.setMinimumSize(buttonSize);

todayButton.setPreferredSize(buttonSize);

todayButton.setDefaultCapable(true);

todayButton.setSelected(true);

todayButton.addActionListener(new ActionListener() {

public void actionPerformed(final ActionEvent evt) {

onToday(evt);

}

});

cancelButton.setText("取消");

cancelButton.setMargin(insets);

cancelButton.setMaximumSize(buttonSize);

cancelButton.setMinimumSize(buttonSize);

cancelButton.setPreferredSize(buttonSize);

cancelButton.addActionListener(new ActionListener() {

public void actionPerformed(final ActionEvent evt) {

onCancel(evt);

}

});

} else {

this.remove(todayButton);

this.remove(cancelButton);

}


分享名称:java日历界面源代码 javaswing日历窗口
当前地址:http://azwzsj.com/article/dosedsg.html