现在的位置: 首页 > 综合 > 正文

Java模拟时钟

2013年03月04日 ⁄ 综合 ⁄ 共 5790字 ⁄ 字号 评论关闭

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); // 设置窗口可见

	}

}

================================================panel2.java=============================================================

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;

import javax.swing.Timer;

public class panel2 extends Panel
{
	private int degree = 0;
	final double pi = Math.PI;
	final double R = 100;
	Timer timer;
	private int hour, min, sec;

	public panel2()
	{
		setPreferredSize(new Dimension(150, 200));
		timer = new Timer(1000, new myActionListener());
		timer.start();

		GregorianCalendar date = new GregorianCalendar();
		hour = date.getTime().getHours();
		if (hour > 12)
		{
			hour = hour - 12;
		}
		hour = hour * 3;
		min = date.getTime().getMinutes();
		min = min * 6;
		sec = date.getTime().getSeconds();
		sec = sec * 6;
	}

	public void paint(Graphics g)
	{
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		// g.fillRect(10, 10, 100, 100);

		g2d.fillOval(ClockCenter.centerX - 1, ClockCenter.centerY - 1, 5, 5);
		for (int i = 6; i <= 360; i += 6)
		{
			if (i % 30 != 0)
			{
				// System.out.println(i/pi);
				g2d.setColor(Color.black);
				g2d.fillOval((int) (ClockCenter.centerX + R
						* Math.cos((double) i / 180 * pi - pi / 2)),
						(int) (ClockCenter.centerX + R
								* Math.sin((double) i / 180 * pi - pi / 2)), 3,
						3);
			} else
			{
				if (i % 90 == 0)
				{
					g2d.setColor(Color.red);
				} else
				{
					g2d.setColor(Color.black);
				}
				// g2d.setFont(new Font("SAN_SERIF",Font.BOLD,15));
				g2d
						.drawString(String.valueOf(i / 30),
								(int) (ClockCenter.centerX + R
										* Math.cos((double) i / 180 * pi - pi
												/ 2)) - 3,
								(int) (ClockCenter.centerX + R
										* Math.sin((double) i / 180 * pi - pi
												/ 2)) + 5);
			}
		}

		/****************************************************************************/
		// 画指针
		g2d.setColor(Color.red);
		g2d.drawLine(ClockCenter.centerX, ClockCenter.centerY,
				(int) (ClockCenter.centerX + (R - 10)
						* Math.cos((double) sec / 180 * pi - pi / 2)),
				(int) (ClockCenter.centerX + (R - 10)
						* Math.sin((double) sec / 180 * pi - pi / 2)));
		g2d.setColor(Color.blue);
		g2d.drawLine(ClockCenter.centerX, ClockCenter.centerY,
				(int) (ClockCenter.centerX + (R - 20)
						* Math.cos((double) min / 180 * pi - pi / 2)),
				(int) (ClockCenter.centerX + (R - 20)
						* Math.sin((double) min / 180 * pi - pi / 2)));
		g2d.setColor(Color.black);
		g2d.drawLine(ClockCenter.centerX, ClockCenter.centerY,
				(int) (ClockCenter.centerX + (R - 30)
						* Math.cos((double) hour / 180 * pi - pi / 2)),
				(int) (ClockCenter.centerX + (R - 30)
						* Math.sin((double) hour / 180 * pi - pi / 2)));
		// 画数字钟

	}

	public class myActionListener implements ActionListener
	{
		@SuppressWarnings("deprecation")
		public void actionPerformed(ActionEvent e)
		{
			if (e.getSource() == timer)
			{
				GregorianCalendar date = new GregorianCalendar();
				hour = date.getTime().getHours();
				if (hour > 12)
				{
					hour = hour - 12;
				}
				hour = hour * 30;
				min = date.getTime().getMinutes();
				min = min * 6;
				sec = date.getTime().getSeconds();
				sec = sec * 6;
				// System.out.println(sec);
				// degree+=6;
				// System.out.println(degree);
				repaint();
			}
		}
	}
}

抱歉!评论已关闭.