简单的java日历程序改错!急急!请帮忙哦!谢谢您!
大哥我的这个日历程序可以在JFrame中显示了.但是它还有很多问题.当我用鼠标选择月份时,它就出错了!还有组合框中的年月我要显示的是从1970至2020,月份从1月到12...
大哥我的这个日历程序可以在JFrame中显示了.但是它还有很多问题.当我用鼠标选择月份时,它就出错了!还有组合框中的年月我要显示的是从1970至2020,月份从1月到12月都错了.您如果有时间帮我改一下!谢谢!
import java.util.Calendar;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
public class PreCalendar extends JFrame implements ActionListener{
JLabel LabelMonth,LabelYear;
JComboBox ComboBoxYear,ComboBoxMonth;
JTextArea display;
int year=2008 ,month=10;
int allday;
Calendar cal=Calendar.getInstance();
String []StringYear;
int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
String StringCalendar;
public PreCalendar()
{
this.setLayout(null);
LabelYear=new JLabel("年") ;
LabelYear.setBounds(40, 40, 40, 20);
this.getContentPane().add(LabelYear);
StringYear=new String [50];
for(int i=0;i<50;i++)
StringYear[i]="1970"+i;
ComboBoxYear=new JComboBox(StringYear);
ComboBoxYear.setBounds(60,40,80,20);
this.getContentPane().add(ComboBoxYear);
LabelMonth=new JLabel("月");
LabelMonth.setBounds(170, 40, 40, 20);
this.getContentPane().add(LabelMonth);
String []StringMonth=new String[12];
for(int j=0;j<12;j++)
StringMonth[j]="1"+j;
ComboBoxMonth=new JComboBox(StringMonth);
ComboBoxMonth.setBounds(190, 40, 80, 20);
ComboBoxMonth.addActionListener(this);
ComboBoxYear.addActionListener(this);
display=new JTextArea();
display.setBounds(50, 110, 225, 200);
setDate(year,month);
print();
display.setText(StringCalendar);
this.getContentPane().add(display);
this.getContentPane().add(ComboBoxMonth);
this.setVisible(true);
this.setTitle("我的日历!");
this.setSize(380,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void print()
{
StringCalendar="------------"+cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH)+1)+"月份------------\n";
StringCalendar+="SUN MON TUR WED THU FRI SAT";
int first=cal.get(Calendar.DAY_OF_WEEK);
int i=0;
for(i=1;i<first;i++)
StringCalendar+=" ";
for(i=1;i<=allday;i++)
{
if(i<10)
StringCalendar+=" ";
StringCalendar+=" "+i;
StringCalendar+=" ";
if(first++%7==0)
StringCalendar+="\n ";
}
}
public void setDate(int m,int n)
{
cal.set(Calendar.YEAR, m);
cal.set(Calendar.MONTH,n-1);
cal.set(Calendar.DATE,1);
if((m%4==0 && m%100!=0 || m % 400==0)&& n==2 )
days[1]++;
allday=days[n-1];
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == ComboBoxYear)
{
year=Integer.parseInt(ComboBoxYear.getSelectedItem().toString() );
setDate(year,month);
print();
display.setText(StringCalendar);
}
if (e.getSource() == ComboBoxMonth)
{
month=Integer.parseInt(ComboBoxMonth.toString());
setDate(year,month);
print();
display.setText(StringCalendar);
}
}
public static void main(String[] args) {
PreCalendar cal=new PreCalendar();
}
} 展开
import java.util.Calendar;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
public class PreCalendar extends JFrame implements ActionListener{
JLabel LabelMonth,LabelYear;
JComboBox ComboBoxYear,ComboBoxMonth;
JTextArea display;
int year=2008 ,month=10;
int allday;
Calendar cal=Calendar.getInstance();
String []StringYear;
int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
String StringCalendar;
public PreCalendar()
{
this.setLayout(null);
LabelYear=new JLabel("年") ;
LabelYear.setBounds(40, 40, 40, 20);
this.getContentPane().add(LabelYear);
StringYear=new String [50];
for(int i=0;i<50;i++)
StringYear[i]="1970"+i;
ComboBoxYear=new JComboBox(StringYear);
ComboBoxYear.setBounds(60,40,80,20);
this.getContentPane().add(ComboBoxYear);
LabelMonth=new JLabel("月");
LabelMonth.setBounds(170, 40, 40, 20);
this.getContentPane().add(LabelMonth);
String []StringMonth=new String[12];
for(int j=0;j<12;j++)
StringMonth[j]="1"+j;
ComboBoxMonth=new JComboBox(StringMonth);
ComboBoxMonth.setBounds(190, 40, 80, 20);
ComboBoxMonth.addActionListener(this);
ComboBoxYear.addActionListener(this);
display=new JTextArea();
display.setBounds(50, 110, 225, 200);
setDate(year,month);
print();
display.setText(StringCalendar);
this.getContentPane().add(display);
this.getContentPane().add(ComboBoxMonth);
this.setVisible(true);
this.setTitle("我的日历!");
this.setSize(380,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void print()
{
StringCalendar="------------"+cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH)+1)+"月份------------\n";
StringCalendar+="SUN MON TUR WED THU FRI SAT";
int first=cal.get(Calendar.DAY_OF_WEEK);
int i=0;
for(i=1;i<first;i++)
StringCalendar+=" ";
for(i=1;i<=allday;i++)
{
if(i<10)
StringCalendar+=" ";
StringCalendar+=" "+i;
StringCalendar+=" ";
if(first++%7==0)
StringCalendar+="\n ";
}
}
public void setDate(int m,int n)
{
cal.set(Calendar.YEAR, m);
cal.set(Calendar.MONTH,n-1);
cal.set(Calendar.DATE,1);
if((m%4==0 && m%100!=0 || m % 400==0)&& n==2 )
days[1]++;
allday=days[n-1];
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == ComboBoxYear)
{
year=Integer.parseInt(ComboBoxYear.getSelectedItem().toString() );
setDate(year,month);
print();
display.setText(StringCalendar);
}
if (e.getSource() == ComboBoxMonth)
{
month=Integer.parseInt(ComboBoxMonth.toString());
setDate(year,month);
print();
display.setText(StringCalendar);
}
}
public static void main(String[] args) {
PreCalendar cal=new PreCalendar();
}
} 展开
3个回答
展开全部
更改过的地方都给你标出来了,现在是没什么问题了
建议你再改一下TextArea的StringCalendar的显示,现在是没什么问题了
我测试过了,年份和月份都可以改变啊
import java.util.Calendar;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
public class PreCalendar extends JFrame implements ActionListener
{
JLabel LabelMonth,LabelYear;
JComboBox ComboBoxYear,ComboBoxMonth;
JTextArea display;
int year=2008 ,month=10;
int allday;
Calendar cal=Calendar.getInstance();
String []StringYear;
String []StringMonth;
int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
String StringCalendar;
public PreCalendar()
{
this.setLayout(null);
LabelYear=new JLabel("年") ;
LabelYear.setBounds(40, 40, 40, 20);
this.getContentPane().add(LabelYear);
StringYear=new String [51]; //更改了此处
for(int i=0;i<51;i++) //更改了此处
StringYear[i]=String.valueOf(1970+i); //更改了此处
ComboBoxYear=new JComboBox(StringYear);
ComboBoxYear.setBounds(60,40,80,20);
this.getContentPane().add(ComboBoxYear);
LabelMonth=new JLabel("月");
LabelMonth.setBounds(170, 40, 40, 20);
this.getContentPane().add(LabelMonth);
StringMonth=new String[12]; //更改了此处
for(int j=0;j<12;j++) //更改了此处
StringMonth[j]=String.valueOf(j+1); //更改了此处
ComboBoxMonth=new JComboBox(StringMonth);
ComboBoxMonth.setBounds(190, 40, 80, 20);
ComboBoxMonth.addActionListener(this);
ComboBoxYear.addActionListener(this);
display=new JTextArea();
display.setBounds(50, 110, 225, 200);
setDate(year,month);
print();
display.setText(StringCalendar);
this.getContentPane().add(display);
this.getContentPane().add(ComboBoxMonth);
this.setVisible(true);
this.setTitle("我的日历!");
this.setSize(380,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void print()
{
StringCalendar="------------"+cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH)+1)+"月份------------\n";
StringCalendar+="SUN MON TUR WED THU FRI SAT";
int first=cal.get(Calendar.DAY_OF_WEEK);
int i=0;
for(i=1;i<first;i++)
StringCalendar+=" ";
for(i=1;i<=allday;i++)
{
if(i<10)
StringCalendar+=" ";
StringCalendar+=" "+i;
StringCalendar+=" ";
if(first++%7==0)
StringCalendar+="\n ";
}
}
public void setDate(int m,int n)
{
cal.set(Calendar.YEAR, m);
cal.set(Calendar.MONTH,n-1);
cal.set(Calendar.DATE,1);
if((m%4==0 && m%100!=0 || m % 400==0)&& n==2 )
days[1]++;
allday=days[n-1];
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == ComboBoxYear)
{
year=Integer.parseInt(ComboBoxYear.getSelectedItem().toString() );
month=Integer.parseInt(ComboBoxMonth.getSelectedItem().toString());//添加了此处
setDate(year,month);
print();
display.setText(StringCalendar);
}
if (e.getSource() == ComboBoxMonth)
{
year=Integer.parseInt(ComboBoxYear.getSelectedItem().toString() ); //添加了此处
month=Integer.parseInt(ComboBoxMonth.getSelectedItem().toString()); //更改了此处
setDate(year,month);
print();
display.setText(StringCalendar);
}
}
public static void main(String[] args) {
PreCalendar cal=new PreCalendar();
}
}
建议你再改一下TextArea的StringCalendar的显示,现在是没什么问题了
我测试过了,年份和月份都可以改变啊
import java.util.Calendar;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
public class PreCalendar extends JFrame implements ActionListener
{
JLabel LabelMonth,LabelYear;
JComboBox ComboBoxYear,ComboBoxMonth;
JTextArea display;
int year=2008 ,month=10;
int allday;
Calendar cal=Calendar.getInstance();
String []StringYear;
String []StringMonth;
int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
String StringCalendar;
public PreCalendar()
{
this.setLayout(null);
LabelYear=new JLabel("年") ;
LabelYear.setBounds(40, 40, 40, 20);
this.getContentPane().add(LabelYear);
StringYear=new String [51]; //更改了此处
for(int i=0;i<51;i++) //更改了此处
StringYear[i]=String.valueOf(1970+i); //更改了此处
ComboBoxYear=new JComboBox(StringYear);
ComboBoxYear.setBounds(60,40,80,20);
this.getContentPane().add(ComboBoxYear);
LabelMonth=new JLabel("月");
LabelMonth.setBounds(170, 40, 40, 20);
this.getContentPane().add(LabelMonth);
StringMonth=new String[12]; //更改了此处
for(int j=0;j<12;j++) //更改了此处
StringMonth[j]=String.valueOf(j+1); //更改了此处
ComboBoxMonth=new JComboBox(StringMonth);
ComboBoxMonth.setBounds(190, 40, 80, 20);
ComboBoxMonth.addActionListener(this);
ComboBoxYear.addActionListener(this);
display=new JTextArea();
display.setBounds(50, 110, 225, 200);
setDate(year,month);
print();
display.setText(StringCalendar);
this.getContentPane().add(display);
this.getContentPane().add(ComboBoxMonth);
this.setVisible(true);
this.setTitle("我的日历!");
this.setSize(380,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void print()
{
StringCalendar="------------"+cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH)+1)+"月份------------\n";
StringCalendar+="SUN MON TUR WED THU FRI SAT";
int first=cal.get(Calendar.DAY_OF_WEEK);
int i=0;
for(i=1;i<first;i++)
StringCalendar+=" ";
for(i=1;i<=allday;i++)
{
if(i<10)
StringCalendar+=" ";
StringCalendar+=" "+i;
StringCalendar+=" ";
if(first++%7==0)
StringCalendar+="\n ";
}
}
public void setDate(int m,int n)
{
cal.set(Calendar.YEAR, m);
cal.set(Calendar.MONTH,n-1);
cal.set(Calendar.DATE,1);
if((m%4==0 && m%100!=0 || m % 400==0)&& n==2 )
days[1]++;
allday=days[n-1];
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == ComboBoxYear)
{
year=Integer.parseInt(ComboBoxYear.getSelectedItem().toString() );
month=Integer.parseInt(ComboBoxMonth.getSelectedItem().toString());//添加了此处
setDate(year,month);
print();
display.setText(StringCalendar);
}
if (e.getSource() == ComboBoxMonth)
{
year=Integer.parseInt(ComboBoxYear.getSelectedItem().toString() ); //添加了此处
month=Integer.parseInt(ComboBoxMonth.getSelectedItem().toString()); //更改了此处
setDate(year,month);
print();
display.setText(StringCalendar);
}
}
public static void main(String[] args) {
PreCalendar cal=new PreCalendar();
}
}
展开全部
import java.util.Calendar;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
public class PreCalendar extends JFrame implements ActionListener
{
JLabel LabelMonth,LabelYear;
JComboBox ComboBoxYear,ComboBoxMonth;
JTextArea display;
int year=2008 ,month=10;
int allday;
Calendar cal=Calendar.getInstance();
Integer []StringYear;
int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
String StringCalendar;
public PreCalendar()
{
this.setLayout(null);
LabelYear=new JLabel("年") ;
LabelYear.setBounds(40, 40, 40, 20);
this.getContentPane().add(LabelYear);
StringYear=new Integer [50];
for(int i=0;i<50;i++)
StringYear[i]=1970+i;
ComboBoxYear=new JComboBox(StringYear);
ComboBoxYear.setBounds(60,40,80,20);
this.getContentPane().add(ComboBoxYear);
LabelMonth=new JLabel("月");
LabelMonth.setBounds(170, 40, 40, 20);
this.getContentPane().add(LabelMonth);
Integer []StringMonth=new Integer[12];
for(int j=0;j<12;j++)
StringMonth[j]=1+j;
ComboBoxMonth=new JComboBox(StringMonth);
ComboBoxMonth.setBounds(190, 40, 80, 20);
ComboBoxMonth.addActionListener(this);
ComboBoxYear.addActionListener(this);
display=new JTextArea();
display.setBounds(50, 110, 225, 200);
setDate(year,month);
print();
display.setText(StringCalendar);
this.getContentPane().add(display);
this.getContentPane().add(ComboBoxMonth);
this.setVisible(true);
this.setTitle("我的日历!");
this.setSize(380,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void print()
{
StringCalendar="------------"+cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH)+1)+"月份------------\n";
StringCalendar+="SUN MON TUR WED THU FRI SAT";
int first=cal.get(Calendar.DAY_OF_WEEK);
int i=0;
for(i=1;i<first;i++)
StringCalendar+=" ";
for(i=1;i<=allday;i++)
{
if(i<10)
StringCalendar+=" ";
StringCalendar+=" "+i;
StringCalendar+=" ";
if(first++%7==0)
StringCalendar+="\n ";
}
}
public void setDate(int m,int n)
{
cal.set(Calendar.YEAR, m);
cal.set(Calendar.MONTH,n-1);
cal.set(Calendar.DATE,1);
if((m%4==0 && m%100!=0 || m % 400==0)&& n==2 )
days[1]++;
allday=days[n-1];
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == ComboBoxYear)
{
year=Integer.parseInt(ComboBoxYear.getSelectedItem().toString() );
setDate(year,month);
print();
display.setText(StringCalendar);
}
if (e.getSource() == ComboBoxMonth)
{
month=Integer.parseInt(ComboBoxMonth.toString());
setDate(year,month);
print();
display.setText(StringCalendar);
}
}
public static void main(String[] args) {
new PreCalendar();
}
}
我把年份月份的显示给你更改了,你看看吧。要用整形相加,不能String相加,那样的话,就是直接连接。但是,你的那个日期显示与星期几不能吻合。
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
public class PreCalendar extends JFrame implements ActionListener
{
JLabel LabelMonth,LabelYear;
JComboBox ComboBoxYear,ComboBoxMonth;
JTextArea display;
int year=2008 ,month=10;
int allday;
Calendar cal=Calendar.getInstance();
Integer []StringYear;
int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
String StringCalendar;
public PreCalendar()
{
this.setLayout(null);
LabelYear=new JLabel("年") ;
LabelYear.setBounds(40, 40, 40, 20);
this.getContentPane().add(LabelYear);
StringYear=new Integer [50];
for(int i=0;i<50;i++)
StringYear[i]=1970+i;
ComboBoxYear=new JComboBox(StringYear);
ComboBoxYear.setBounds(60,40,80,20);
this.getContentPane().add(ComboBoxYear);
LabelMonth=new JLabel("月");
LabelMonth.setBounds(170, 40, 40, 20);
this.getContentPane().add(LabelMonth);
Integer []StringMonth=new Integer[12];
for(int j=0;j<12;j++)
StringMonth[j]=1+j;
ComboBoxMonth=new JComboBox(StringMonth);
ComboBoxMonth.setBounds(190, 40, 80, 20);
ComboBoxMonth.addActionListener(this);
ComboBoxYear.addActionListener(this);
display=new JTextArea();
display.setBounds(50, 110, 225, 200);
setDate(year,month);
print();
display.setText(StringCalendar);
this.getContentPane().add(display);
this.getContentPane().add(ComboBoxMonth);
this.setVisible(true);
this.setTitle("我的日历!");
this.setSize(380,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void print()
{
StringCalendar="------------"+cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH)+1)+"月份------------\n";
StringCalendar+="SUN MON TUR WED THU FRI SAT";
int first=cal.get(Calendar.DAY_OF_WEEK);
int i=0;
for(i=1;i<first;i++)
StringCalendar+=" ";
for(i=1;i<=allday;i++)
{
if(i<10)
StringCalendar+=" ";
StringCalendar+=" "+i;
StringCalendar+=" ";
if(first++%7==0)
StringCalendar+="\n ";
}
}
public void setDate(int m,int n)
{
cal.set(Calendar.YEAR, m);
cal.set(Calendar.MONTH,n-1);
cal.set(Calendar.DATE,1);
if((m%4==0 && m%100!=0 || m % 400==0)&& n==2 )
days[1]++;
allday=days[n-1];
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == ComboBoxYear)
{
year=Integer.parseInt(ComboBoxYear.getSelectedItem().toString() );
setDate(year,month);
print();
display.setText(StringCalendar);
}
if (e.getSource() == ComboBoxMonth)
{
month=Integer.parseInt(ComboBoxMonth.toString());
setDate(year,month);
print();
display.setText(StringCalendar);
}
}
public static void main(String[] args) {
new PreCalendar();
}
}
我把年份月份的显示给你更改了,你看看吧。要用整形相加,不能String相加,那样的话,就是直接连接。但是,你的那个日期显示与星期几不能吻合。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
呵呵!!!
我把两个程序都运行了
好像都只能改变年份不能改变月份???????????
我把两个程序都运行了
好像都只能改变年份不能改变月份???????????
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询