很多人可能对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); } }}这里用到了其他的一些类,由于篇幅的原因,就不往上贴了。源代码下载地址: