Java模拟时钟
来源:互联网 发布:each遍历json对象二重 编辑:程序博客网 时间:2024/05/22 02:16
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Panel;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
-
- import javax.swing.JLabel;
-
- public class clockPanel extends Panel
- {
- Panel p1, p2, p3;
- JLabel label1;
- int year, month, day, week;
-
- public clockPanel()
- {
- setBackground(Color.yellow);
- setPreferredSize(new Dimension(250, 320));
- setLayout(new BorderLayout(10, 10));
-
- p1 = new Panel();
- label1 = new JLabel();
-
-
- SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日(EE)");
-
-
-
- label1.setText(df.format(new Date()));
- p1.add(label1);
-
- p2 = new panel2();
-
- p3 = new panel3();
-
-
- this.add(p1, BorderLayout.NORTH);
- this.add(p2, BorderLayout.CENTER);
- this.add(p3, BorderLayout.SOUTH);
- }
-
- }
- import java.awt.Container;
- import javax.swing.JFrame;
-
- public class myClock
- {
-
-
-
-
- public static void main(String[] args)
- {
- JFrame frame = new JFrame("时钟");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container contentPane = frame.getContentPane();
-
- contentPane.add(new clockPanel());
- frame.pack();
- frame.setVisible(true);
-
- }
-
- }
-
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Panel;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.GregorianCalendar;
-
- import javax.swing.JLabel;
- import javax.swing.Timer;
-
- public class panel3 extends Panel
- {
- Timer timer;
- private int hour, min, sec;
-
- JLabel label2;
-
- public panel3()
- {
- label2 = new JLabel();
- label2.setFont(new Font("SAN_SERIF", Font.BOLD, 20));
- add(label2);
-
-
- timer = new Timer(1000, new myActionListener());
- timer.start();
-
- GregorianCalendar date = new GregorianCalendar();
- hour = date.getTime().getHours();
- min = date.getTime().getMinutes();
- sec = date.getTime().getSeconds();
- }
-
- public void paint(Graphics g)
- {
- super.paint(g);
-
-
- label2.setText(String.format("%1$,02d", hour) + ":"
- + String.format("%1$,02d", min) + ":"
- + String.format("%1$,02d", sec));
- }
-
- public class myActionListener implements ActionListener
- {
- @SuppressWarnings("deprecation")
- public void actionPerformed(ActionEvent e)
- {
- if (e.getSource() == timer)
- {
- GregorianCalendar date = new GregorianCalendar();
- hour = date.getTime().getHours();
- min = date.getTime().getMinutes();
- sec = date.getTime().getSeconds();
-
-
-
- repaint();
- }
- }
- }
- }
-
- public class ClockCenter
- {
- public static int centerX = 120;
- public static int centerY = 120;
- }
-
- import java.awt.Container;
- import javax.swing.JFrame;
-
- public class myClock
- {
-
-
-
-
- public static void main(String[] args)
- {
- JFrame frame = new JFrame("时钟");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container contentPane = frame.getContentPane();
-
- contentPane.add(new clockPanel());
- frame.pack();
- frame.setVisible(true);
-
- }
-
- }