栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java时钟未计入Swing中

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java时钟未计入Swing中

您的

StopWatch
课程运行一次,然后终止…

public void run() {    // Start here    SwingUtilities.invokeLater(new Runnable()    {        @Override        public void run() { long time = getElapsedTime(); sidePanel.this.clock.setText(String.valueOf(time));        }    });    // End here...}

线程将在存在于其

run
方法时终止,在本例中为您
StopWatch
run
方法。

你需要做的,什么是维持一个循环,直到

isRunning
变成
false

public void run() {    while (isRunning) {        SwingUtilities.invokeLater(new Runnable()        { @Override public void run() {     long time = getElapsedTime();     sidePanel.this.clock.setText(String.valueOf(time)); }        });        // Because we really don't want to bombboard the Event dispatching thread        // With lots of updates, which probably won't get rendered any way,        // We put in a small delay...        // This day represents "about" a second accuracy...        try { Thread.sleep(500);        } catch (Exception exp) {        }    }}

javax.swing.Timer
尽管使用… 会更简单…

private Timer timer;public void init(){    pane = new JPanel();    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));    clock = new JLabel("00:00");    toggle = new JButton("Start/Stop");    toggle.addActionListener(this);    pane.add(clock);    pane.add(toggle);    timer = new Timer(500, new ActionListener() {         public void actionPerformed(ActionEvent evt) { long time = getElapsedTime(); sidePanel.this.clock.setText(String.valueOf(time));     }    });    timer.setRepeats(true);    add(pane);}@Overridepublic void actionPerformed(ActionEvent e) {    if(e.getSource() == toggle)    {        if(timer.isRunning())        { endTime = System.currentTimeMillis(); timer.stop();        }        else        { startTime = System.currentTimeMillis(); timer.start();        }    }}

然后,您可以从中剥离功能

StopWatch
(即
getElapsedTime()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609756.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号