Java 获取时钟更新

发布于 2024-12-21 02:35:26 字数 1546 浏览 1 评论 0原文

我正在为学校做一个项目,但我有点陷入困境。我编写了一个显示当前时间的程序,但我不知道如何让它随着时间的变化而更新。如果有人可以帮助我,或者指出我开始的方向,我们将不胜感激。我将在下面发布我到目前为止所写的内容。

import javax.swing.*;
import java.awt.*;
import java.util.*;


public class Project2 extends JFrame{
public static void main(String[] args){
    Project2 myFrame = new Project2();
    myFrame.pack();
    myFrame.setTitle("Digital Clock");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setLocationRelativeTo(null);
    myFrame.setVisible(true);


}//main()

public Project2(){
System.out.println(currentTime());
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
JLabel time = new JLabel(currentTime());
time.setFont(new Font("TimesRoman", Font.BOLD, 20));
time.setForeground(Color.blue);
p1.add(time);
this.setLayout(new BorderLayout());
this.add(p1, BorderLayout.CENTER);

}

public String currentTime(){
    Calendar calendar = Calendar.getInstance();
   int hours = calendar.get(Calendar.HOUR_OF_DAY);
   int minutes = calendar.get(Calendar.MINUTE);
   int seconds = calendar.get(Calendar.SECOND);
   int aP = calendar.get(Calendar.AM_PM);
   String currentTime = hours+":"+checkTime(minutes)+":"+checkTime(seconds)+" "+amP(aP);
   return currentTime;
 }

public String checkTime(int t){
    String time1;
    if (t < 10){
        time1 = ("0"+t);
        }
    else{
        time1 = (""+t);
        }
    return time1;
}

public String amP(int ap){
    String amPm;
    if( ap == 0)
        amPm = "AM";
    else
        amPm = "PM";
    return amPm;
}



}//Project2

I'm working on a project for school, and I'm sorta stuck. I have written a program the displays the current time, but I can't figure out how to get it to update as the time changes. If some could help me, or point me in some direction to get started, your help will be appreciated. I will post what I have written so far below.

import javax.swing.*;
import java.awt.*;
import java.util.*;


public class Project2 extends JFrame{
public static void main(String[] args){
    Project2 myFrame = new Project2();
    myFrame.pack();
    myFrame.setTitle("Digital Clock");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setLocationRelativeTo(null);
    myFrame.setVisible(true);


}//main()

public Project2(){
System.out.println(currentTime());
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
JLabel time = new JLabel(currentTime());
time.setFont(new Font("TimesRoman", Font.BOLD, 20));
time.setForeground(Color.blue);
p1.add(time);
this.setLayout(new BorderLayout());
this.add(p1, BorderLayout.CENTER);

}

public String currentTime(){
    Calendar calendar = Calendar.getInstance();
   int hours = calendar.get(Calendar.HOUR_OF_DAY);
   int minutes = calendar.get(Calendar.MINUTE);
   int seconds = calendar.get(Calendar.SECOND);
   int aP = calendar.get(Calendar.AM_PM);
   String currentTime = hours+":"+checkTime(minutes)+":"+checkTime(seconds)+" "+amP(aP);
   return currentTime;
 }

public String checkTime(int t){
    String time1;
    if (t < 10){
        time1 = ("0"+t);
        }
    else{
        time1 = (""+t);
        }
    return time1;
}

public String amP(int ap){
    String amPm;
    if( ap == 0)
        amPm = "AM";
    else
        amPm = "PM";
    return amPm;
}



}//Project2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你丑哭了我 2024-12-28 02:35:26

将 JPanel 和 JLabel 声明为全局变量,并在末尾添加以下行
构造函数

ActionListener taskPerformer = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println(currentTime());
                    time.setText(currentTime());
                }
            };
            Timer t = new Timer(1000, taskPerformer);
            t.start();

这是完整的代码:

import javax.swing.*;
import javax.swing.Timer;

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

public class DigitalClock extends JFrame {

    private JPanel p1;
    private JLabel time;

    public static void main(String[] args) {
        DigitalClock myFrame = new DigitalClock();
        myFrame.pack();
        myFrame.setTitle("Digital Clock");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setLocationRelativeTo(null);
        myFrame.setVisible(true);

    }// main()

    public DigitalClock() {
        System.out.println(currentTime());
        p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        time = new JLabel(currentTime());
        time.setFont(new Font("TimesRoman", Font.BOLD, 20));
        time.setForeground(Color.blue);
        p1.add(time);
        this.setLayout(new BorderLayout());
        this.add(p1, BorderLayout.CENTER);
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println(currentTime());
                time.setText(currentTime());
            }
        };
        Timer t = new Timer(1000, taskPerformer);
        t.start();
    }

    public String currentTime() {
        Calendar calendar = Calendar.getInstance();
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minutes = calendar.get(Calendar.MINUTE);
        int seconds = calendar.get(Calendar.SECOND);
        int aP = calendar.get(Calendar.AM_PM);
        String currentTime = hours + ":" + checkTime(minutes) + ":"
                + checkTime(seconds) + " " + amP(aP);
        return currentTime;
    }

    public String checkTime(int t) {
        String time1;
        if (t < 10) {
            time1 = ("0" + t);
        } else {
            time1 = ("" + t);
        }
        return time1;
    }

    public String amP(int ap) {
        String amPm;
        if (ap == 0)
            amPm = "AM";
        else
            amPm = "PM";
        return amPm;
    }

}// Project2

Declare JPanel and JLabel as global variables and add the following lines at the end of the
constructor

ActionListener taskPerformer = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println(currentTime());
                    time.setText(currentTime());
                }
            };
            Timer t = new Timer(1000, taskPerformer);
            t.start();

Here is the complete code:

import javax.swing.*;
import javax.swing.Timer;

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

public class DigitalClock extends JFrame {

    private JPanel p1;
    private JLabel time;

    public static void main(String[] args) {
        DigitalClock myFrame = new DigitalClock();
        myFrame.pack();
        myFrame.setTitle("Digital Clock");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setLocationRelativeTo(null);
        myFrame.setVisible(true);

    }// main()

    public DigitalClock() {
        System.out.println(currentTime());
        p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        time = new JLabel(currentTime());
        time.setFont(new Font("TimesRoman", Font.BOLD, 20));
        time.setForeground(Color.blue);
        p1.add(time);
        this.setLayout(new BorderLayout());
        this.add(p1, BorderLayout.CENTER);
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println(currentTime());
                time.setText(currentTime());
            }
        };
        Timer t = new Timer(1000, taskPerformer);
        t.start();
    }

    public String currentTime() {
        Calendar calendar = Calendar.getInstance();
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minutes = calendar.get(Calendar.MINUTE);
        int seconds = calendar.get(Calendar.SECOND);
        int aP = calendar.get(Calendar.AM_PM);
        String currentTime = hours + ":" + checkTime(minutes) + ":"
                + checkTime(seconds) + " " + amP(aP);
        return currentTime;
    }

    public String checkTime(int t) {
        String time1;
        if (t < 10) {
            time1 = ("0" + t);
        } else {
            time1 = ("" + t);
        }
        return time1;
    }

    public String amP(int ap) {
        String amPm;
        if (ap == 0)
            amPm = "AM";
        else
            amPm = "PM";
        return amPm;
    }

}// Project2
哭了丶谁疼 2024-12-28 02:35:26

提示:查看计时器TimerTask 类。然后使用这些类来更新 JLabel 的文本。

Hint: Have a look at Timer and TimerTask classes. Then use these classes to update the JLabel's text.

偏爱你一生 2024-12-28 02:35:26

我同意Pradeep的观点,你必须使用一个计时器,例如你可以定义一个计时器,并使其每1秒调用一个方法,在这个方法中你可以更新显示的时间......

I am agree with Pradeep, you must use a timer, for example you can define a timer, and make that a method is call every 1 second, in this method you can update the displayed time...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文