主题:[讨论]一个显示时间的程序代码秒在动
mn960mn
[专家分:0] 发布于 2009-04-17 16:13:00
我想做一个显示时间的程序代码秒在动
代码如下
import java.util.*;
class times extends Thread{
public void main(String[] args)throws Exception{
while(true){
Date d=new Date();
System.out.println(d);
Thread.sleep(1000);
}
}
}
程序每隔1秒就打印一次时间
但是却不清屏
请问如何解决
回复列表 (共1个回复)
沙发
瞬间移动 [专家分:320] 发布于 2009-04-24 09:30:00
java下面实现控制台清屏需要使用JNI吧。
下面是图形界面实现:
//ShowTimeApp.java
import javax.swing.JFrame;
public class ShowTimeApp extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String args[]) {
ShowTimeApp frame = new ShowTimeApp();
frame.setVisible(true);
}
public ShowTimeApp() {
super();
setBounds(100, 100, 240, 67);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ShowTimePanel showTimePanel = new ShowTimePanel();
getContentPane().add(showTimePanel);
}
}
//ShowTimePanel.java
import java.awt.Graphics;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ShowTimePanel extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private Thread showTimeThread;
private JLabel timeLabel;
public ShowTimePanel() {
super();
timeLabel = new JLabel();
timeLabel.setText(new Date().toString());
add(timeLabel);
showTimeThread = new Thread(this);
showTimeThread.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
timeLabel.setText(new Date().toString());
}
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
}
我来回复