我先编个能在文本框显示每隔一秒显示时间可是 我的代码如下:(只显示不更新)import java.awt.*;
import java.util.*;
import java.awt.List;
import java.io.*;
import java.net.*;
import java.awt.event.*;public class MyServer extends Frame implements Runnable
{
    Thread timer=null;
    Frame msf=new Frame("客户端");
    List UserList=new List();
    Label DateLab=new Label();
    Label UserLab=new Label("用户列表:");
    TextArea DataTa=new TextArea();
    
    public MyServer()
    {
        msf.setBackground(Color.LIGHT_GRAY);
        DateLab.setBackground(Color.getHSBColor(360,360,360));
        UserLab.setBackground(Color.getHSBColor(360,360,360));
     msf.setBounds(200,50,900,700);
     DateLab.setBounds(50,50,500,60);
     UserLab.setBounds(600,50,250,60);
     DataTa.setBounds(50,150,500,500);
     UserList.setBounds(600,150,250,500);
     Font LabFont=new Font("Serief",Font.BOLD,30);
     Font DataFont=new Font("Serief",Font.BOLD,22);
        UserList.setFont(DataFont);
        DataTa.setFont(DataFont);
     DateLab.setFont(LabFont);
     UserLab.setFont(LabFont);
     msf.add(DataTa);
     msf.add(DateLab);
     msf.add(UserLab);
     msf.add(UserList);
     msf.setLayout(null);
     msf.setVisible(true);
    
    }
    
    public void NowDate()
    {
        Calendar cal=Calendar.getInstance();
        String today="时间日期:"+cal.get(Calendar.YEAR)+"年"+cal.get(Calendar.MONTH)+"月"+cal.get(Calendar.DATE)
        +"日"+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND);
        DateLab.setText(today);
    }
    
    public void start()
    {
     timer=new Thread(this);
     timer.start();
    
    }
    
    public void stop()
    {
     timer.interrupt();
     timer=null;
    }
    public void run()
    {
     try{Thread.sleep(1000);}
     catch(InterruptedException e){}
     DateLab.setText(null);
     NowDate();
    }

    class WondowsClosing extends WindowAdapter
{
public void closing(WindowEvent e)
{
msf.dispose();
}
}

public static void main(String[]args)
{
MyServer ms=new MyServer();
ms.NowDate();
}}新手如果有什么好的建议也希望能提下

解决方案 »

  1.   


    run方法里面要用循环啊,你不循环怎么获取新的时间并更新啊
      

  2.   

    修改的地方写在程序注释里
    import java.awt.*;
    import java.util.*;
    import java.awt.List;
    import java.io.*;
    import java.net.*;
    import java.awt.event.*;public class MyServer extends Frame implements Runnable
    {
        Thread timer=null;
        Frame msf;//=new Frame("客户端"); //这里MyServer本身继承Frame,所以没必要再用别的Frame
        List UserList=new List();
        Label DateLab=new Label();
        Label UserLab=new Label("用户列表:");
        TextArea DataTa=new TextArea();
        boolean flag = true; //追加一个flag作为控制
        
        public MyServer()
        {
            super("客户端"); //这里可以设置标题
            msf = this; //这里为了不改动下面的代码,所以保持msf,并让它等于对象本身
            msf.setBackground(Color.LIGHT_GRAY);
            DateLab.setBackground(Color.getHSBColor(360,360,360));
            UserLab.setBackground(Color.getHSBColor(360,360,360));    
            msf.setBounds(200,50,900,700);
            DateLab.setBounds(50,50,500,60);
            UserLab.setBounds(600,50,250,60);
            DataTa.setBounds(50,150,500,500);
            UserList.setBounds(600,150,250,500);
            Font LabFont=new Font("Serief",Font.BOLD,30);
            Font DataFont=new Font("Serief",Font.BOLD,22);
            UserList.setFont(DataFont);
            DataTa.setFont(DataFont);
            DateLab.setFont(LabFont);
            UserLab.setFont(LabFont);
            msf.add(DataTa);
            msf.add(DateLab);
            msf.add(UserLab);
            msf.add(UserList);
            msf.setLayout(null);
            msf.setVisible(true);
            msf.addWindowListener(new WindowsClosing()); //添加窗体关闭监听
            
        }
        
        public void NowDate()
        {
            Calendar cal=Calendar.getInstance();
            String today="时间日期:"+cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH)+1)+"月"+cal.get(Calendar.DATE)
            +"日"+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND); //这里要注意Calendar的月份和实际月份相差1
            DateLab.setText(today);
        }
        
        public void start()
        {
            flag = true; //设置flag为true
            timer=new Thread(this);
            timer.start();
            
        }
        
        public void stop()
        {
            timer.interrupt();
            flag = false; //设置flag为false
            timer=null;
        }
        public void run()
        {
            while (flag) { //这里要用个循环才能不断地调用NowDate来刷新画面信息
                try{Thread.sleep(1000);} 
                catch(InterruptedException e){}
                DateLab.setText(null);
                NowDate();
            }
        }
        
        class WindowsClosing extends WindowAdapter //把Wondows改成了Windows
        {
            public void windowClosing(WindowEvent e) //注意接口的方法是windowClosing
            {
                //close();
                //msf.dispose(); //如果要用dispose,要记得把现成停止,否则线成还或者,程序不会终止
                System.exit(0); //这里直接退出就好了
            }
        }
        
        public static void main(String[]args)
        {
            MyServer ms=new MyServer();
            ms.NowDate();
            ms.start(); //这里要启动timer才能让线程启动
        }}
      

  3.   

    private java.util.Timer timer; 
    timer = new Timer(true); 
    timer.schedule(
    new java.util.TimerTask() { public void run() { //server.checkNewMail(); 要操作的方法 } }, 0, 1000); 
      

  4.   

    我想问下第一个注释"//这里MyServer本身继承Frame,所以没必要再用别的Frame”
    在方法里用super();和用 Frame msf=New Frame();有什么本质的区别,有点不太清楚哈,比较笨还有关于线程这个 书上是 用applet的例子的没有看到他用到你这里main()主函数中的ms.start();,applet中不需要的吗?我写程序还是处于模仿阶段,希望我有天也能和你一样
      

  5.   

    还有个问题想问的是我试过了貌似windowsAdapter的接口方法用System.exit(0);不行啊,能用如下的代码吗?class WindowsClosing extends WindowAdapter //把Wondows改成了Windows
        {
            public void windowClosing(WindowEvent e) //注意接口的方法是windowClosing
            {
              msf.dispose(); //销毁窗口
               ms.stop();//线程终止
               //System.exit(0);         }
        }
      

  6.   

    super("title");是调用父类方法,直接给窗体设置标题
    也可以调用setTitle("title");设置窗体标题
    Frame Frame msf=New Frame("title");这个是定一个新的变量msf,让他引用new出来的一个Frame对象,这个new出来的Frame对象和MyServer当前被创建的对象本身this是不同的,这个就是本质的区别
    比如
    class A {
        A a = new A(); //这个a引用的是new出来的A对象
        public A {
            System.out.println(a==this); //和这里的当前被创建的A对象本身不是同一个对象
        }
    }
    打个比方,一个孕妇,她肚子里的小孩是另一个人,不是孕妇本身,这个就是区别。
      

  7.   

    System.exit(0);为何不行,出现了什么问题?
    用这样的方式也可以
    msf.dispose(); //销毁窗口
    ms.stop();//线程终止
    不过要知道,内部类WindowsClosing是看不到main方法的变量ms的,所以要改成
    msf.dispose(); //销毁窗口
    MyServer.this.stop();//注意这里,或者直接写stop,这里的内部类WindowsClosing能知道stop方法的
      

  8.   

    你写的没问题,是我忘了把WindowsAdapter的接口方法名改成windowClosing了,程序这个方法没重写,所以就不行了。至于this 和 New 这两个,我现在的理解是new Frame是重新在内存上找一块地址建立个新的东西,而this指的是这个类的本身或是该构造函数本身或是该方法本身还是不明白,能说下上文的两个this具体的指代不?如果是要用代码替换他们写那应该是怎么写呢? msf = this; //这里为了不改动下面的代码,所以保持msf,并让它等于对象本身
    timer=new Thread(this);import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Dic extends Frame implements ActionListener
    {
    Frame frm=new Frame("汉英互译词典");
    Button cbut=new Button("汉译英");
    Button ebut=new Button("英译汉");
    Button dbut=new Button("确定");
    TextField itf=new TextField(30);
    TextArea ota=new TextArea("",5,20,TextArea.SCROLLBARS_VERTICAL_ONLY); 
    windowclosing wdclosing=new windowclosing();
    Dialog cdiag=new Dialog(frm);
    Label dlab=new Label("未找到所查询的词",Label.CENTER);
    Dic()
    {
    frm.setLayout(null);
    frm.setBounds(400,300,250,300);
    cdiag.setBounds(425,350,200,150);
    cdiag.setLayout(null);
    cdiag.setTitle("请单击按钮");
    cdiag.setBackground(Color.LIGHT_GRAY);
    dbut.setBounds(60,90,80,40);
    dlab.setBounds(30,60,140,20);
    ota.setBackground(Color.lightGray);
    itf.setBounds(35,50,180,20);
    cbut.setBounds(35,90,70,30);
    ebut.setBounds(145,90,70,30);
    ota.setBounds(35,140,180,140);
    cbut.addActionListener(this);
    ebut.addActionListener(this);
    dbut.addActionListener(this);
    frm.addWindowListener(wdclosing);
    cdiag.add(dbut);
    cdiag.add(dlab);
    frm.add(itf);
    frm.add(ota);
    frm.add(cbut);
    frm.add(ebut);
    ota.setEditable(false);
    frm.setVisible(true);
    }
    public static void main(String[]args)
    {
    new Dic();
    }
        class windowclosing extends WindowAdapter
    {
        public void windowClosing(WindowEvent b)
        {
    frm.dispose();
        }
    }    
    public void actionPerformed(ActionEvent e)
    {
    Button but=(Button)e.getSource();
    if(but==cbut)
    {
    String thisLine;
    int i=0;
    String[] str=new String[100];
    try
    {
          FileReader fr=new FileReader("e:\\Java\\Dictionary\\CE.txt");
         BufferedReader br=new BufferedReader(fr);
    while((thisLine=br.readLine())!=null)
    {
    str=thisLine.split(" ");
    if(str[0].equalsIgnoreCase(itf.getText()))
    {
    ota.setText(thisLine);
        i=1;
    }
    }
    if(i==0)
    cdiag.setVisible(true);
    br.close();
    }
    catch(IOException a)
    {
    a.getStackTrace();
    }
    }
    else if(but==ebut)
    {
    String thisLine;
    int i=0;
    String[] str=new String[100];
    try
    {
          FileReader fr=new FileReader("e:\\Java\\Dictionary\\EC.txt");
         BufferedReader br=new BufferedReader(fr);
    while((thisLine=br.readLine())!=null)
    {
    str=thisLine.split(" ");
    if(str[0].equalsIgnoreCase(itf.getText()))
    {
    ota.setText(thisLine);
        i=1;
    }
    }
    if(i==0)
    cdiag.setVisible(true);
    br.close();
    }
    catch(IOException a)
    {
    a.getStackTrace();
    }
    }
    else if(but==dbut)
    {
    cdiag.setVisible(false);
    }
    }
    }还有这里面的几个this,虽然写着没问题但是不明白其中原理
      

  9.   

    this就是当前被调用对象本身
    比如
    public class Test {
        public static void main(String[] args) {
            A a1 = new A(10); //构造函数A的this指的是当前a1所引用的对象
            a1.print(); //print方法里的this指的是当前a1所引用的对象        A a2 = new A(15); //构造函数A的this指的是当前a2所引用的对象
            a2.print(); //print方法里的this指的是当前a2所引用的对象
        }
    }
    class A {
        int value;
        public A(int value) {
            this.value = value;
        }
        public void print
            System.out.println(this.value);
        }
    }所以,方法内部的this指的就是当前被调用的对象
    打个比方
    class 人 {
        String 名字;
        public 人(String 名字) {
            this.名字 = 名字; //这里的this就相当于我
        }
        public void print() {
            System.out.println("我的名字叫" + this.名字); //这个this就相当于我
        }
    }每个人都有自己的名字,那么张三可以称自己为“我”,李四也可以称自己是“我”,那么这个“我”到底是谁呢?很简单,就看是谁说的。如果是张三说的,那么我们就知道是张三,如果是李四说的,我们就知道是李四,同样的道理,类里面的定义的方法中用到的this就代表“我”,那么这个this究竟是哪个对象?那就看这个方法是哪个对象调用的,也就相当于这个“我”是谁说的。