最后的积分都送出去了~~
在SWING中用JMenuItem唤出JLabel,并在里写日期。但是不成功。请教~~()我急需。谢谢个位了package com.frm;import java.awt.*;import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.text.SimpleDateFormat;
import userInfo.show;
import java.sql.Time;
import java.util.Calendar;
import java.util.GregorianCalendar;/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class riqi extends JFrame {
    JMenuBar jMenuBar1 = new JMenuBar();
    JMenu jMenu1 = new JMenu();
    JMenuItem jMenuItem1 = new JMenuItem();
    JLabel jLabel1 = new JLabel();
    JButton button = new JButton();
    int count = 2;
    Date date = new Date();    Calendar calendar = new GregorianCalendar();
    public riqi() {
        {
//            new Thread(new Runnable() {
//                public void run() {
//                    while (true) {
//                        try {
//                            Thread.sleep(1000);
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                        repaint();
//                    }
//                }
//            }
//            ).start();
        }
                try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        
    }    public String run() {
        while (true) {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String str = sdf.format(date);
            try {
                Thread.sleep(1000);
            } catch (Exception e) {            }
            return str;
        }
    }
    public static void main(String[] args) {
      riqi oi=  new riqi();
      oi.run();
    }//    public void run() {
//        while (true) {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
//            jLabel1.setText(sdf.format(new Date()));
//            try {
//                Thread.sleep(1000);
//            } catch (Exception e) {
//                jLabel1.setText("出错错误,请重启程序");
//            }
//        }
//    }
    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        this.setJMenuBar(jMenuBar1);
        jMenu1.setText("sdf");
        jMenuItem1.setText("显示日期");
        jLabel1.setBounds(new Rectangle(204, 0, 196, 61));
        button.setBounds(new Rectangle(2, 0, 105, 31));
        button.setText("显示日期");
        jMenuBar1.add(jMenu1);
        jMenu1.add(jMenuItem1);            jMenuItem1.addActionListener(new ActionListener() {
            private Object calendar;
            public void actionPerformed(ActionEvent e) {
/*
              怎么调用自己写的方法
              
                 */
                if (count % 2 == 0) {
                    String str="";
                    while (true) {
                        SimpleDateFormat sdf = new SimpleDateFormat(
                                "yyyy-MM-dd HH:mm:ss");
                        str = sdf.format(new Date());      
                        
                        try {
                            Thread.sleep(1000);
                            jLabel1.setText(str);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }  
                    }
                    
//                    jLabel1.setText(run().toString());
//
//                        while(true)
//                        {
//                            SimpleDateFormat sdf = new SimpleDateFormat(
//                                    "yyyy-MM-dd HH:mm:ss");
//                            try {
//                                Thread.sleep(1000);            
//                                String str = sdf.format(new Date());
//                            jLabel1.setText(str);
//                            }
//                            catch (InterruptedException ex) {
//                                 ex.printStackTrace();
//                            }//                    jLabel1.setText("显示日期");
                      }else {
                        jLabel1.setText("");
                    }
                    count++;
                }
            });
        this.getContentPane().add(jLabel1, null);
        this.getContentPane().add(button);
        this.setSize(800,600);
        this.setVisible(true);
    
    }   }

解决方案 »

  1.   

    你是要开启线程不断刷新时间?
    还是点次刷新一次点次刷新:
    jMenuItem1.addActionListener(new   ActionListener()   { 
                            private   Object   calendar; 
                            public   void   actionPerformed(ActionEvent   e){ 
                                    setTimeLable();
                            }
    }private void setTimeLable(){
          Date   date   =   new   Date(); 
          SimpleDateFormat   sdf   =   new   SimpleDateFormat("yyyy-MM-dd   HH:mm:ss"); 
          String   str   =   sdf.format(date); 
          jLabel1.setText(str); 
    }
    线程刷新
    private mytime mt=null;
    jMenuItem1.addActionListener(new   ActionListener()   { 
                 public   void   actionPerformed(ActionEvent  e){
                        if(null==mt){
                             mt=new mytime();
                             mt.start();
                        }
                 }
    }
    class mytime extends Threds{
         public   void   run()   { 
                   while   (true)   { 
                       try   { 
                                     setTimeLable(); //这个是上面定义的一样的方法
                                     this.sleep(1000); 
                       }   catch   (Exception   e)   { 
                             jLabel1.setText("出错错误,请重启");                                  
                             mt=null;
                             return ;
                       } 
                    } 
             } 
    }没执行 大致是这样了
      

  2.   

    回复 KK3K2005 首先谢谢你的回复。 我这程序的目的很简单。就是简单的点一下jMenuItem1 然后显示出日期(有星期几更好),
    再点一下jMenuItem1  就隐藏日期/。我日期放在JLabel里了。
    请您再为我解答一下好吗?
    您的
    mytime 我不明白什么意思。API里也没找到`