成都网站建设设计

将想法与焦点和您一起共享

java窗体日历代码,java显示日历编程

java如何在窗口中显示日历

MainFrame.java是显示日历程序,Clock.java是日历计算程序(可以不要)。

创新互联公司公司2013年成立,先为镇雄等服务建站,镇雄等地企业,进行企业商务咨询服务。为镇雄企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

编译后运行MainFrame这个类即可。

swing窗口显示万年历,jdk1.4以上环境编译运行。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133    package org.test;import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.Date;import java.util.Calendar;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class MainFrame extends JFrame { private static final long serialVersionUID = -306484324260972141L; JPanel panel = new JPanel(new BorderLayout()); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(new GridLayout(7, 7)); JPanel panel3 = new JPanel(); JLabel[] label = new JLabel[49]; JLabel y_label = new JLabel("年份"); JLabel m_label = new JLabel("月份"); JComboBox com1 = new JComboBox(); JComboBox com2 = new JComboBox(); int re_year, re_month, x_size, y_size; String year_num; Calendar now = Calendar.getInstance(); // 实例化Calendar   MainFrame() {  super("万年历");   setSize(300, 350);  x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());  y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());  setLocation((x_size - 300) / 2, (y_size - 350) / 2);  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  panel1.add(y_label);  panel1.add(com1);  panel1.add(m_label);  panel1.add(com2);  for (int i = 0; i  49; i++) {   label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中    panel2.add(label[i]);  }  panel3.add(new Clock(this));  panel.add(panel1, BorderLayout.NORTH);  panel.add(panel2, BorderLayout.CENTER);  panel.add(panel3, BorderLayout.SOUTH);   panel.setBackground(Color.white);  panel1.setBackground(Color.white);  panel2.setBackground(Color.white);  panel3.setBackground(Color.white);  Init();  com1.addActionListener(new ClockAction());  com2.addActionListener(new ClockAction());  setContentPane(panel);  setVisible(true);  setResizable(false); }   class ClockAction implements ActionListener {  public void actionPerformed(ActionEvent arg0) {   int c_year, c_month, c_week;   c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份    c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11   c_week = use(c_year, c_month); // 调用函数use,得到星期几   Resetday(c_week, c_year, c_month); // 调用函数Resetday   } }   public void Init() {  int year, month_num, first_day_num;  String log[] = { "日", "一", "二", "三", "四", "五", "六" };  for (int i = 0; i  7; i++) {   label[i].setText(log[i]);  }     for (int i = 0; i  49; i = i + 7) {   label[i].setForeground(Color.red); // 将星期日的日期设置为红色  }     for (int i = 6; i  49; i = i + 7) {   label[i].setForeground(Color.green);// 将星期六的日期设置为绿色  }     for (int i = 1; i  10000; i++) {   com1.addItem("" + i);  }     for (int i = 1; i  13; i++) {   com2.addItem("" + i);  }     month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份   year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份    com1.setSelectedIndex(year - 1); // 设置下拉列表显示为当前年  com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月  first_day_num = use(year, month_num);  Resetday(first_day_num, year, month_num); }   public int use(int reyear, int remonth) {  int week_num;  now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天  week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期   return week_num; }    @SuppressWarnings("deprecation") public void Resetday(int week_log, int year_log, int month_log) {  int month_day_score; // 存储月份的天数   int count;  month_day_score = 0;  count = 1;  Date date = new Date(year_log, month_log + 1, 1); // now  Calendar cal = Calendar.getInstance();  cal.setTime(date);  cal.add(Calendar.MONTH, -1); // 前个月  month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天  for (int i = 7; i  49; i++) { // 初始化标签   label[i].setText("");  }  week_log = week_log + 6; // 将星期数加6,使显示正确  month_day_score = month_day_score + week_log;  for (int i = week_log; i  month_day_score; i++, count++) {   label[i].setText(count + "");  } }   public static void main(String[] args) {  JFrame.setDefaultLookAndFeelDecorated(true);  new MainFrame(); }}    

12345678910111213141516171819202122232425262728293031323334353637383940414243    package org.test;import java.awt.Color;import java.util.Calendar;import java.awt.Canvas;import java.awt.Font;import java.awt.Graphics;import java.text.SimpleDateFormat;public class Clock extends Canvas implements Runnable{ private static final long serialVersionUID = 3660124045489727166L; MainFrame mf; Thread t; String time;   public Clock(MainFrame mf){  this.mf=mf;  setSize(280,40);  setBackground(Color.white);  t=new Thread(this);//实例化线程  t.start();   //调用线程    }   public void run(){  while(true){   try{    Thread.sleep(1000);//休眠1秒钟   }catch(InterruptedException e){    System.out.println("异常");   }   this.repaint(100);  } }   public void paint(Graphics g){  Font f=new Font("宋体",Font.BOLD,16);  SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型    Calendar now=Calendar.getInstance();  time=SDF.format(now.getTime());        //得到当前日期和时间      g.setFont(f);  g.setColor(Color.orange);  g.drawString(time,45,25); }}

JAVA日历代码,怎么做?

import java.util.Date;

import java.util.Calendar;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.text.ParseException;

import java.awt.Color;

import java.awt.Font;

import java.awt.Point;

import java.awt.Dimension;

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.Cursor;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JPanel;

import javax.swing.JLabel;

import javax.swing.JSpinner;

import javax.swing.SpinnerNumberModel;

import javax.swing.SwingUtilities;

import javax.swing.SwingConstants;

import javax.swing.event.ChangeListener;

import javax.swing.event.ChangeEvent;

import javax.swing.border.LineBorder;

/**

* @company:NEUSOFT

* @Title:日期选择控件

* @Description:在原有基础上修改了以下内容:

* 1. 将容器由Frame改为了Dialog,以便在基于对话框的程序中也能够使用

* 2. 将最小日期由1980改为了1950,考虑到目前球员的出生日期可能早于1980年

* 3. 将初始显示格式设置为 yyyy年MM月dd日 格式,原有的小时去掉了,不适合于出生日期字段

*/

public class DateChooserJButton extends JButton {

private DateChooser dateChooser = null;

private String preLabel = "";

public DateChooserJButton() {

this(getNowDate());

}

public DateChooserJButton(SimpleDateFormat df, String dateString) {

this();

setText(df, dateString);

}

public DateChooserJButton(Date date) {

this("", date);

}

public DateChooserJButton(String preLabel, Date date) {

if (preLabel != null)

this.preLabel = preLabel;

setDate(date);

setBorder(null);

setCursor(new Cursor(Cursor.HAND_CURSOR));

addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (dateChooser == null)

dateChooser = new DateChooser();

Point p = getLocationOnScreen();

p.y = p.y + 30;

dateChooser.showDateChooser(p);

}

});

}

private static Date getNowDate() {

return Calendar.getInstance().getTime();

}

private static SimpleDateFormat getDefaultDateFormat() {

return new SimpleDateFormat("yyyy年MM月dd日");

}

// 覆盖父类的方法

public void setText(String s) {

Date date;

try {

date = getDefaultDateFormat().parse(s);

} catch (ParseException e) {

date = getNowDate();

}

setDate(date);

}

public void setText(SimpleDateFormat df, String s) {

Date date;

try {

date = df.parse(s);

} catch (ParseException e) {

date = getNowDate();

}

setDate(date);

}

public void setDate(Date date) {

super.setText(preLabel + getDefaultDateFormat().format(date));

}

public Date getDate() {

String dateString = this.getText().substring(preLabel.length());

try {

return getDefaultDateFormat().parse(dateString);

} catch (ParseException e) {

return getNowDate();

}

}

public String getDateString()

{

Date birth =getDate();

DateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");

return formatDate.format(birth).toString();

//return this.getText().substring(preLabel.length());

}

// 覆盖父类的方法使之无效

//public void addActionListener(ActionListener listener) {

//}

private class DateChooser extends JPanel implements ActionListener,

ChangeListener {

int startYear = 1950; // 默认【最小】显示年份

int lastYear = 2050; // 默认【最大】显示年份

int width = 200; // 界面宽度

int height = 200; // 界面高度

Color backGroundColor = Color.gray; // 底色

// 月历表格配色----------------//

Color palletTableColor = Color.white; // 日历表底色

Color todayBackColor = Color.orange; // 今天背景色

Color weekFontColor = Color.blue; // 星期文字色

Color dateFontColor = Color.black; // 日期文字色

Color weekendFontColor = Color.red; // 周末文字色

// 控制条配色------------------//

Color controlLineColor = Color.pink; // 控制条底色

Color controlTextColor = Color.white; // 控制条标签文字色

Color rbFontColor = Color.white; // RoundBox文字色

Color rbBorderColor = Color.red; // RoundBox边框色

Color rbButtonColor = Color.pink; // RoundBox按钮色

Color rbBtFontColor = Color.red; // RoundBox按钮文字色

JDialog dialog;

JSpinner yearSpin;

JSpinner monthSpin;

JSpinner hourSpin;

JButton[][] daysButton = new JButton[6][7];

DateChooser() {

setLayout(new BorderLayout());

setBorder(new LineBorder(backGroundColor, 2));

setBackground(backGroundColor);

JPanel topYearAndMonth = createYearAndMonthPanal();

add(topYearAndMonth, BorderLayout.NORTH);

JPanel centerWeekAndDay = createWeekAndDayPanal();

add(centerWeekAndDay, BorderLayout.CENTER);

}

private JPanel createYearAndMonthPanal() {

Calendar c = getCalendar();

int currentYear = c.get(Calendar.YEAR);

int currentMonth = c.get(Calendar.MONTH) + 1;

int currentHour = c.get(Calendar.HOUR_OF_DAY);

JPanel result = new JPanel();

result.setLayout(new FlowLayout());

result.setBackground(controlLineColor);

yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,

startYear, lastYear, 1));

yearSpin.setPreferredSize(new Dimension(48, 20));

yearSpin.setName("Year");

yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));

yearSpin.addChangeListener(this);

result.add(yearSpin);

JLabel yearLabel = new JLabel("年");

yearLabel.setForeground(controlTextColor);

result.add(yearLabel);

monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,

12, 1));

monthSpin.setPreferredSize(new Dimension(35, 20));

monthSpin.setName("Month");

monthSpin.addChangeListener(this);

result.add(monthSpin);

JLabel monthLabel = new JLabel("月");

monthLabel.setForeground(controlTextColor);

result.add(monthLabel);

hourSpin = new JSpinner(new SpinnerNumberModel(currentHour, 0, 23,

1));

hourSpin.setPreferredSize(new Dimension(35, 20));

hourSpin.setName("Hour");

hourSpin.addChangeListener(this);

result.add(hourSpin);

JLabel hourLabel = new JLabel("时");

hourLabel.setForeground(controlTextColor);

result.add(hourLabel);

return result;

}

private JPanel createWeekAndDayPanal() {

String colname[] = { "日", "一", "二", "三", "四", "五", "六" };

JPanel result = new JPanel();

// 设置固定字体,以免调用环境改变影响界面美观

result.setFont(new Font("宋体", Font.PLAIN, 12));

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

result.setBackground(Color.white);

JLabel cell;

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

cell = new JLabel(colname[i]);

cell.setHorizontalAlignment(JLabel.RIGHT);

if (i == 0 || i == 6)

cell.setForeground(weekendFontColor);

else

cell.setForeground(weekFontColor);

result.add(cell);

}

int actionCommandId = 0;

for (int i = 0; i 6; i++)

for (int j = 0; j 7; j++) {

JButton numberButton = new JButton();

numberButton.setBorder(null);

numberButton.setHorizontalAlignment(SwingConstants.RIGHT);

numberButton.setActionCommand(String

.valueOf(actionCommandId));

numberButton.addActionListener(this);

numberButton.setBackground(palletTableColor);

numberButton.setForeground(dateFontColor);

if (j == 0 || j == 6)

numberButton.setForeground(weekendFontColor);

else

numberButton.setForeground(dateFontColor);

daysButton[i][j] = numberButton;

result.add(numberButton);

actionCommandId++;

}

return result;

}

private JDialog createDialog(JDialog owner) {

JDialog result = new JDialog(owner, "日期时间选择", true);

result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

result.getContentPane().add(this, BorderLayout.CENTER);

result.pack();

result.setSize(width, height);

return result;

}

void showDateChooser(Point position) {

JDialog owner = (JDialog) SwingUtilities

.getWindowAncestor(DateChooserJButton.this);

if (dialog == null || dialog.getOwner() != owner)

dialog = createDialog(owner);

dialog.setLocation(getAppropriateLocation(owner, position));

flushWeekAndDay();

dialog.setVisible(true);

}

Point getAppropriateLocation(JDialog owner, Point position) {

Point result = new Point(position);

Point p = owner.getLocation();

int offsetX = (position.x + width) - (p.x + owner.getWidth());

int offsetY = (position.y + height) - (p.y + owner.getHeight());

if (offsetX 0) {

result.x -= offsetX;

}

if (offsetY 0) {

result.y -= offsetY;

}

return result;

}

private Calendar getCalendar() {

Calendar result = Calendar.getInstance();

result.setTime(getDate());

return result;

}

private int getSelectedYear() {

return ((Integer) yearSpin.getValue()).intValue();

}

private int getSelectedMonth() {

return ((Integer) monthSpin.getValue()).intValue();

}

private int getSelectedHour() {

return ((Integer) hourSpin.getValue()).intValue();

}

private void dayColorUpdate(boolean isOldDay) {

Calendar c = getCalendar();

int day = c.get(Calendar.DAY_OF_MONTH);

c.set(Calendar.DAY_OF_MONTH, 1);

int actionCommandId = day - 2 + c.get(Calendar.DAY_OF_WEEK);

int i = actionCommandId / 7;

int j = actionCommandId % 7;

if (isOldDay)

daysButton[i][j].setForeground(dateFontColor);

else

daysButton[i][j].setForeground(todayBackColor);

}

private void flushWeekAndDay() {

Calendar c = getCalendar();

c.set(Calendar.DAY_OF_MONTH, 1);

int maxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);

int dayNo = 2 - c.get(Calendar.DAY_OF_WEEK);

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

for (int j = 0; j 7; j++) {

String s = "";

if (dayNo = 1 dayNo = maxDayNo)

s = String.valueOf(dayNo);

daysButton[i][j].setText(s);

dayNo++;

}

}

dayColorUpdate(false);

}

public void stateChanged(ChangeEvent e) {

JSpinner source = (JSpinner) e.getSource();

Calendar c = getCalendar();

if (source.getName().equals("Hour")) {

c.set(Calendar.HOUR_OF_DAY, getSelectedHour());

setDate(c.getTime());

return;

}

dayColorUpdate(true);

if (source.getName().equals("Year"))

c.set(Calendar.YEAR, getSelectedYear());

else

// (source.getName().equals("Month"))

c.set(Calendar.MONTH, getSelectedMonth() - 1);

setDate(c.getTime());

flushWeekAndDay();

}

public void actionPerformed(ActionEvent e) {

JButton source = (JButton) e.getSource();

if (source.getText().length() == 0)

return;

dayColorUpdate(true);

source.setForeground(todayBackColor);

int newDay = Integer.parseInt(source.getText());

Calendar c = getCalendar();

c.set(Calendar.DAY_OF_MONTH, newDay);

setDate(c.getTime());

}

}

}

这是一个专门的选日期的类 ,你看看完了调用就行了

用java写的日历记事本代码?

详细代码

//CalendarWindow类:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

public class CalendarWindow extends JFrame implements ActionListener,

MouseListener,FocusListener{

int year,month,day;

CalendarMessage calendarMessage;

CalendarPad calendarPad;

NotePad notePad;

JTextField showYear,showMonth;

JTextField [] showDay;

CalendarImage calendarImage;

Clock clock;

JButton nextYear,previousYear,nextMonth,previousMonth;

JButton saveDailyRecord,deleteDailyRecord,readDailyRecord;

File dir;

Color backColor=Color.gray;

public CalendarWindow(){

dir=new File("./dailyRecord");

dir.mkdir();

showDay=new JTextField[42];

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

showDay[i]=new JTextField();

showDay[i].setBackground(backColor);

showDay[i].setLayout(new GridLayout(3,3));

showDay[i].addMouseListener(this);

showDay[i].addFocusListener(this);

}

calendarMessage=new CalendarMessage();

calendarPad=new CalendarPad();

notePad=new NotePad();

Calendar calendar=Calendar.getInstance();

calendar.setTime(new Date());

year=calendar.get(Calendar.YEAR);

month=calendar.get(Calendar.MONTH)+1;

day=calendar.get(Calendar.DAY_OF_MONTH);

calendarMessage.setYear(year);

calendarMessage.setMonth(month);

calendarMessage.setDay(day);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.setShowDayTextField(showDay);

notePad.setShowMessage(year,month,day);

calendarPad.showMonthCalendar();

doMark(); //给有日志的号码做标记,见后面的doMark()方法

calendarImage=new CalendarImage();

calendarImage.setImageFile(new File("sea.jpg"));

clock=new Clock();

JSplitPane splitV1=

new JSplitPane(JSplitPane.VERTICAL_SPLIT,calendarPad,calendarImage);

JSplitPane splitV2=

new JSplitPane(JSplitPane.VERTICAL_SPLIT,notePad,clock);

JSplitPane splitH=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitV1,splitV2);

add(splitH,BorderLayout.CENTER);

showYear=new JTextField(""+year,6);

showYear.setFont(new Font("TimesRoman",Font.BOLD,12));

showYear.setHorizontalAlignment(JTextField.CENTER);

showMonth=new JTextField(" "+month,4);

showMonth.setFont(new Font("TimesRoman",Font.BOLD,12));

showMonth.setHorizontalAlignment(JTextField.CENTER);

nextYear=new JButton("下年");

previousYear=new JButton("上年");

nextMonth=new JButton("下月");

previousMonth=new JButton("上月");

nextYear.addActionListener(this);

previousYear.addActionListener(this);

nextMonth.addActionListener(this);

previousMonth.addActionListener(this);

showYear.addActionListener(this);

JPanel north=new JPanel();

north.add(previousYear);

north.add(showYear);

north.add(nextYear);

north.add(previousMonth);

north.add(showMonth);

north.add(nextMonth);

add(north,BorderLayout.NORTH);

saveDailyRecord=new JButton("保存日志") ;

deleteDailyRecord=new JButton("删除日志");

readDailyRecord=new JButton("读取日志");

saveDailyRecord.addActionListener(this);

deleteDailyRecord.addActionListener(this);

readDailyRecord.addActionListener(this);

JPanel pSouth=new JPanel();

pSouth.add(saveDailyRecord);

pSouth.add(deleteDailyRecord);

pSouth.add(readDailyRecord);

add(pSouth,BorderLayout.SOUTH);

setVisible(true);//根据参数 b 的值显示或隐藏此 Window

setBounds(60,60,660,480);

validate();//验证此容器及其所有子组件

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置用户在此窗体上发起 "close" 时默认执行的操作

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==nextYear){

year++;

showYear.setText(""+year);

calendarMessage.setYear(year);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==previousYear){

year--;

showYear.setText(""+year);

calendarMessage.setYear(year);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==nextMonth){

month++;

if(month12) month=1;

showMonth.setText(" "+month);

calendarMessage.setMonth(month);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==previousMonth){

month--;

if(month1) month=12;

showMonth.setText(" "+month);

calendarMessage.setMonth(month);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==showYear){

String s=showYear.getText().trim();

char a[]=s.toCharArray();

boolean boo=false;

for(int i=0;ia.length;i++)

if(!(Character.isDigit(a[i])))

boo=true;

if(boo==true) //弹出“警告”消息对话框

JOptionPane.showMessageDialog(this,"您输入了非法年份","警告", JOptionPane.WARNING_MESSAGE);

else if(boo==false)

year=Integer.parseInt(s);

showYear.setText(""+year);

calendarMessage.setYear(year);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==saveDailyRecord){

notePad.save(dir,year,month,day);

doMark();

}

else if(e.getSource()==deleteDailyRecord){

notePad.delete(dir,year,month,day);

doMark();

}

else if(e.getSource()==readDailyRecord)

notePad.read(dir,year,month,day);

}

public void mousePressed(MouseEvent e){

JTextField text=(JTextField)e.getSource();

String str=text.getText().trim();

try{ day=Integer.parseInt(str);

}

catch(NumberFormatException exp){

}

calendarMessage.setDay(day);

notePad.setShowMessage(year,month,day);

}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {}

public void focusGained(FocusEvent e){

Component com=(Component)e.getSource();

com.setBackground(Color.red);

}

public void focusLost(FocusEvent e){

Component com=(Component)e.getSource();

com.setBackground(backColor);

}

public void doMark(){

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

showDay[i].removeAll();

String str=showDay[i].getText().trim();

try{

int n=Integer.parseInt(str);

if(isHaveDailyRecord(n)==true){ //见后面的isHaveDailyRecord()方法

JLabel mess=new JLabel("存");

mess.setFont(new Font("TimesRoman",Font.PLAIN,11));

mess.setForeground(Color.black) ;

showDay[i].add(mess);

} }

catch(Exception exp){}

}

calendarPad.repaint();

calendarPad.validate();

}

public boolean isHaveDailyRecord(int n){

String key=""+year+""+month+""+n;

String [] dayFile=dir.list();

boolean boo=false;

for(int k=0;kdayFile.length;k++){

if(dayFile[k].equals(key+".txt")){

boo=true;

break;

} }

return boo;

}

public static void main(String args[]){

new CalendarWindow();

}

}

//CalendarPad类:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class CalendarPad extends JPanel{

int year,month,day;

CalendarMessage calendarMessage;

JTextField [] showDay;

JLabel title[];

String [] 星期={"SUN/日","MON/一","TUE/二","WED/三","THU/四","FRI/五","SAT/六"};

JPanel north,center;

public CalendarPad(){

setLayout(new BorderLayout());

north=new JPanel();

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

center=new JPanel();

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

add(center,BorderLayout.CENTER);

add(north,BorderLayout.NORTH);

title=new JLabel[7];

for(int j=0;j7;j++){

title[j]=new JLabel();

title[j].setFont(new Font("TimesRoman",Font.BOLD,12));

title[j].setText(星期[j]);

title[j].setHorizontalAlignment(JLabel.CENTER);

title[j].setBorder(BorderFactory.createRaisedBevelBorder());

north.add(title[j]);

}

title[0].setForeground(Color.red);

title[6].setForeground(Color.blue);

}

public void setShowDayTextField(JTextField [] text){

showDay=text;

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

showDay[i].setFont(new Font("TimesRoman",Font.BOLD,15));

showDay[i].setHorizontalAlignment(JTextField.CENTER);

showDay[i].setEditable(false);

center.add(showDay[i]);

}

}

public void setCalendarMessage(CalendarMessage calendarMessage){

this.calendarMessage=calendarMessage;

}

public void showMonthCalendar(){

String [] a=calendarMessage.getMonthCalendar();

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

showDay[i].setText(a[i]);

validate();

}

}


网站栏目:java窗体日历代码,java显示日历编程
网页链接:http://chengdu.cdxwcx.cn/article/dsghcsd.html