当前位置: 代码迷 >> Java相关 >> 线程问题
  详细解决方案

线程问题

热度:195   发布时间:2008-02-11 10:46:21.0
线程问题
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)));抛空指针异常,怎么回事?
搜索更多相关的解决方案: import  线程  java  con  awt  

----------------解决方案--------------------------------------------------------
JLabel l3=new JLabel();替换为 l3=new JLabel();看看
----------------解决方案--------------------------------------------------------
谢谢
----------------解决方案--------------------------------------------------------
  相关解决方案