etc-day22

Sure, I can help you with that! Here is the layout code for the digital clock: ```java import java.awt.Font; import java.util.Calendar; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class DigitalClock extends JFrame { private JLabel hourLabel; private JLabel minuteLabel; private JLabel secondLabel; private JLabel amPmLabel; public DigitalClock() { setTitle("Digital Clock"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 150); setLocationRelativeTo(null); setResizable(false); JPanel panel = new JPanel(); hourLabel = new JLabel("00"); minuteLabel = new JLabel("00"); secondLabel = new JLabel("00"); amPmLabel = new JLabel("AM"); Font font = new Font("Serif", Font.ITALIC, 36); hourLabel.setFont(font); minuteLabel.setFont(font); secondLabel.setFont(font); amPmLabel.setFont(font); panel.add(hourLabel); panel.add(new JLabel(":")); panel.add(minuteLabel); panel.add(new JLabel(":")); panel.add(secondLabel); panel.add(amPmLabel); add(panel); setVisible(true); } public void updateTime() { 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); String amPmStr = amPm == Calendar.AM ? "AM" : "PM"; hourLabel.setText(String.format("%02d", hour)); minuteLabel.setText(String.format("%02d", minute)); secondLabel.setText(String.format("%02d", second)); amPmLabel.setText(amPmStr); } public static void main(String[] args) { DigitalClock clock = new DigitalClock(); while (true) { clock.updateTime(); try { Thread.sleep(1000); } catch (InterruptedException e) { // handle exception } } } } ``` This code creates a JFrame with four JLabels to display the time and AM/PM indicator. The updateTime() function uses the Calendar class to get the current time and updates the JLabels with the new time. Finally, the main method creates a DigitalClock object and continuously updates the time using the updateTime() function with a one second delay between each update.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值