很多人可能对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);
}
}
}
这里用到了其他的一些类,由于篇幅的原因,就不往上贴了。源代码下载地址:
http://download.csdn.net/download/luoweifu/4590710
分享到:
相关推荐
这是java种用JWidow写一个可以在桌面上拖动的小时钟
1,可以悬浮在桌面上,悬浮在其它程序之上,透明底色,不会遮挡其它程序界面; 2,可以将时钟拖动到桌面任何地方; 3,可以显示秒的跳动(采用电脑本身的时间) 4,可以自定义时钟的字体、字体大小和字体颜色–...
一个透明的桌面时钟小程序,功能自测,附源码
用flash做的一个小钟表,分针和时针可以拖动。用来给小学生教学钟表使用。
水晶桌面时钟, 拖动右下角可以改变大小, 双击时钟将其关闭。
一个简单的桌面数字时钟,可以鼠标左键随意放置,半透明,右键点击退出
要更改桌面时钟位置垂直,你只需要左键单击桌面上的时钟(它的字母之一),并按住鼠标左键向下拖动桌面时钟所需要的新的垂直位置。时钟颜色或透明度的变化不会生效,直到单击“设置”按键。可设置语音报时类别,如每...
这是一个可以拖拽设置时间的时钟,方便使用
★云备份,只需用鼠标拖动要备份的文件(一般10M以内)到大字体数字时钟上即可以附件的方式发送到你所设定的邮箱。本人觉得拖动备份已经很实用,如果有网友需要定时备件的,以后还可以加上。 ★网络速速测试功能 ★...
Android上类似桌面应用的一个GridView,可以拖动位置,支持多页,可以拖动子项到另一页。
可拖拽时钟
实现功能有,拖拽、合并文件、换位置
桌面概念时钟 XinBSConceptClock_v1.2,单文件,直接双击运行,可在桌面拖动并锁定
365效率专家是一款漂亮、免费的桌面日历、备忘便笺、计划管理软件,可始终浮动在所有窗口前面,不用时通过双击标题栏可收缩为一个可拖动的条状浮动窗,支持窗体透明度设置、和桌面背景的融合、屏幕四周的磁性停靠、...
C#代码,运用GDI绘图,功能齐全,欢迎下载。
大字体数字时钟,真人语音报时,实时显示本地天气预报(自动获取),实时监控本机网络状态(每秒流量和是否掉线),内置电脑终极省电模式(类似鲁大师),透明窗体在屏幕内做类似屏保的飘移运动,内存占用极小(几百K,...
仿iphone桌面拖动排序 仿苹果桌面 仿iphone ios桌面 launcher 本人见市场上很少仿排序拖拉这样的算法。所以改android源码。供大家学习使用哦。 这是android仿ios桌面的应用,支持拖动排序。 高仿iphone桌面,排列...
适用于Win10系统的桌面倒计时小工具,时刻提醒距离截止时间的天数,方便...拖动小工具位置需将鼠标移至小工具右上角有凸点的位置,然后按住左键拖动至桌面合适的位置,在小工具上右击会有彩蛋(前端显示和透明设置)。
HTML中可拖动来改变框架大小,此范例来源于一个路由,值得学习和借签
一个透明的椭圆形小时钟,点击最右端关闭,无标题栏,可以用鼠标拖动在整个屏幕移动,时钟始终保持在所有窗口的最前端。