import java.awt.*;
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的黄平网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.concurrent.*;
public class Clock extends JFrame implements Runnable {
private JLabel label = new JLabel();
public Clock() {
this("Q6");
label.setFont(new Font("Dialog", Font.BOLD, 72));
flush();
this.add(label);
this.pack();
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(this);
}
public static void main(String[] args) {
Clock c = new Clock();
c.setVisible(true);
}
private void flush() {
String strTime = String.format("%tT", new Date());
label.setText(strTime);
}
public void run() {
while(true) {
flush();
try {
Thread.sleep(1000);
} catch(Exception e) {}
}
}
}
import java.awt.Color;
import java.awt.Font;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyTime {
JFrame frame=null;
JLabel label=null;
public MyTime(){
frame=new JFrame("时钟");
label=new JLabel();
label.setFont(new Font("幼圆",1,40));
label.setBackground(Color.BLUE);
frame.add(label);
frame.setSize(200,90);
frame.setLocation(500,300);
frame.setVisible(true);
}
public static void main(String[] args) {
MyTime mt=new MyTime();
new TimeThread(mt).start();
}
}
class TimeThread extends Thread{
private MyTime mt;
public TimeThread(MyTime mt){
this.mt=mt;
}
public void run(){
while(true){
String fullTime=new Date().toString();
String time=fullTime.substring(11, 19);
mt.label.setText(time);
mt.label.repaint();
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
主方法应该这样写:
public static void main(String[] args)
{
Clock2 clock=new Clock2();
new Thread(clock).start();
}
此外,运行时是什么情况?最好描述一下,有助于别人理解。