基本信息
源码名称:java实现电子时钟(完整源代码)
源码大小:0.08M
文件格式:.zip
开发语言:Java
更新时间:2020-02-14
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
package com.mingrisoft.time; import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Calendar; import java.util.GregorianCalendar; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; import javax.swing.UIManager; public class ElectronicClock extends JFrame { /** * */ private static final long serialVersionUID = 3688891358876658465L; private JPanel contentPane; private ImageIcon colon = new ImageIcon("src/images/colon.png"); private ImageIcon[] numbers = { new ImageIcon("src/images/0.png"), new ImageIcon("src/images/1.png"), new ImageIcon("src/images/2.png"), new ImageIcon("src/images/3.png"), new ImageIcon("src/images/4.png"), new ImageIcon("src/images/5.png"), new ImageIcon("src/images/6.png"), new ImageIcon("src/images/7.png"), new ImageIcon("src/images/8.png"), new ImageIcon("src/images/9.png") }; private JLabel hour1Label; private JLabel hour2Label; private JLabel colon1Label; private JLabel minute1Label; private JLabel minute2Label; private JLabel colon2Label; private JLabel second1Label; private JLabel second2Label; /** * Launch the application. */ public static void main(String[] args) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run() { try { ElectronicClock frame = new ElectronicClock(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ElectronicClock() { addWindowListener(new WindowAdapter() { @Override public void windowActivated(WindowEvent arg0) { do_this_windowActivated(arg0); } }); setTitle("\u7535\u5B50\u65F6\u949F"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 350, 100); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new GridLayout(1, 8, 5, 5)); JPanel hour1Panel = new JPanel(); contentPane.add(hour1Panel); hour1Label = new JLabel(""); hour1Label.setHorizontalAlignment(SwingConstants.CENTER); hour1Panel.add(hour1Label); JPanel hour2Panel = new JPanel(); contentPane.add(hour2Panel); hour2Label = new JLabel(""); hour2Label.setHorizontalAlignment(SwingConstants.CENTER); hour2Panel.add(hour2Label); JPanel colon1Panel = new JPanel(); contentPane.add(colon1Panel); colon1Label = new JLabel(""); colon1Label.setHorizontalAlignment(SwingConstants.CENTER); colon1Panel.add(colon1Label); JPanel minute1Panel = new JPanel(); contentPane.add(minute1Panel); minute1Label = new JLabel(""); minute1Label.setHorizontalAlignment(SwingConstants.CENTER); minute1Panel.add(minute1Label); JPanel minute2Panel = new JPanel(); contentPane.add(minute2Panel); minute2Label = new JLabel(""); minute2Label.setHorizontalAlignment(SwingConstants.CENTER); minute2Panel.add(minute2Label); JPanel colon2Panel = new JPanel(); contentPane.add(colon2Panel); colon2Label = new JLabel(""); colon2Label.setHorizontalAlignment(SwingConstants.CENTER); colon2Panel.add(colon2Label); JPanel second1Panel = new JPanel(); contentPane.add(second1Panel); second1Label = new JLabel(""); second1Label.setHorizontalAlignment(SwingConstants.CENTER); second1Panel.add(second1Label); JPanel second2Panel = new JPanel(); contentPane.add(second2Panel); second2Label = new JLabel(""); second2Label.setHorizontalAlignment(SwingConstants.CENTER); second2Panel.add(second2Label); } private void getTime() { Calendar calendar = new GregorianCalendar(); int hour = calendar.get(Calendar.HOUR_OF_DAY); // ��õ�ǰ��Сʱ int minute = calendar.get(Calendar.MINUTE); // ��õ�ǰ�ķ��� int second = calendar.get(Calendar.SECOND); // ��õ�ǰ������ hour1Label.setIcon(numbers[hour / 10]);// �����̻��Сʱ��һλͼƬ hour2Label.setIcon(numbers[hour % 10]);// �����������Сʱ�ڶ�λͼƬ minute1Label.setIcon(numbers[minute / 10]);// �����̻�÷��ӵ�һλͼƬ minute2Label.setIcon(numbers[minute % 10]);// �����������Сʱ�ڶ�λͼƬ second1Label.setIcon(numbers[second / 10]);// �����̻�����ӵ�һλͼƬ second2Label.setIcon(numbers[second % 10]);// �����������Сʱ�ڶ�λͼƬ } private class ClockRunnable implements Runnable { @Override public void run() { while (true) { getTime(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } protected void do_this_windowActivated(WindowEvent arg0) { colon1Label.setIcon(colon); colon2Label.setIcon(colon); new Thread(new ClockRunnable()).start(); } }