Java 获取时钟更新
我正在为学校做一个项目,但我有点陷入困境。我编写了一个显示当前时间的程序,但我不知道如何让它随着时间的变化而更新。如果有人可以帮助我,或者指出我开始的方向,我们将不胜感激。我将在下面发布我到目前为止所写的内容。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 JPanel 和 JLabel 声明为全局变量,并在末尾添加以下行
构造函数
这是完整的代码:
Declare JPanel and JLabel as global variables and add the following lines at the end of the
constructor
Here is the complete code:
提示:查看计时器和 TimerTask 类。然后使用这些类来更新 JLabel 的文本。
Hint: Have a look at Timer and TimerTask classes. Then use these classes to update the JLabel's text.
我同意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...