主题:[讨论]我的日历程序哪里错了????急!!!!
请问我的程序哪里出错了?为什么按了“<<”后会没有反应,并且当窗口缩小再还原时,日历的数值都清空了?
(因为我下面的程序是测试用的,所以只做了一个功能)
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test extends JFrame
{
private JPanel JP1=new JPanel();
private JPanel JP2=new JPanel();
private JPanel JP3=new JPanel();
private int day,week,month,year;
private JLabel left,center,right;
Listener L;
Calendar C;
test()
{
C=Calendar.getInstance();
month=C.get(Calendar.MONTH);
year=C.get(Calendar.YEAR);
this.setLayout(null);
JP1.setLayout(new GridLayout(1,7));
JP1.add(new JLabel("日 "));
JP1.add(new JLabel("一 "));
JP1.add(new JLabel("二 "));
JP1.add(new JLabel("三 "));
JP1.add(new JLabel("四 "));
JP1.add(new JLabel("五 "));
JP1.add(new JLabel("六"));
this.add(JP1);
JP1.setBounds(5,0,200,25);
JP2.setLayout(new GridLayout(6,7));
JP2.setBackground(Color.white);
GregorianCalendar G1=new GregorianCalendar(year,month,1);
week=G1.get(Calendar.DAY_OF_WEEK);
for(int i=1;i<=week-1;i++)
{
JP2.add(new JLabel(""));
}
for(int i=1;i<=31;i++)
{
JP2.add(new JLabel(String.valueOf(i),JLabel.CENTER));
}
this.add(JP2);
JP2.setBounds(0,25,195,100);
JP3.setLayout(new BorderLayout());
GregorianCalendar G2=new GregorianCalendar();
day=G2.get(Calendar.DAY_OF_MONTH);
left=new JLabel("<<");
right=new JLabel(">>");
center=new JLabel("今天:"+String.valueOf(year)+"年"+String.valueOf(month+1)+"月"+String.valueOf(day)+"日",JLabel.CENTER);
JP3.add(left,BorderLayout.WEST);
JP3.add(center,BorderLayout.CENTER);
JP3.add(right,BorderLayout.EAST);
this.add(JP3);
JP3.setBounds(5,125,185,50);
left.addMouseListener(new Listener());
}
void update()
{
JP2.removeAll();
JP3.removeAll();
GregorianCalendar G3=new GregorianCalendar(year,month,1);
week=G3.get(Calendar.DAY_OF_WEEK);
for(int i=1;i<=week-1;i++)
{
JP2.add(new JLabel(""));
}
for(int i=1;i<=29;i++)
{
JP2.add(new JLabel(String.valueOf(i),JLabel.CENTER));
}
center=new JLabel("今天:"+String.valueOf(year)+"年"+String.valueOf(month)+"月"+String.valueOf(day)+"日");
JP3.add(left,BorderLayout.WEST);
JP3.add(center,BorderLayout.CENTER);
JP3.add(right,BorderLayout.EAST);
add(JP3);
JP3.setBounds(5,125,185,50);
}
class Listener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
month=month-1;
update();
}
}
public static void main(String[] args)
{
test JF=new test();
JF.setSize(200,200);
JF.setLocation(250,100);
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setResizable(false);
JF.setVisible(true);
}
}
(因为我下面的程序是测试用的,所以只做了一个功能)
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test extends JFrame
{
private JPanel JP1=new JPanel();
private JPanel JP2=new JPanel();
private JPanel JP3=new JPanel();
private int day,week,month,year;
private JLabel left,center,right;
Listener L;
Calendar C;
test()
{
C=Calendar.getInstance();
month=C.get(Calendar.MONTH);
year=C.get(Calendar.YEAR);
this.setLayout(null);
JP1.setLayout(new GridLayout(1,7));
JP1.add(new JLabel("日 "));
JP1.add(new JLabel("一 "));
JP1.add(new JLabel("二 "));
JP1.add(new JLabel("三 "));
JP1.add(new JLabel("四 "));
JP1.add(new JLabel("五 "));
JP1.add(new JLabel("六"));
this.add(JP1);
JP1.setBounds(5,0,200,25);
JP2.setLayout(new GridLayout(6,7));
JP2.setBackground(Color.white);
GregorianCalendar G1=new GregorianCalendar(year,month,1);
week=G1.get(Calendar.DAY_OF_WEEK);
for(int i=1;i<=week-1;i++)
{
JP2.add(new JLabel(""));
}
for(int i=1;i<=31;i++)
{
JP2.add(new JLabel(String.valueOf(i),JLabel.CENTER));
}
this.add(JP2);
JP2.setBounds(0,25,195,100);
JP3.setLayout(new BorderLayout());
GregorianCalendar G2=new GregorianCalendar();
day=G2.get(Calendar.DAY_OF_MONTH);
left=new JLabel("<<");
right=new JLabel(">>");
center=new JLabel("今天:"+String.valueOf(year)+"年"+String.valueOf(month+1)+"月"+String.valueOf(day)+"日",JLabel.CENTER);
JP3.add(left,BorderLayout.WEST);
JP3.add(center,BorderLayout.CENTER);
JP3.add(right,BorderLayout.EAST);
this.add(JP3);
JP3.setBounds(5,125,185,50);
left.addMouseListener(new Listener());
}
void update()
{
JP2.removeAll();
JP3.removeAll();
GregorianCalendar G3=new GregorianCalendar(year,month,1);
week=G3.get(Calendar.DAY_OF_WEEK);
for(int i=1;i<=week-1;i++)
{
JP2.add(new JLabel(""));
}
for(int i=1;i<=29;i++)
{
JP2.add(new JLabel(String.valueOf(i),JLabel.CENTER));
}
center=new JLabel("今天:"+String.valueOf(year)+"年"+String.valueOf(month)+"月"+String.valueOf(day)+"日");
JP3.add(left,BorderLayout.WEST);
JP3.add(center,BorderLayout.CENTER);
JP3.add(right,BorderLayout.EAST);
add(JP3);
JP3.setBounds(5,125,185,50);
}
class Listener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
month=month-1;
update();
}
}
public static void main(String[] args)
{
test JF=new test();
JF.setSize(200,200);
JF.setLocation(250,100);
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setResizable(false);
JF.setVisible(true);
}
}