- 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();
- //Calendar c = Calendar.getInstance();
- SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日(EE)");//设置日期格式
- //System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
- //System.out.println(new Date());
- //week = c.getTime().getDay();
- 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
- {
- /**
- * @param args
- */
- public static void main(String[] args)
- {
- JFrame frame = new JFrame("时钟");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点击x结束程序
- 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);
- // setPreferredSize(new Dimension(150,200));
- 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();
- // System.out.println(sec);
- // degree+=6;
- // System.out.println(degree);
- 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
- {
- /**
- * @param args
- */
- public static void main(String[] args)
- {
- JFrame frame = new JFrame("时钟");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点击x结束程序
- Container contentPane = frame.getContentPane();
- // 得到窗口内容面板
- contentPane.add(new clockPanel());
- frame.pack();
- frame.setVisible(true); // 设置窗口可见
- }
- }