Create a digital clock a) Create the layout for your digital clock. The clock should have the following layout: so: ● HH - displays the hours ● MM - displays the minutes ● SS - displays the seconds ● AM/PM - displays whether it is before (AM) or after mid-day (PM). The hours/minutes/seconds should be displayed in 36 point, italic serif. (Correct layout: 0.25P, Class extends JFrame: 0.25P, Class follows OOP principles: 0.25P, Global set-up in main method: 0.25P)1 b) Make the clock show the current time. For this you should use the Calendar class from java.util with: ● Calendar.getInstance() - create Calendar object for current date/time ● get(field) - return integer field from Calendar object, where field may be Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND, Calendar.AM_PM, etc. ● the value returned from Calendar.AM_PM may be compared with the values Calendar.AM and Calendar.PM to determine if it is before or after mid-day Create a function that runs continuously and updates the display to always show the current time
时间: 2024-02-16 15:00:09 浏览: 95
a) 首先我们需要创建一个布局来显示数字时钟,布局应该包含以下内容:
- HH:显示小时
- MM:显示分钟
- SS:显示秒钟
- AM/PM:显示是上午还是下午
其中,小时/分钟/秒钟应该使用36号斜体衬线字体显示。以下是一个示例布局:
```
_______________________
| | | |
| HH | MM | SS |
|_______|_______|_______|
| | |
| AM/PM | DATE |
|______________|________|
```
b) 接下来我们需要让时钟显示当前时间。为此,我们可以使用Java的`Calendar`类,具体步骤如下:
1. 使用`Calendar.getInstance()`方法创建一个代表当前日期/时间的`Calendar`对象。
2. 使用`get(field)`方法从`Calendar`对象中获取所需的时间字段(例如,`Calendar.HOUR`表示小时,`Calendar.MINUTE`表示分钟,`Calendar.SECOND`表示秒钟,`Calendar.AM_PM`表示上午/下午等)。
3. 将`Calendar.AM_PM`返回的值与`Calendar.AM`和`Calendar.PM`进行比较,以确定是上午还是下午。
为了使时钟不断更新,我们可以创建一个函数,并使用`Thread.sleep(millis)`方法将其放入一个无限循环中,以便每秒钟更新一次时钟。
下面是一个示例代码,实现了上述功能:
```java
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.*;
public class DigitalClock extends JFrame {
private JLabel hourLabel, minuteLabel, secondLabel, amPmLabel, dateLabel;
public DigitalClock() {
// 设置窗口标题、大小和布局
setTitle("Digital Clock");
setSize(300, 150);
setLayout(new GridLayout(2, 1));
// 创建时钟数字标签
Font font = new Font("Serif", Font.ITALIC, 36);
hourLabel = new JLabel("00");
hourLabel.setFont(font);
minuteLabel = new JLabel("00");
minuteLabel.setFont(font);
secondLabel = new JLabel("00");
secondLabel.setFont(font);
// 创建上午/下午标签和日期标签
amPmLabel = new JLabel("AM");
dateLabel = new JLabel();
// 将标签添加到窗口中
JPanel panel1 = new JPanel(new GridLayout(1, 3));
panel1.add(hourLabel);
panel1.add(minuteLabel);
panel1.add(secondLabel);
add(panel1);
JPanel panel2 = new JPanel(new GridLayout(1, 2));
panel2.add(amPmLabel);
panel2.add(dateLabel);
add(panel2);
// 设置窗口可见性和关闭方式
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void updateTime() {
while (true) {
// 获取当前日期/时间
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int amPm = calendar.get(Calendar.AM_PM);
// 更新时钟数字标签
hourLabel.setText(String.format("%02d", hour));
minuteLabel.setText(String.format("%02d", minute));
secondLabel.setText(String.format("%02d", second));
// 更新上午/下午标签
amPmLabel.setText(amPm == Calendar.AM ? "AM" : "PM");
// 更新日期标签
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateLabel.setText(dateFormat.format(calendar.getTime()));
try {
// 休眠1秒钟
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
DigitalClock clock = new DigitalClock();
clock.updateTime();
}
}
```
运行该程序,将会显示一个数字时钟窗口,并且窗口上的数字将会随着时间的推移而更新。
阅读全文