java 电子时钟_java简易电子时钟代码Word版

这篇博客分享了一段Java代码,用于创建一个简单的电子时钟。时钟在GUI中显示当前时间,并通过按钮控制启动和停止。代码使用了Swing库来构建用户界面,线程来实时更新时间,并实现了ActionListener接口来响应按钮事件。
摘要由CSDN通过智能技术生成

《java简易电子时钟代码Word版》由会员分享,可在线阅读,更多相关《java简易电子时钟代码Word版(3页珍藏版)》请在人人文库网上搜索。

1、传播优秀Word版文档 ,希望对您有帮助,可双击去除!import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import java.text.SimpleDateFormat;public class ClockJFrame extends JFrameprivate Date now=new Date();Panel buttons=new Panel();Button button_start=new Button(启动);Button button_interrupt=new B。

2、utton(停止);Clock label=new Clock();public ClockJFrame() /构造方法 super(电子时钟);this.setBounds(300,240,300,120);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setLayout(new BorderLayout();this.getContentPane().add(North,label);/初始化一个容器,用来在容器上添加一个标签this.getContentPane().add(South,buttons);buttons.setLayo。

3、ut(new FlowLayout();buttons.add(button_start);buttons.add(button_interrupt);setVisible(true);private class Clock extends Label implements ActionListener,Runnableprivate Thread clocker=null;private Date now=new Date();public Clock()button_start.addActionListener(this);button_interrupt.addActionListen。

4、er(this);SimpleDateFormat dateFormat = new SimpleDateFormat(HH:mm:ss);/可以方便地修改日期格式String t = dateFormat.format( now );this.setText(t);public void start()if(clocker=null)传播优秀Word版文档 ,希望对您有帮助,可双击去除!clocker=new Thread(this);clocker.start();public void stop()clocker=null;public void run()Thread currentT。

5、hread=Thread.currentThread();while(clocker=currentThread)now=new Date();SimpleDateFormat dateFormat = new SimpleDateFormat(HH:mm:ss);/可以方便地修改日期格式String t = dateFormat.format( now );this.setText(t);tryclocker.sleep(1000);catch(InterruptedException ie)JOptionPane.showMessageDialog(this,Thread error:+i。

6、e); public void actionPerformed(ActionEvent e)if (e.getSource()=button_start) clocker = new Thread(this); /重新创建一个线程对象clocker.start();button_start.setEnabled(false);button_interrupt.setEnabled(true);if (e.getSource()=button_interrupt) /单击中断按钮时clocker.stop(); /设置当前线程对象停止标记button_start.setEnabled(true);button_interrupt.setEnabled(false); /内部类结束public static void main(String args) ClockJFrame time=new ClockJFrame();传播优秀Word版文档 ,希望对您有帮助,可双击去除!运行结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值