博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用JWidow写一个可以在桌面上拖动的小时钟
阅读量:6252 次
发布时间:2019-06-22

本文共 4631 字,大约阅读时间需要 15 分钟。

hot3.png

很多人可能对JWidow的使用不太清楚,下面就对JWidow写的一个小例子供参考!

import java.awt.*;

import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.net.URL;import java.util.Calendar;import javax.swing.*;/** * 用于显示时钟 * @author 罗伟富 * */public class ShowClock extends JWindow implements Runnable, ActionListener{	/**	 * 窗口的宽度	 */	public static final int WIDTH = 300;	/**	 * 窗口的宽度	 */	public static final int HEIGHT = 330;	/**	 * 时钟的宽度	 */	public static final int CWIDTH = 300;	/**	 *时钟的高度	 */	public static final int CHEIGHT = 300;	/**	 * 关闭按钮的宽度	 */	public static final int CLOSEWIDTH = 30;	/**	 * 关闭按钮的宽度	 */	public static final int CLOSEHEIGHT = 30;	/**	 *  度数转换成弧度的比例	 */	final double RAD = Math.PI / 180; 	/**	 * 时针、分针、秒针的参考半径	 */	public static final int R = 120;		private CalendarTime calTime;	private Image img;	private JPanel clockPanel;	private JButton closeButton;	private Thread thread;	private boolean isWorking = true;	//原点,半径(时、分、秒),角度,	private int ox, oy, r, h, m, s, hh, mm, ss;	private int X = 0, Y = 0;		/**	 * 构造函数	 * @param calTime Calendar对象,用于计时	 * @param img 用于作为背景的图像	 */	public ShowClock(CalendarTime calTime, Image img) {		this.calTime = calTime;		this.img = img;		thread = new Thread(this);		thread.start();		initialize();	}	/**	 * 为true,则计时器在计时,否则不计时	 * @return  为true,则计时器在计时,否则不计时	 */	public boolean isWorking() {		return isWorking;	}	/**	 * 设置isWork的状态	 * @param isWorking	 */	public void setWorking(boolean isWorking) {		this.isWorking = isWorking;	}	/**	 * 初始化组件	 */	public void initialize() {		ox = 150;		oy = 150;		final ImageIcon originalImg = new ImageIcon(this.getClass().getResource("/Images/close1.gif"));		final ImageIcon rolloverImg = new ImageIcon(this.getClass().getResource("/Images/close2.gif"));		closeButton = new ImageButton(originalImg);		closeButton.setRolloverIcon(rolloverImg);		closeButton.addActionListener(this);		clockPanel = new PaintPanel();		clockPanel.setPreferredSize(new Dimension(CWIDTH, CHEIGHT));		JLabel emptyLabel = new JLabel();		emptyLabel.setPreferredSize(new Dimension(WIDTH-CLOSEWIDTH, CLOSEHEIGHT));		Container c = getContentPane();		c.setLayout(new GridBagLayout());		GridBagConstraints gbc = new GridBagConstraints();		gbc.gridx = 0;		gbc.gridy = 0;		gbc.gridwidth = 1;		gbc.gridheight = 1;		gbc.fill = gbc.HORIZONTAL;		gbc.anchor = gbc.CENTER;		c.add(emptyLabel, gbc);		gbc.gridx = 1;		gbc.gridy = 0;		c.add(closeButton, gbc);		gbc.gridx = 0;		gbc.gridy = 1;		gbc.gridwidth = 2;		c.add(clockPanel, gbc);		setSize(WIDTH, HEIGHT);		addMouseListener(new MouseAdapter() {			public void mousePressed(MouseEvent e) {				X = e.getX();				Y = e.getY();			}		}		);		addMouseMotionListener(new MouseAdapter() {			public void mouseDragged(MouseEvent e) {				setLocation(getLocation().x + (e.getX()-X),getLocation().y + (e.getY()-Y));			}		}		);	}	/**	 * 每隔一秒中刷新一次显示时钟的屏幕	 */	public void run() {		try {			while(isWorking) { 				Thread.sleep(1000);				clockPanel.repaint();			}		} catch (InterruptedException e) {			e.printStackTrace();		}	}	/**	 * 	 */	public void actionPerformed(ActionEvent e) {		if(e.getSource() == closeButton) {			setVisible(false);		}	}		public static void main(String[] args) {		URL urlImg = ShowClock.class.getResource("/Images/clock.jpg");		Image imgClock = Toolkit.getDefaultToolkit().getImage(urlImg);		ShowClock showclock = new ShowClock(new CalendarTime(), imgClock);		showclock.setLocationRelativeTo(null);		showclock.setVisible(true);	}	/**	 * 用于画时钟的JPanel面板	 */	class PaintPanel extends JPanel {		public void paint(Graphics g) {			super.paint(g);			Graphics2D g2 = (Graphics2D)g;			g2.drawImage(img, 0, 0, CWIDTH, CHEIGHT, this);			 h = calTime.get(Calendar.HOUR);			 m = calTime.get(Calendar.MINUTE);			 s = calTime.get(Calendar.SECOND);			 ss = 90-6*s;			 mm = 90-(6*m+s/10);	//即 mm = 90-(6*m+6*(s/60));			 hh = 90-(30*h+m/2);	//即hh = 90-30*(m/60);			 g2.setStroke(new BasicStroke(1.0f));			 g2.setColor(Color.red);			 int x1 = (int)(0.8*R*Math.cos(ss*RAD)) + ox;			 int y1 = (int)(0.8*R*Math.sin(ss*RAD)) + oy;			 g2.drawLine(ox, oy, x1, CHEIGHT-y1);			 			 g2.setStroke(new BasicStroke(2.0f));			 g2.setColor(new Color(138, 43, 226));			 int x2 = (int)(0.7*R*Math.cos(mm*RAD)) + ox;			 int y2 = (int)(0.7*R*Math.sin(mm*RAD)) + oy;			 g2.drawLine(ox, oy, x2, CHEIGHT-y2);			 			 g2.setStroke(new BasicStroke(5.0f));			 g2.setColor(Color.black);			 int x3 = (int)(0.5*R*Math.cos(hh*RAD)) + ox;			 int y3 = (int)(0.5*R*Math.sin(hh*RAD)) + oy;			 g2.drawLine(ox, oy, x3, CHEIGHT-y3);		}	}}
这里用到了其他的一些类,由于篇幅的原因,就不往上贴了。源代码下载地址:

转载于:https://my.oschina.net/verynix/blog/365890

你可能感兴趣的文章
openj9
查看>>
继承现有的控件
查看>>
装逼语录:
查看>>
PHP函数
查看>>
[Leetcode]414. Third Maximum Number
查看>>
UTC引发时区配置和Linux系统时间和bios时间问题
查看>>
C语言32个关键字
查看>>
图像处理之canny---求梯度
查看>>
OpenGL编程轻松入门之一个简单的例子
查看>>
MVC控制器返回重定向操作
查看>>
LINUX总结
查看>>
编译php5.4的时候出现错误----configure: error: in `/usr/local/src/php540/php-5.4.0':
查看>>
机器学习编程01_线性回归
查看>>
Markdown语法
查看>>
《CSS世界》读书笔记(十六)
查看>>
初入前端
查看>>
(回文串 )Best Reward -- hdu -- 3613
查看>>
最少拦截系统------LCS--------动态规划
查看>>
关于EOF的种种。
查看>>
h5 拍照上传 代码
查看>>