标题:线程问题
只看楼主
thread
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-2-11
 问题点数:0 回复次数:2 
线程问题
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 
2008-02-11 10:46
koman
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2007-9-12
得分:0 
JLabel l3=new JLabel();替换为 l3=new JLabel();看看
2008-02-11 18:39
thread
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-2-11
得分:0 
谢谢
2008-02-12 15:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-199691-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.761723 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved