首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

出错了 倒计时牌子总是来回闪 不知道咋回事 请高手解决

2012-05-13 
出错了倒计时牌子总是来回闪不知道怎么回事请高手解决import java.awt.BorderLayoutimport java.awt.Choi

出错了 倒计时牌子总是来回闪 不知道怎么回事 请高手解决
import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calandor extends JFrame implements ItemListener {
private int sum=0,sum1=0,sum2=0;

long longTime;
long currentTime;
long distTime;
long day ,hour,minutes,seconds;
Choice chYear = new Choice();
Choice chMonth = new Choice();
Choice chDay = new Choice();
JLabel ly = new JLabel("年");
JLabel lm = new JLabel("月");
JLabel ld = new JLabel("日");
JLabel ls = new JLabel("倒计时");
JTextField tf = new JTextField(20);
JLabel[] titleLabel = new JLabel[7];
JLabel[] dayLabel = new JLabel[42];
JPanel pn = new JPanel();
JPanel pc = new JPanel();
JPanel pb = new JPanel();

public Calandor() {
super("倒计时日历");
setLocation(300, 300);
initCenter();
initNorth();
initSouth();
add(pn, BorderLayout.NORTH);
add(pc, BorderLayout.CENTER);
add(pb, BorderLayout.SOUTH);
chYear.addItemListener(this);
chMonth.addItemListener(this);
chDay.addItemListener(this);
setCalendar();

pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public void setCalendar() {
// TODO Auto-generated method stub

int year = Integer.parseInt(chYear.getSelectedItem());
int month = Integer.parseInt(chMonth.getSelectedItem());
int day = Integer.parseInt(chDay.getSelectedItem());

jiSuan();

/*for(int i=year1;i<year;i++){
if(i%400==0 || i%4==0 && i%100!=0){
sum+=366;
}else{
sum+=365;
}
System.out.println("sum = "+sum);
}
//System.out.println("sum = "+sum);
for(int j=month-1 ; j>0 ; j--){
int days = getMonthDays(year, month);
sum1+=sum1;
}
System.out.println("sum1 = "+sum1);
System.out.println("day2= "+day);
sum2=sum+sum1+day;
System.out.println("sum2 = "+sum2);*/







Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, 1);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int days = getMonthDays(year, month);
for (int i = 0; i < dayLabel.length; i++) {
dayLabel[i].setText("");
}
int index = 1;
for (int i = dayOfWeek - 1; i < dayOfWeek + days - 1; i++) {
dayLabel[i].setText(String.valueOf(index));
index++;
}

}

public int getMonthDays(int year, int month) {
// TODO Auto-generated method stub
int[] m = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2 && (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)) {
return 29;
}
return m[month];
}

public void initSouth() {
// TODO Auto-generated method stub
pb.add(ls);
pb.add(tf);

}

public void initNorth() {
// TODO Auto-generated method stub
Calendar cal = Calendar.getInstance();
int year1 = cal.get(Calendar.YEAR);
int month1 = cal.get(Calendar.MONTH);
int date = cal.get(Calendar.DATE);
System.out.println(date);
for (int i = year1; i < year1 + 10; i++) {
chYear.add(String.valueOf(i));
}
chYear.select(String.valueOf(year1));
for (int i = 1; i < 13; i++) {
chMonth.add(String.valueOf(i));


}
chMonth.select(String.valueOf(month1+1));

int days = getMonthDays(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH));
System.out.println(days);
for(int i = 1;i <= days ;i++ ){
chDay.add(String.valueOf(i));
}

chDay.select(String.valueOf(date));
pn.add(chYear);
pn.add(ly);
pn.add(chMonth);
pn.add(lm);
pn.add(chDay);
pn.add(ld);

}

public void initCenter() {
// TODO Auto-generated method stub
pc.setLayout(new GridLayout(0, 7));
String[] ss = { "日", "一", "二", "三", "四", "五", "六" };
for (int i = 0; i < ss.length; i++) {
titleLabel[i] = new JLabel(ss[i], JLabel.CENTER);
titleLabel[i].setBackground(Color.DARK_GRAY);
pc.add(titleLabel[i]);
}

for (int i = 0; i < dayLabel.length; i++) {
dayLabel[i] = new JLabel(String.valueOf(i + 1), JLabel.CENTER);
pc.add(dayLabel[i]);
}

}

public void jiSuan() {
Calendar cal = Calendar.getInstance();
final int year2 = Integer.parseInt(chYear.getSelectedItem());
final int month2 = Integer.parseInt(chMonth.getSelectedItem());
final int day2 = Integer.parseInt(chDay.getSelectedItem());
//int year1 = cal.get(Calendar.YEAR);
//int month1 = cal.get(Calendar.MONTH);
//int date = cal.get(Calendar.DATE);
//System.out.println(year2+"nian"+month2+"yue"+day2+"ri");
//System.out.println(year2+"nian"+month1+"yue"+date+"ri");
/*for(int i=year1;i<year2;i++){
if(i%400==0 || i%4==0 && i%100!=0){
sum+=366;
}else{
sum+=365;
}
}
System.out.println("sum = "+sum);
for(int j=month2-1 ; j>0 ; j--){
int days = getMonthDays(year2, month2);
sum1+=sum1;
}
System.out.println("sum1 = "+sum1);
System.out.println("day2= "+day2);
sum2=sum+sum1+day2;
System.out.println("sum2 = "+sum2);*/


Timer timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run(){
Calendar cal =Calendar.getInstance();
cal.set(year2, month2-1,day2,0,0,0);
longTime =cal.getTimeInMillis();
currentTime=new Date().getTime();
distTime =longTime - currentTime;
day=((distTime/1000)/(3600*24));
hour=((distTime/1000)-day*86400)/3600;
minutes=((distTime/1000)-day*86400-hour*3600)/60;
seconds=(distTime/1000)-day*86400-hour*3600-minutes*60;
tf.setText("倒计时"+day+"天"+hour+"小时"+minutes+"分钟"+seconds+"秒");
}

},0,1000);

}

public static void main(String[] args) {
// TODO Auto-generated method stub
new Calandor();


}

@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
setCalendar();
}

}


[解决办法]
运行起来没问题。。。
[解决办法]
貌似先后选不同的时间的话。则之前的也在进行倒计时,没有删除掉或被覆盖掉。
也就是多个时间点在同时到计时,会闪是在轮流显示不同时间点的剩余时间。

[解决办法]
启动了多个timer

Java code
public class Calandor extends JFrame implements ItemListener {    Timer timer;//放这里    public Calandor() {        setCalendar();        pack();        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        setVisible(true);    }    public void setCalendar() {        ....    }    public int getMonthDays(int year, int month) {        ...    }    public void initSouth() {            }    public void initNorth() {        ...    }    public void initCenter() {....    }    public void jiSuan() {        if(timer !=null){//如果timer不等于空,cancel掉            timer.cancel();        }        timer = new Timer();        timer.schedule(new TimerTask(){            @Override            public void run(){                Calendar cal = Calendar.getInstance();                cal.set(year2, month2 - 1, day2, 0, 0, 0);                longTime = cal.getTimeInMillis();                currentTime = new Date().getTime();                distTime = longTime - currentTime;                day = ((distTime / 1000) / (3600 * 24));                hour = ((distTime / 1000) - day * 86400) / 3600;                minutes = ((distTime / 1000) - day * 86400 - hour * 3600) / 60;                seconds = (distTime / 1000) - day * 86400 - hour * 3600                        - minutes * 60;                tf.setText("倒计时" + day + "天" + hour + "小时" + minutes + "分钟"                        + seconds + "秒");            }        }, 0, 1000);    }    public static void main(String[] args) {                new Calandor();    }    public void itemStateChanged(ItemEvent e) {        setCalendar();            }} 

热点排行