黑马程序员技术交流社区

标题: 己经复习到GUI了写了个实时时钟表示一下 [打印本页]

作者: GoodBoy123    时间: 2014-6-22 16:00
标题: 己经复习到GUI了写了个实时时钟表示一下


package Time;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class WindowAndTime
{
private Frame f ;
private TextArea ta;

WindowAndTime()
{
  setWindow();
}
public void setWindow()
{
  f = new Frame("实时时钟");  
  
  f.setSize(170,65);
  f.setLocation(300,300);
  f.setLayout(new FlowLayout());
  f.add(setTextArea());
  
  setWindowEvent();
  f.setVisible(true);   

}
//文本框设置
public TextArea setTextArea()
{
  ta = new TextArea();
  ta.setColumns(20);
  ta.setRows(2);
  return ta;
}
//日期设置
public void setDate()
{
  while(true)
  {
   Date d = new Date();
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
   String str = sdf.format(d);
   ta.setText(str);
   try {
    Thread.sleep(1000);
   }
   catch (InterruptedException e)
   {
    e.printStackTrace();
   }
  }  
}
//事件监听器
public void setWindowEvent()
{
  f.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    System.exit(0);
   }
  });
}




}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package Time;
public class Test
{
public static void main(String args [])
{
  new WindowAndTime().setDate();;                              //主方法
}
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2