如何在Java应用程序中添加倒计时代码?

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

How to add a countdown code to a Java app?

问题

这是我的建议:

你需要将倒计时的功能集成到你的 Java GUI 中。在你的代码中,首先你需要创建一个倒计时显示的标签,并在每秒更新标签的文本来实现倒计时效果。然后,你需要设置一个定时器来触发倒计时的更新。

以下是你可以采取的步骤:

  1. 导入所需的包:
    在你的代码顶部导入以下包,以便使用 Swing 定时器和相关组件:
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
  1. 创建一个用于显示倒计时的标签:
    在你的 Bildhintergrund 类中添加一个标签用于显示倒计时,类似于你之前创建的文本标签。
// 在类的成员变量中声明倒计时标签
private JLabel countdownLabel;

在构造函数中添加以下代码来创建并初始化倒计时标签:

countdownLabel = new JLabel();
countdownLabel.setFont(countdownLabel.getFont().deriveFont((float) 48));
countdownLabel.setBounds(300, 400, 200, 50);
add(countdownLabel);
  1. 创建倒计时定时器:
    在构造函数中创建一个定时器对象,用于每秒更新倒计时标签的文本。
Timer countdownTimer = new Timer(1000, new ActionListener() {
    int timet = 5 * 60; // 5 minutes in seconds

    @Override
    public void actionPerformed(ActionEvent e) {
        if (timet >= 0) {
            int minutes = timet / 60;
            int seconds = timet % 60;
            String timeText = String.format("%02d:%02d", minutes, seconds);
            countdownLabel.setText(timeText);

            if (timet == 0) {
                countdownLabel.setText("The Presentation will start shortly");
            }

            timet--;
        }
    }
});

countdownTimer.start();

将以上代码添加到你的构造函数中,这将创建一个定时器,每秒更新倒计时标签的文本。当倒计时达到 0 时,标签会显示 "The Presentation will start shortly"。

确保你根据需要调整标签的位置、字体大小和其他外观设置。

以上步骤将帮助你将倒计时功能集成到你的 Java GUI 中。记得在你的代码中添加所需的导入语句,并将相应的代码部分添加到你的类中。

英文:

I've recently got a project in school.

  • Create a Java GUI with an image as background and some textbox.

  • the first textbox is just a text that says: Lunch break at 1pm.

  • the second textbox is a countdown timer. This timer should appear 5 minutes before the end of the upcoming break, which counts down from 5:00 to 0:00 minutes every second. When reaching 0:00, the timer should respond to a text (e.g. "The
    Presentation will start shortly ").

  • Image as a background: done

  • Text that says: Lunch break at 1pm: done

  • Countdown: I'm stuck at this point. I program a countdown timer first on the console. But I really don't know how to include this countdown to a Java GUI.

Do you have any suggestions?

This is my code for the background:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JPanel;

import javax.swing.*;

public class Bildhintergrund extends JFrame{


	
	//Background
		public Bildhintergrund () {
        setTitle(" Bildhintergrund");
        setSize(Toolkit.getDefaultToolkit().getScreenSize());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(new BorderLayout());
        setContentPane(new JLabel(new ImageIcon("/path/to/bild.jpg")));
		setLayout(new FlowLayout());
		JLabel background = new JLabel();
				add(background);
		
		//Text
	
		JLabel text = new JLabel() {
		      public void paintComponent(Graphics g) {
		        super.paintComponent(g);
		        
		      }
		    };
		;
		    JPanel panel = new JPanel();
		    Dimension size = text.getPreferredSize();
		    getContentPane().add( text );
		    text.setFont(text.getFont().deriveFont((float) 58));
		    text.setText("Lunch break at 1pm");
		    /*text.setAlignmentX(0);
		    text.setAlignmentY(0);*/
		    text.setBounds(300, 300, size.width, size.height);
		    panel.add(text);
		    panel.setLayout(null);
		    add(text);
		}
		public static void main(String[] args) {
	new Bildhintergrund();
}
		}

This is my code for the countdown:

  {
    /*int Time = 5;
    String time;
    int seconds;
    int minutes;*/
    
    int timet= Time * 60; // Convert to seconds
    long delay = timet * 1000;

    do
    {
      minutes = timet / 60;
      seconds = timet % 60;
      time = minutes + ":"  + seconds;
  
	
      System.out.println(GetTimer());
   
      Thread.sleep(1000);
      timet = timet - 1;
      delay = delay - 1000;

    }
    while (delay != 0);
    System.out.println("Another topic will follow");
  
  }
  public static  String GetTimer()
  {
	
	  
  	return  time;
  }
}

答案1

得分: 1

您可以像以下示例一样使用javax.swing.Timer

javax.swing.Timer timer = new javax.swing.Timer(1000/*定时器调用ActionListener.actionPerformed(ActionEvent e)之间的延迟(以毫秒为单位)*/, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("时间到了!!!");
				//在此处放置您的代码
			}
		});
		timer.setRepeats(true);//使定时器无限重复(或直到调用timer.stop()方法)
		timer.start();
英文:

You can use javax.swing.Timer like this example:

javax.swing.Timer timer = new javax.swing.Timer(1000/*timer delay between calling ActionListener.actionPerformed(ActionEvent e) (in milliseconds)*/, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("Second over!!!");
				//put your code here
			}
		});
		timer.setRepeats(true);//makes timer repeats forever (or until call timer.stop() method)
		timer.start();

huangapple
  • 本文由 发表于 2020年10月9日 22:18:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64281853.html
匿名

发表评论

匿名网友

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

确定