2个回答
展开全部
给你一个现成的,我自己写的。
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class WanNianLi extends JFrame implements ActionListener {
private static int year,month,days;
private JButton[] btn=new JButton[days];
WanNianLi() {
super("万年历");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout bl=new GridLayout(5,7);
JPanel pane=new JPanel();
pane.setLayout(bl);
for (int i=0;i<days;i++) {
int temp=i+1;
btn[i]=new JButton(""+temp);
btn[i].addActionListener(this);
pane.add(btn[i]);
}
setContentPane(pane);
pack();
setLookAndFeel();
setVisible(true);
}
public static void main(String[] args) {
if (args.length>0)
year=Integer.parseInt(args[0]);
else
year=1982;
if (args.length>1)
month=Integer.parseInt(args[1]);
else
month=1;
GetDays gd=new GetDays(year,month);
days=gd.getDays();
new WanNianLi();
}
public void actionPerformed(ActionEvent evt) {
Object src=evt.getSource();
for (int i=0;i<days;i++)
if (src==btn[i]) {
int day=i+1;
GetWeekday gw=new GetWeekday(year,month,day);
String str="";
switch (gw.getWeekday()) {
case 1:
str="天";
break;
case 2:
str="一";
break;
case 3:
str="二";
break;
case 4:
str="三";
break;
case 5:
str="四";
break;
case 6:
str="五";
break;
case 7:
str="六";
break;
}
setTitle(year+"年"+month+"月"+day+"日"+"星期"+str);
repaint();
}
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){
System.out.print(e.toString());
}
}
}
//////////////
//获取星期几//
//////////////
class GetWeekday {
private Calendar cal=Calendar.getInstance();
private static int weekday;
public int getWeekday() {
return weekday;
}
GetWeekday(int y,int m,int d) {
cal.clear();
cal.set(Calendar.YEAR,y);
cal.set(Calendar.MONTH,m-1);
cal.set(Calendar.DAY_OF_MONTH,d);
weekday=cal.get(Calendar.DAY_OF_WEEK);
}
}
////////////////////
//获取当前月的天数//
////////////////////
class GetDays {
private static int days;
public int getDays() {
return days;
}
GetDays(int y,int m) {
GregorianCalendar gc=new GregorianCalendar();
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 2:
if (gc.isLeapYear(y))
days=29;
else
days=28;
break;
}
}
}
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class WanNianLi extends JFrame implements ActionListener {
private static int year,month,days;
private JButton[] btn=new JButton[days];
WanNianLi() {
super("万年历");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout bl=new GridLayout(5,7);
JPanel pane=new JPanel();
pane.setLayout(bl);
for (int i=0;i<days;i++) {
int temp=i+1;
btn[i]=new JButton(""+temp);
btn[i].addActionListener(this);
pane.add(btn[i]);
}
setContentPane(pane);
pack();
setLookAndFeel();
setVisible(true);
}
public static void main(String[] args) {
if (args.length>0)
year=Integer.parseInt(args[0]);
else
year=1982;
if (args.length>1)
month=Integer.parseInt(args[1]);
else
month=1;
GetDays gd=new GetDays(year,month);
days=gd.getDays();
new WanNianLi();
}
public void actionPerformed(ActionEvent evt) {
Object src=evt.getSource();
for (int i=0;i<days;i++)
if (src==btn[i]) {
int day=i+1;
GetWeekday gw=new GetWeekday(year,month,day);
String str="";
switch (gw.getWeekday()) {
case 1:
str="天";
break;
case 2:
str="一";
break;
case 3:
str="二";
break;
case 4:
str="三";
break;
case 5:
str="四";
break;
case 6:
str="五";
break;
case 7:
str="六";
break;
}
setTitle(year+"年"+month+"月"+day+"日"+"星期"+str);
repaint();
}
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){
System.out.print(e.toString());
}
}
}
//////////////
//获取星期几//
//////////////
class GetWeekday {
private Calendar cal=Calendar.getInstance();
private static int weekday;
public int getWeekday() {
return weekday;
}
GetWeekday(int y,int m,int d) {
cal.clear();
cal.set(Calendar.YEAR,y);
cal.set(Calendar.MONTH,m-1);
cal.set(Calendar.DAY_OF_MONTH,d);
weekday=cal.get(Calendar.DAY_OF_WEEK);
}
}
////////////////////
//获取当前月的天数//
////////////////////
class GetDays {
private static int days;
public int getDays() {
return days;
}
GetDays(int y,int m) {
GregorianCalendar gc=new GregorianCalendar();
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 2:
if (gc.isLeapYear(y))
days=29;
else
days=28;
break;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询