当前位置: 首页 > 面试题库 >

Java时钟未计入Swing中

戴嘉珍
2023-03-14
问题内容

我正在尝试使用摆陀制作秒表,但无法正常工作。这是我的代码。Jlabel时钟始终显示-1,只有在它停止时才发生。我是否正确使用了invokelater?

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class sidePanel extends JApplet implements ActionListener{
    JPanel pane;
    JLabel clock;
    JButton toggle;

    Timer timer;
    StopWatch stopWatch;


    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, this);
        timer.setRepeats(true);

        stopWatch = new StopWatch();



        add(pane);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == toggle)
        {


            if(timer.isRunning())
            {
                stopWatch.endTime = System.currentTimeMillis();
                timer.stop();
            }
            else
            {
                stopWatch.startTime = System.currentTimeMillis();
                timer.start();
            }
        }

        if(e.getSource() == timer)
        {
            long time = stopWatch.getElapsedTime();
            sidePanel.this.clock.setText(String.valueOf(time));
        }
    }




    private class StopWatch{

        private long startTime =0;
        private long endTime =0;
        public boolean isRunning = false;


        public void start(){
            startTime = System.currentTimeMillis();
            isRunning = true;
        }

        public void end(){
            endTime = System.currentTimeMillis();
            isRunning = false;
        }

        public long getElapsedTime()
        {
            long currentTime = System.currentTimeMillis();
            if(isRunning)
                return (currentTime - startTime)/1000;
            else
                return -1;
        }

    }






}

工作代码

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class sidePanel extends JApplet implements ActionListener{
    JPanel pane;
    JLabel clock;
    JButton toggle;

    Timer timer;
    //StopWatch stopWatch;

    boolean pressed = false;

    long startTime =0;
    long endTime =0;


    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, this);
        timer.setRepeats(true);

        //stopWatch = new StopWatch();



        add(pane);

    }

    long cur;
    long end;
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == toggle)
        {

            if(!pressed)
            {
                timer.start();
                startTime = System.currentTimeMillis();
                pressed = true;
            }
            else
            {
                timer.stop();

                pressed = false;
            }
        }



            if(timer.isRunning())
            {
                endTime = System.currentTimeMillis();
                clock.setText(String.valueOf((endTime-startTime)/1000));

            }

    }

}

问题答案:

您的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方法时终止,在本例中为您StopWatchrun方法。

你需要做的,什么是维持一个循环,直到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);

}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == toggle)
    {
        if(timer.isRunning())
        {
            endTime = System.currentTimeMillis();
            timer.stop();
        }
        else
        {
            startTime = System.currentTimeMillis();
            timer.start();
        }
    }
}

然后,您可以从中剥离功能StopWatch(即getElapsedTime()



 类似资料:
  • 主要内容:1 案例-Swing实现时钟1 案例-Swing实现时钟 输出结果为:

  • 本文向大家介绍使用Swing绘制动态时钟,包括了使用Swing绘制动态时钟的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了利用Swing绘制一个动态时钟的具体代码,供大家参考,具体内容如下 效果 代码在下面,可跳过解析。 前言 编程实现一个时钟 利用Swing绘制一个时钟,只能是静态的。利用Calendar类获取当前的时分秒,然后根据数学公式绘制相应的时钟就可以了。 如果静态的时钟已

  • 本文向大家介绍JavaScript电子时钟倒计时,包括了JavaScript电子时钟倒计时的使用技巧和注意事项,需要的朋友参考一下 本文实例讲解了JavaScript电子时钟倒计时的详细代码,分享给大家供大家参考,具体内容如下 JavaScript时间类 1、获取时分秒:          getHours()          getMinutes();          getSeconds(

  • 问题内容: 我正在尝试制作绘画程序,但是在拖动鼠标时无法绘制线条。看来涂料一直在刷新,因此只绘制了鼠标的当前位置。我对此有些陌生,因此如何在拖动鼠标时将所有行显示在JPanel上?谢谢,这是我所拥有的: 问题答案: 有多种方法可以解决您的问题。@MattiasF和@MadProgrammer是正确的:Swing在做它应该做的事情。您的方法应该重绘整个场景,而不是添加到前一个场景。 到目前为止,建议

  • 我有一个Java swing GUI,使用其他窗口管理器可以很好地运行和显示,但当我在Xmonad中运行它时,它不能正确显示。所有显示的是窗口的框架,它是灰色的,没有按钮,菜单等。我如何使Swing GUI显示正确?

  • 以下是上述应用程序的代码(包括2个类:Main和ClockPanel) 问题是:如果我删除该行(正如我在上面的代码中所评论的那样),时钟将不会运行。我完全不知道为什么会这样。有人能给我解释一下吗?是因为系统。出来println()影响正在运行的线程或其他什么?