How do I implement the codes which works as a timer app using awt,swing,Thread while satisfying some conditions?

huangapple go评论103阅读模式
英文:

How do I implement the codes which works as a timer app using awt,swing,Thread while satisfying some conditions?

问题

Here is the translated content you requested:

我需要使用awt、swing和Thread编写Java的定时器代码。

最终应用程序的概述如下有4个特点:

  1. 应用程序只有一个按钮。

  2. 首先,按钮上显示“START”。

  3. 当按下按钮时,按钮上会显示动态时间。

  4. 在计时时按下按钮,按钮会停止计时并显示“START”。

我已经编写了以下代码。

boolean isCounting = false;

int cnt = 0;

void counter() {
    while (isCounting == true) {
        btn.setText(Integer.toString(++cnt));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public void actionPerformed(ActionEvent e) {
    if (isCounting == true) {
        isCounting = false;
    } else {
        isCounting = true;
        counter();
    }
}

当然,这段代码不满足条件,因为一旦按下按钮,按钮就不能再次按下,计数器也永远不起作用。

在这段代码中,一旦按下按钮,就会调用函数“counter”,但按钮上的值在按钮被释放之前永远不会更改。

我需要编写满足上述条件的代码。如何实现它?

英文:

I have to make the timer codes of Java using awt,swing,Thread.

The overview of the eventual app has below 4 features.

  1. The app has just one button.

  2. Firstly the button display the "START"on the button itself.

  3. Dynamic time is displayed on the button as the button is pressed.

  4. As the button pressed while counting the time,the button stop the counting and display "START".

I've written the code such as below.

boolean isCounting = false;

int cnt = 0;

void counter() {
	while (isCounting == true) {
		btn.setText(Integer.toString(++cnt));
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

public void actionPerformed(ActionEvent e) {
	if (isCounting == true) {
		isCounting = false;
	} else {
		isCounting = true;
		counter();
	}

}

Of course this code doesn't satisfy the conditions because once the button is pressed then
the button is no more able to be pressed again and the counter never works.

In this code,once the button is pressed then the function "counter" is called but the value on the button never changes until the button is unpressed.

I have to make the codes satisfying the above conditions.

How do I implement it?

答案1

得分: 0

如果我正确理解你的问题,那么我迅速拼凑的这段代码应该适用于你。

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Testing {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Testing t = new Testing();
            }
        });
    }

    private Timer timer;
    private JFrame frame;
    private JButton btn;
    private int timePassed;

    public Testing() {

        frame = new JFrame();
        timer = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                timePassed++;
                updateTimeOnButton();
            }
        });
        btn = new JButton("START");

        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (timer.isRunning()) {
                    timer.stop();
                    btn.setText("START");
                } else {
                    timePassed = 0;
                    timer.start();
                    updateTimeOnButton();
                }
            }
        });
        frame.getContentPane().add(btn);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(new Dimension(300, 300));
    }

    private void updateTimeOnButton() {
        btn.setText(timePassed + " seconds");
    }
}
英文:

If I understood your question correctly, then this snippet I quickly put together should work for you.

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Testing {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Testing t = new Testing();
            }
        });
    }

    private Timer timer;
    private JFrame frame;
    private JButton btn;
    private int timePassed;

    public Testing() {

        frame = new JFrame();
        timer = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                timePassed++;
                updateTimeOnButton();
            }
        });
        btn = new JButton("START");

        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (timer.isRunning()) {
                    timer.stop();
                    btn.setText("START");
                } else {
                    timePassed = 0;
                    timer.start();
                    updateTimeOnButton();
                }
            }
        });
        frame.getContentPane().add(btn);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(new Dimension(300, 300));
    }

    private void updateTimeOnButton() {
        btn.setText(timePassed + " seconds");
    }
}

huangapple
  • 本文由 发表于 2020年8月3日 20:16:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63229263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定