下面是设计一个日历查看器,可结果总不能达到预期效果,运行后点击查询无反映,请提些宝贵意见!
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.event.*;
import java.applet.Applet;
class FrameInOut extends Frame implements ActionListener{
Label s1,s2,ouptut;
TextField input1,input2;
Button OK;
int year,month;
FrameInOut(){
super("日历查看器");
s1=new Label("输入查询的年份");
input1=new TextField(6);
s2=new Label("输入查询的月份");
input2=new TextField(6);
//output=new TextField(100);
OK=new Button("查询");
setLayout(new FlowLayout());
add(s1);
add(s2);
add(input1);
add(input2);
add(OK);
add(output);input1.addActionListener(this);
input2.addActionListener(this);
OK.addActionListener(this);
setSize(300,200);
show();
}public void actionPerformed(ActionEvent e){
String label=e.getActionCommand();
int year=Integer.parseInt(input1.getText());
int month=Integer.parseInt(input2.getText());
Date date=new Date();
String st=date.toString();
if(e.getSource()==OK)
output=new Label(st);
else{
dispose();
System.exit(0);
}
}
} class Date extends FrameInOut
{
int year=super.year;
int month=super.month;
void Date()
{
int count =0;
int DaysMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
int i,k;
int firstday;
System.out.println("SUM"+"\t"+"MON "+"\t"+"TUE"+"\t"+"WED"+"\t"+"THU"+"\t"+"FRI"+"\t"+"SAT"+"\n");
firstday=CaluJanFistday(year);
for(i=0;i<month-1;i++)
{firstday+=DaysMonth[i];}
if((month>2)&&IsLeapyear(year))
{firstday++;}
k=(firstday%7);
for (i=0;i<(DaysMonth[month-1]+k);i++ )
{ if(IsLeapyear(year))
DaysMonth[1]=29;
if(i<k)
{System.out.print(""+"\t");}
else{System.out.print(i-k+1+"\t");}
count++;
if(count%7==0)
{ System.out.println("\n");}
}
}
//判断是否是闰年
boolean IsLeapyear(int year)
{
if((year%100)==0)
return((year%400)==0);
return((year%4)==0);
}
//计算1月1号是星期几
int CaluJanFistday(int year)
{
return ((6+(year-2000)+CaluLeapYear(year))%7);
}
//计算闰年数
int CaluLeapYear(int year)
{
int yearnum;
int leapyears=0;
for(yearnum=2000;yearnum<year;yearnum++)
{ if(IsLeapyear(yearnum))
{leapyears++;}
}
return (leapyears);
}
}
//输出当月日历/*void getdate()
{
System.out.println("SUM"+"\t"+"MON "+"\t"+"TUE"+"\t"+"WED"+"\t"+"THU"+"\t"+"FRI"+"\t"+"SAT"+"\n");
}
}*/public class Rli1 extends Applet
{
FrameInOut f=new FrameInOut();
f.addWindowListener(new WindowHandler());
}
class WindowHandler implements WindowListener{
public void windowClosing(WindowEvent e){
System.out.println("System exit");
System.exit(1);
}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.event.*;
import java.applet.Applet;
class FrameInOut extends Frame implements ActionListener{
Label s1,s2,ouptut;
TextField input1,input2;
Button OK;
int year,month;
FrameInOut(){
super("日历查看器");
s1=new Label("输入查询的年份");
input1=new TextField(6);
s2=new Label("输入查询的月份");
input2=new TextField(6);
//output=new TextField(100);
OK=new Button("查询");
setLayout(new FlowLayout());
add(s1);
add(s2);
add(input1);
add(input2);
add(OK);
add(output);input1.addActionListener(this);
input2.addActionListener(this);
OK.addActionListener(this);
setSize(300,200);
show();
}public void actionPerformed(ActionEvent e){
String label=e.getActionCommand();
int year=Integer.parseInt(input1.getText());
int month=Integer.parseInt(input2.getText());
Date date=new Date();
String st=date.toString();
if(e.getSource()==OK)
output=new Label(st);
else{
dispose();
System.exit(0);
}
}
} class Date extends FrameInOut
{
int year=super.year;
int month=super.month;
void Date()
{
int count =0;
int DaysMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
int i,k;
int firstday;
System.out.println("SUM"+"\t"+"MON "+"\t"+"TUE"+"\t"+"WED"+"\t"+"THU"+"\t"+"FRI"+"\t"+"SAT"+"\n");
firstday=CaluJanFistday(year);
for(i=0;i<month-1;i++)
{firstday+=DaysMonth[i];}
if((month>2)&&IsLeapyear(year))
{firstday++;}
k=(firstday%7);
for (i=0;i<(DaysMonth[month-1]+k);i++ )
{ if(IsLeapyear(year))
DaysMonth[1]=29;
if(i<k)
{System.out.print(""+"\t");}
else{System.out.print(i-k+1+"\t");}
count++;
if(count%7==0)
{ System.out.println("\n");}
}
}
//判断是否是闰年
boolean IsLeapyear(int year)
{
if((year%100)==0)
return((year%400)==0);
return((year%4)==0);
}
//计算1月1号是星期几
int CaluJanFistday(int year)
{
return ((6+(year-2000)+CaluLeapYear(year))%7);
}
//计算闰年数
int CaluLeapYear(int year)
{
int yearnum;
int leapyears=0;
for(yearnum=2000;yearnum<year;yearnum++)
{ if(IsLeapyear(yearnum))
{leapyears++;}
}
return (leapyears);
}
}
//输出当月日历/*void getdate()
{
System.out.println("SUM"+"\t"+"MON "+"\t"+"TUE"+"\t"+"WED"+"\t"+"THU"+"\t"+"FRI"+"\t"+"SAT"+"\n");
}
}*/public class Rli1 extends Applet
{
FrameInOut f=new FrameInOut();
f.addWindowListener(new WindowHandler());
}
class WindowHandler implements WindowListener{
public void windowClosing(WindowEvent e){
System.out.println("System exit");
System.exit(1);
}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
import java.awt.event.*;
import javax.swing.*;
import java.util.*; public class CalenderTrain extends JFrame implements ActionListener {
JComboBox Month = new JComboBox(); //月份下拉列表框
JComboBox Year = new JComboBox(); //年份下拉列表框
JLabel Year_l = new JLabel("Year::"); //定义标签
JLabel Month_l = new JLabel("Month::"); //定义标签
Date now_date = new Date(); //获取今天的日期
JButton[] button_day = new JButton[49]; //定义一个数组用来存放日期
JButton button_ok = new JButton("确定"); //现实选择日期
JButton button_today = new JButton("今天"); //显示今天按钮
int now_year = now_date.getYear() + 1900; //获取年份值
int now_month = now_date.getMonth(); //获取月份值(当前月份-1)
String year_int = null; //存放年份
int month_int; //存放月份
JPanel pane_ym = new JPanel(); //放置下拉列表框和控制按钮面板
JPanel pane_day = new JPanel(); //放置日期面板
JPanel pane_parent = new JPanel(); //放置以上两个面板
//定义方法绘制面板
public CalenderTrain() {
super("Calender!"); //设定面板得title
//---以下几行使得关闭面板时退出程序
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClose(WindowEvent e) {
System.exit(0);
}
});
//---
setResizable(false); //面板的大小不能变化
//设定年月
/*年份的区间是当前年份的过去10年到当前年份的未来20年
* 月份正常1??12月
*/
for (int i = now_year - 10; i <= now_year + 20; i++) {
Year.addItem(i + "");
}
for (int i = 1; i < 13; i++) {
Month.addItem(i + "");
}
Year.setSelectedIndex(10);//设定年份下拉列表为当前年份
pane_ym.add(Year_l);//添加年份标签
pane_ym.add(Year);//添加年份下拉列表框
Month.setSelectedIndex(now_month);//设定月份下拉列表为当前月份
pane_ym.add(Month_l);//添加月份标签
pane_ym.add(Month);//添加月份下拉列表框
pane_ym.add(button_ok);//添加确定按钮
pane_ym.add(button_today);//添加“今天”按钮
button_ok.addActionListener(this);//确定按钮添加监听事件
button_today.addActionListener(this);//“今天”按钮添加监听事件
//年月设定结束
//初始化日期按钮并绘制
pane_day.setLayout(new GridLayout(7, 7, 10, 10));
for (int i = 0; i < 49; i++) {
button_day[i] = new JButton(" ");
pane_day.add(button_day[i]);
}
this.setDay();//调用setDay()方法
pane_parent.setLayout(new BorderLayout());//设定布局管理器
setContentPane(pane_day);
setContentPane(pane_ym);
pane_parent.add(pane_day, BorderLayout.SOUTH);
pane_parent.add(pane_ym, BorderLayout.NORTH);
setContentPane(pane_parent);
pack();
show();
}
void setDay() {
year_int = Year.getSelectedItem().toString();
month_int = Month.getSelectedIndex();
int year_sel = Integer.parseInt(year_int) - 1900;//获得年份值
Date dt = new Date(year_sel, month_int, 1);//构造一个日期
GregorianCalendar cal = new GregorianCalendar();//创建一个Calendar实例
cal.setTime(dt);
String week[] = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };
int day = 0;//day中存放某个月份的天数
int day_week = 0;//用来存放某个月的第一天是星期几的数值
//--将星期添加到前7个按钮中
for (int i = 0; i < 7; i++) {
button_day[i].setText(week[i]);
}
//--
/*判断是几月份,根据它来设定day的值
* 其中二月份要判断是否是闰年
*/
if (month_int == 0
|| month_int == 2
|| month_int == 4
|| month_int == 6
|| month_int == 7
|| month_int == 9
|| month_int == 11) {
day = 31;
} else if (
month_int == 3
|| month_int == 5
|| month_int == 8
|| month_int == 10) {
day = 30;
} else {
if (cal.isLeapYear(year_sel)) {
day = 29;
} else {
day = 28;
}
}
day_week = 7 + dt.getDay();
int count = 1;
/*绘制按钮
* 在这里我们首先要根据选定的月份的第一天是星期几来确定我们绘制按钮的起始位置
* 其中day_week就是我们要绘制的起始位置
* 对于那些没有数值可以显示的按钮要置空
*/
for (int i = day_week; i < day_week + day; count++, i++) {
if (i % 7 == 0
|| i == 13
|| i == 20
|| i == 27
|| i == 48
|| i == 34
|| i == 41) {
button_day[i].setForeground(Color.RED);
button_day[i].setText(count + "");
} else {
button_day[i].setText(count + "");
} }
//--对于没有日期数值显示的按钮进行置空处理
if (day_week == 0) {
for (int i = day; i < 49; i++) {
button_day[i].setText(" ");
}
} else {
//第一天前面的按钮置空
for (int i = 7; i < day_week; i++) {
button_day[i].setText(" ");
}//最后一天后面的按钮置空
for (int i = day_week + day; i < 49; i++) {
button_day[i].setText(" ");
}
} }
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_ok) {
this.setDay();//如果点击确定按钮就调用setDay()重新方法绘制按钮
} else if (e.getSource() == button_today) {
new CalenderTrain();//如果点击今天按钮,得到今天的日期
}
}
public static void main(String[] args) {
CalenderTrain ct = new CalenderTrain();
}
} 试试这个