哪位大侠帮帮忙吧,我都崩溃了
代码已经能运行了,但是在日期的显示上不正确,星期几跟日子对不上,看了很久都不知怎么下手改,哪位能帮忙看一下算法,帮忙改一下,万分感谢,万分感谢import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
public class MyCalendar{
int year , month;
String output;
JFrame myFrame;
JPanel panel1 , panel2 , panel3 , panel4;
JRadioButton B1 , B2 , B3 , B4 , B5 , B6 , B7 , B8 , B9 , B10 , B11 , B12;
JLabel labelYear,label1;
JTextArea jtaCalendar;
JTextField tfYear;
ButtonGroup group;
public MyCalendar(){
myFrame = new JFrame("日历");
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
label1 = new JLabel(" 注:请先输入年份,然后选择月份");
labelYear = new JLabel("输入年份");
jtaCalendar = new JTextArea();
tfYear = new JTextField(4);
group = new ButtonGroup();
B1 = new JRadioButton("一 月");
B2 = new JRadioButton("二 月");
B3 = new JRadioButton("三 月");
B4 = new JRadioButton("四 月");
B5 = new JRadioButton("五 月");
B6 = new JRadioButton("六 月");
B7 = new JRadioButton("七 月");
B8 = new JRadioButton("八 月");
B9 = new JRadioButton("九 月");
B10 = new JRadioButton("十 月");
B11 = new JRadioButton("十一月");
B12 = new JRadioButton("十二月");
group.add(B1);
group.add(B2);
group.add(B3);
group.add(B4);
group.add(B5);
group.add(B6);
group.add(B7);
group.add(B8);
group.add(B9);
group.add(B10);
group.add(B11);
group.add(B12);
B1.addActionListener(new myListener());
B2.addActionListener(new myListener());
B3.addActionListener(new myListener());
B4.addActionListener(new myListener());
B5.addActionListener(new myListener());
B6.addActionListener(new myListener());
B7.addActionListener(new myListener());
B8.addActionListener(new myListener());
B9.addActionListener(new myListener());
B10.addActionListener(new myListener());
B11.addActionListener(new myListener());
B12.addActionListener(new myListener());
tfYear.setEnabled(true);
panel1.setLayout(new FlowLayout());
panel1.add(labelYear);
panel1.add(tfYear);
panel2.setLayout(new GridLayout(2,6,2,2));
panel2.add(B1);
panel2.add(B2);
panel2.add(B3);
panel2.add(B4);
panel2.add(B5);
panel2.add(B6);
panel2.add(B7);
panel2.add(B8);
panel2.add(B9);
panel2.add(B10);
panel2.add(B11);
panel2.add(B12);
panel3.setLayout(new GridLayout(1,2,2,2));
panel3.add(panel1);
panel3.add(panel2);
panel4.add(jtaCalendar);
myFrame.getContentPane().add(label1,BorderLayout.NORTH);
myFrame.getContentPane().add(panel3,BorderLayout.CENTER);
myFrame.getContentPane().add(panel4,BorderLayout.SOUTH);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setResizable(true);
myFrame.setVisible(true);
// printMonthBody();
}
public static void main(String s[]){
new MyCalendar();
}
//监听器类
class myListener implements ActionListener{
public void actionPerformed(ActionEvent e){
int year = Integer.parseInt(tfYear.getText().trim());
try{
if(e.getSource() == B1)
month = 1;
if(e.getSource() == B2)
month = 2;
if(e.getSource() == B3)
month = 3;
if(e.getSource() == B4)
month = 4;
if(e.getSource() == B5)
month = 5;
if(e.getSource() == B6)
month = 6;
if(e.getSource() == B7)
month = 7;
if(e.getSource() == B8)
month = 8;
if(e.getSource() == B9)
month = 9;
if(e.getSource() == B10)
month = 10;
if(e.getSource() == B11)
month = 11;
if(e.getSource() == B12)
month = 12;
printMonthBody();
}
catch(NumberFormatException e1){
System.out.print("Error");
}
}
//获取月份名称
String getMonthName(){
String monthName = null;
switch(month){
case 1: monthName = "一 月"; break;
case 2: monthName = "二 月"; break;
case 3: monthName = "三 月"; break;
case 4: monthName = "四 月"; break;
case 5: monthName = "五 月"; break;
case 6: monthName = "六 月"; break;
case 7: monthName = "七 月"; break;
case 8: monthName = "八 月"; break;
case 9: monthName = "九 月"; break;
case 10: monthName = "十 月"; break;
case 11: monthName = "十 一 月"; break;
case 12: monthName = "十 二 月"; break;
}
return monthName;
}
void printMonthBody(){
int year = Integer.parseInt(tfYear.getText().trim());
output = " " + getMonthName() + " " + year + "年" + "\n";
output += " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" + "\n";
output += " 星期日 星期一 星期二 星期三 星期四 星期五 星期六" + "\n";
int startDay = getStartDay();
int daysInMonth = getDaysInMonth();
for(int i = 0;i < startDay;i++)
output += " ";
for(int j = 1;j <= daysInMonth;j++){
if(j < 10)
output += " " + j;
else
output += " " + j;
if((j + startDay) % 7 == 0)
output += "\n";
}
output += "\n";
jtaCalendar.setText(output);
}
int getStartDay(){
int startDay1800 = 3; //1800年1月1日是星期三
int totalDays = getTotalDays();
return (totalDays + startDay1800)%7;
}
int getTotalDays(){
int total = 0;
int year = Integer.parseInt(tfYear.getText().trim());
for(int i = 1800;i < year;i++){
if(isLeapYear(i))
total = total + 366;
else
total = total + 365;
}
for(int i = 1;i < month;i++)
total = total + getDaysInMonth();
return total;
}
int getDaysInMonth(){
if(month == 1 || month == 3 || month == 5 || month == 7||
month == 8 || month == 10 || month == 12)
return 31;
if(month == 4 || month == 6 || month == 9 || month == 11)
return 30;
if(month == 2)
return isLeapYear(year)? 29 : 28;
return 0;
}
boolean isLeapYear(int temp_year){
int year = Integer.parseInt(tfYear.getText().trim());
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
}
}
----------------解决方案--------------------------------------------------------