查看: 856|回复: 2
|
有可能用Java在GUIclass 里显示时间吗??
[复制链接]
|
|
我的program是用java写的。我有一个GUI class, 我要如何写把时间显示在我的GUI 里??
我目前做到的能显示时间,不过那时间不能“走”。请问能做到那时间像window的时间一直run的吗?? |
|
|
|
|
|
|
|
发表于 11-12-2006 11:56 PM
|
显示全部楼层
原帖由 jeftlee 于 10-12-2006 11:41 PM 发表
我的program是用java写的。我有一个GUI class, 我要如何写把时间显示在我的GUI 里??
我目前做到的能显示时间,不过那时间不能“走”。请问能做到那时间像window的时间一直run的吗??
用Thread来更新就ok了.
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.WindowConstants;
- public class ShowMyTime extends JFrame{
- private JLabel timeLabel;
-
- private Timer timer;
- private TimerTask timerTask;
-
- public ShowMyTime(){
- initComponents();
- initTimer();
- }
- private void initTimer() {
- timerTask = new TimerTask(){
- @Override
- public void run() {
- doReadTime();
- }
- };
-
- timer = new Timer();
- timer.scheduleAtFixedRate(timerTask, 0, 1000);
- }
- protected void doReadTime() {
- Date now =Calendar.getInstance().getTime();
- timeLabel.setText(now.toString());
- }
- private void initComponents() {
- timeLabel = new JLabel();
-
- this.getContentPane().add(timeLabel, BorderLayout.NORTH);
- this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
- this.pack();
-
- this.setSize(new Dimension(200,300));
- this.setLocationRelativeTo(null);
- }
-
- public static void main(String[] args) {
- ShowMyTime showMyTime = new ShowMyTime();
-
- showMyTime.setVisible(true);
- }
- }
复制代码 |
|
|
|
|
|
|
|

楼主 |
发表于 12-12-2006 02:14 AM
|
显示全部楼层
原帖由 黑木头 于 11-12-2006 11:56 PM 发表
用Thread来更新就ok了.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
...
真的太谢谢你了。。我的问题终于解决了。。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|