日历的刷新显示问题

meisei81 2010-04-26 10:02:00
最近在写一个在面板中显示日历的程序,但是不知道怎么触发显示下一个月或者前一个月的日历。麻烦各位帮帮忙支支招。附上代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class NoteBook extends JFrame {

private int year = 0;
private int month = 0;
private int day = 0;

private int startDays=0;

private JButton jbtAdd = new JButton("添加");
private JButton jbtSee = new JButton("查看");
private JButton jbtDel = new JButton("删除");
private JButton jbtPre = new JButton("Pre");
private JButton jbtNext = new JButton("Next");

private JTextField jtfYear = new JTextField(10);
private JTextField jtfMonth = new JTextField(10);
private JTextField jtfDay = new JTextField(10);


private JLabel jlbYear = new JLabel("年");
private JLabel jlbMonth = new JLabel("月");
private JLabel jlbDay = new JLabel("日");

private JLabel jlbNull = new JLabel(); //设置一个空的标签
private JLabel jlb1 = new JLabel("星期一");
private JLabel jlb2 = new JLabel("星期二");
private JLabel jlb3 = new JLabel("星期三");
private JLabel jlb4 = new JLabel("星期四");
private JLabel jlb5 = new JLabel("星期五");
private JLabel jlb6 = new JLabel("星期六");
private JLabel jlb0 = new JLabel("星期天");

private JLabel[] jlb = new JLabel[42]; //定义一个标签数组用来存放日期

JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();


public NoteBook(){

jtfYear.setHorizontalAlignment(JTextField.RIGHT); //设置JTextField的显示方式
jtfMonth.setHorizontalAlignment(JTextField.RIGHT);
jtfDay.setHorizontalAlignment(JTextField.RIGHT);

setLayout(new BorderLayout());
p4.setLayout(new BorderLayout());
p1.add(jbtAdd);
p1.add(jbtSee);
p1.add(jbtDel);
add(p1,BorderLayout.NORTH);
p2.add(jtfYear);
p2.add(jlbYear);
p2.add(jtfMonth);
p2.add(jlbMonth);
p2.add(jtfDay);
p2.add(jlbDay);
p4.add(p2,BorderLayout.NORTH);
p3.setLayout(new GridLayout(7,7,2,2));
p3.add(jlb0);
p3.add(jlb1);
p3.add(jlb2);
p3.add(jlb3);
p3.add(jlb4);
p3.add(jlb5);
p3.add(jlb6);

getDate(); //获得年月日
jtfYear.setText(""+year); //设置年
jtfMonth.setText(""+month); //设置月
jtfDay.setText(""+day); //设置日

// year=Integer.parseInt(jtfYear.getText().trim());
// month=Integer.parseInt(jtfMonth.getText().trim());
showCalendar();


p4.add(p3,BorderLayout.CENTER);
p5.add(jbtPre);
p5.add(jlbNull);
p5.add(jbtNext);
p4.add(p5,BorderLayout.SOUTH);

add(p4,BorderLayout.CENTER);


setTitle("NoteBook");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
// setSize(500,600);
setVisible(true);

jbtPre.addActionListener(new ActionListener(){ //注册按钮 Pre
public void actionPerformed(ActionEvent e){
month--;
repaint();
}
});
jbtNext.addActionListener(new ActionListener(){ //注册按钮 Next
public void actionPerformed(ActionEvent e){
month++;
repaint();
}
});

}

public void showCalendar(){ //显示日历
startDays = getStartDay(getDays(year,month));
for(int i=0,j=1;i<42;i++){
if(i>=startDays&&(j<=getMonthDay(year,month))){
p3.add(jlb[i]=new JLabel(""+j));
//System.out.println("j is "+j);
j++;
}
else p3.add(jlb[i]= new JLabel());
//System.out.println("i is "+i);
}
}

public void getDate(){ //获得当前日期

java.util.Date current=new java.util.Date();
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd");
String c=sdf.format(current);
// System.out.println(c);

// Calendar calendar = Calendar.getInstance();
// Date date = calendar.getTime();
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
String[] tokens = c.split("-");
// for(String a : tokens){
// System.out.println(a);
// }
for(int i=0,temp=0;i<tokens.length;i++){
temp=Integer.parseInt(tokens[i]);
System.out.println(temp);
}
year=Integer.parseInt(tokens[0]); //获得当月的年月日
month=Integer.parseInt(tokens[1]);
day=Integer.parseInt(tokens[2]);
}

public boolean isLeapYear(int y){ //判断闰年
boolean isLY=false;
if((y%4==0&& y%100!=0)||(y%400==0))
isLY=true;
return isLY;
}

public int getDays(int year,int month){ //获得总共的天数
int totalDays=0;
for(int i =1800;i<year;i++){
if(isLeapYear(i))
totalDays+=366;
else totalDays+=365;
}
totalDays +=getMonthDays(year,month) ;
//System.out.println("总天数为"+totalDays);
return totalDays;
}

public int getMonthDays(int year,int month){ //获得当年月份的总天数
int monthDays=0;
for(int i=1;i<month;i++){
switch(i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:monthDays+=31;break;
case 4:
case 6:
case 9:
case 11:monthDays+=30;break;
case 2:if(isLeapYear(year)) monthDays+=29;else monthDays+=28;break;
default:break;
}
}
//System.out.println("月份的总天数"+monthDays);
return monthDays;
}


public int getStartDay(int allDays){ //获得开始的日子
int start=0;
int start1800=3;
start=(allDays+start1800)%7;
return start;
}

public int getMonthDay(int year,int month){ //获得当月的天数
int monthDay=0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:monthDay=31;break;
case 4:
case 6:
case 9:
case 11:monthDay=30;break;
case 2:if(isLeapYear(year)) monthDay=29;else monthDay=28;break;
default:monthDay+=0;
}
return monthDay;
}

public void paintComponent(Graphics g){
// super.paintComponent(g);
showCalendar();
}

public static void main(String args[]){
new NoteBook();
// System.exit(0);

}

}


麻烦大家给我指点指点
...全文
126 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
hq1305018 2010-04-27
  • 打赏
  • 举报
回复
改好了,你看一下.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class NoteBook extends JFrame {

private int year = 0;

private int month = 0;

private int day = 0;

private int startDays = 0;

private JButton jbtAdd = new JButton("添加");

private JButton jbtSee = new JButton("查看");

private JButton jbtDel = new JButton("删除");

private JButton jbtPre = new JButton("Pre");

private JButton jbtNext = new JButton("Next");

private JTextField jtfYear = new JTextField(10);

private JTextField jtfMonth = new JTextField(10);

private JTextField jtfDay = new JTextField(10);

private JLabel jlbYear = new JLabel("年");

private JLabel jlbMonth = new JLabel("月");

private JLabel jlbDay = new JLabel("日");

private JLabel jlbNull = new JLabel(); // 设置一个空的标签

private JLabel jlb1 = new JLabel("星期一");

private JLabel jlb2 = new JLabel("星期二");

private JLabel jlb3 = new JLabel("星期三");

private JLabel jlb4 = new JLabel("星期四");

private JLabel jlb5 = new JLabel("星期五");

private JLabel jlb6 = new JLabel("星期六");

private JLabel jlb0 = new JLabel("星期天");

private JLabel[] jlb = new JLabel[42]; // 定义一个标签数组用来存放日期

JPanel p1 = new JPanel();

JPanel p2 = new JPanel();

JPanel p3 = new JPanel();

JPanel p4 = new JPanel();

JPanel p5 = new JPanel();

public NoteBook() {

jtfYear.setHorizontalAlignment(JTextField.RIGHT); // 设置JTextField的显示方式
jtfMonth.setHorizontalAlignment(JTextField.RIGHT);
jtfDay.setHorizontalAlignment(JTextField.RIGHT);

setLayout(new BorderLayout());
p4.setLayout(new BorderLayout());
p1.add(jbtAdd);
p1.add(jbtSee);
p1.add(jbtDel);
add(p1, BorderLayout.NORTH);
p2.add(jtfYear);
p2.add(jlbYear);
p2.add(jtfMonth);
p2.add(jlbMonth);
p2.add(jtfDay);
p2.add(jlbDay);
p4.add(p2, BorderLayout.NORTH);
p3.setLayout(new GridLayout(7, 7, 2, 2));
p3.add(jlb0);
p3.add(jlb1);
p3.add(jlb2);
p3.add(jlb3);
p3.add(jlb4);
p3.add(jlb5);
p3.add(jlb6);

getDate(); // 获得年月日
jtfYear.setText("" + year); // 设置年
jtfMonth.setText("" + month); // 设置月
jtfDay.setText("" + day); // 设置日

// year=Integer.parseInt(jtfYear.getText().trim());
// month=Integer.parseInt(jtfMonth.getText().trim());
showCalendar();

p4.add(p3, BorderLayout.CENTER);
p5.add(jbtPre);
p5.add(jlbNull);
p5.add(jbtNext);
p4.add(p5, BorderLayout.SOUTH);

add(p4, BorderLayout.CENTER);

setTitle("NoteBook");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
// setSize(500,600);
setVisible(true);

jbtPre.addActionListener(new ActionListener() { // 注册按钮 Pre
public void actionPerformed(ActionEvent e) {
if (month == 1) {
year--;
month = 12;
} else {
month--;
}

showCalendar();
}
});
jbtNext.addActionListener(new ActionListener() { // 注册按钮 Next
public void actionPerformed(ActionEvent e) {
if (month == 12) {
year++;
month = 1;
} else {
month++;
}
showCalendar();
}
});

}

public void showCalendar() { // 显示日历
startDays = getStartDay(getDays(year, month));
p3.removeAll();
for (int i = 0, j = 1; i < 42; i++) {
if (i >= startDays && (j <= getMonthDay(year, month))) {
p3.add(jlb[i] = new JLabel("" + j));
// System.out.println("j is "+j);
j++;
} else
p3.add(jlb[i] = new JLabel());
// System.out.println("i is "+i);
}
p3.validate();
}

public void getDate() { // 获得当前日期

java.util.Date current = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
String c = sdf.format(current);
// System.out.println(c);

// Calendar calendar = Calendar.getInstance();
// Date date = calendar.getTime();
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
String[] tokens = c.split("-");
// for(String a : tokens){
// System.out.println(a);
// }
for (int i = 0, temp = 0; i < tokens.length; i++) {
temp = Integer.parseInt(tokens[i]);
System.out.println(temp);
}
year = Integer.parseInt(tokens[0]); // 获得当月的年月日
month = Integer.parseInt(tokens[1]);
day = Integer.parseInt(tokens[2]);
}

public boolean isLeapYear(int y) { // 判断闰年
boolean isLY = false;
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
isLY = true;
return isLY;
}

public int getDays(int year, int month) { // 获得总共的天数
int totalDays = 0;
for (int i = 1800; i < year; i++) {
if (isLeapYear(i))
totalDays += 366;
else
totalDays += 365;
}
totalDays += getMonthDays(year, month);
// System.out.println("总天数为"+totalDays);
return totalDays;
}

public int getMonthDays(int year, int month) { // 获得当年月份的总天数
int monthDays = 0;
for (int i = 1; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthDays += 31;
break;
case 4:
case 6:
case 9:
case 11:
monthDays += 30;
break;
case 2:
if (isLeapYear(year))
monthDays += 29;
else
monthDays += 28;
break;
default:
break;
}
}
// System.out.println("月份的总天数"+monthDays);
return monthDays;
}

public int getStartDay(int allDays) { // 获得开始的日子
int start = 0;
int start1800 = 3;
start = (allDays + start1800) % 7;
return start;
}

public int getMonthDay(int year, int month) { // 获得当月的天数
int monthDay = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthDay = 31;
break;
case 4:
case 6:
case 9:
case 11:
monthDay = 30;
break;
case 2:
if (isLeapYear(year))
monthDay = 29;
else
monthDay = 28;
break;
default:
monthDay += 0;
}
return monthDay;
}

public void paintComponent(Graphics g) {
// super.paintComponent(g);
showCalendar();
}

public static void main(String args[]) {
new NoteBook();
// System.exit(0);

}

}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧