线程问题
import java.awt.*;import java.text.*;
import java.util.*;
import java.lang.*;
import java.awt.event.*;
import javax.swing.*;
class pEast implements Runnable
{ int x,month,z;
Date date=new Date();
JLabel l3;
pEast()
{ JFrame jf=new JFrame("时间");
Container con=jf.getContentPane();
con.setLayout(new FlowLayout());
jf.setSize(560,330);
jf.setVisible(true);
JPanel p=new JPanel();
con.add(p);
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
x=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH)+1;
z=calendar.get(Calendar.DAY_OF_MONTH);
Icon image=new ImageIcon("gif469.gif");
JLabel l3=new JLabel();
calendar.setTime(new Date());
SimpleDateFormat df=new SimpleDateFormat("hh:mm:ss");
l3.setText(String.valueOf(String.valueOf(x)+"."+String.valueOf(month)+"."+String.valueOf(z)+" "+df.format(date)));
l3.setIcon(image);
l3.setVerticalTextPosition(JLabel.TOP);
l3.setHorizontalTextPosition(JLabel.CENTER);
p.add(l3);
new Thread(this).start();
}
public void run(){
while(true) {
try{
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
SimpleDateFormat df=new SimpleDateFormat("hh:mm:ss");
l3.setText(String.valueOf(String.valueOf(x)+"."+String.valueOf(month)+"."+String.valueOf(z)+" "+df.format(date)));
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
}
}
public class threah
{
public static void main(String[] args){
new pEast();
}
}
在l3.setText(String.valueOf(String.valueOf(x)+"."+String.valueOf(month)+"."+String.valueOf(z)+" "+df.format(date)));抛空指针异常,怎么回事?
----------------解决方案--------------------------------------------------------
JLabel l3=new JLabel();替换为 l3=new JLabel();看看
----------------解决方案--------------------------------------------------------
谢谢
----------------解决方案--------------------------------------------------------