java实现像QQ新闻消息提示对话框

 我们都知道QQ如果有什么重大新闻时会第一时间在右下角弹出一个消息提示框的.而且会有一些动画.实现这个其实并不难,很简单,关键在于它的一些外观,以及鼠标移动等操作.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lgh.mail.components;

import com.lgh.pic.Pic;
import com.lgh.util.MouseMotionUtil;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JWindow;

/**
 *
 * @author lgh
 */
public class TipWindow extends JWindow implements Runnable {

    private static Dimension dim;
    private int x,  y;
    private int width,  height;


    {
        dim = Toolkit.getDefaultToolkit().getScreenSize();
        width = 200;
        height = 150;
        x = (int) (dim.getWidth() - width);
        y = (int) (dim.getHeight());
    }

    public TipWindow() {
        initComponents();
        new Thread(this).start();
    }

    public void run() {
        for (int i = 0; i <= height; i += 10) {
            try {
                this.setLocation(x, y - i);
                Thread.sleep(20);
            } catch (InterruptedException ex) {
                Logger.getLogger(TipWindow.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void initComponents() {
        this.setSize(width, height);
        this.setLocation(x, y);
        this.setLayout(new BorderLayout());
        JPanel tipBar = createTipBar();
        new MouseMotionUtil().addMouseMotionListener(this, tipBar);
        this.add(tipBar, BorderLayout.NORTH);
        this.setVisible(true);

    }

    /**
     * 创建上面的工具条
     */
    private JPanel createTipBar() {
        TipBar tipBar = new TipBar(Pic.TOP_BAR_BG_MID);
        return tipBar;
    }

    private class TipBar extends MailPanel implements ActionListener {

        private MailButton closeButton;

        public TipBar(Image bgImage) {
            super(bgImage);
            initComponents();
        }

        private void initComponents() {
            closeButton = new MailButton(Pic.map.get("mail/buttons/button_close"), new Rectangle(170, 1, 26, 26));
            this.setLayout(null);
            closeButton.addListenAndCommand(this, "closeButton");
            this.add(closeButton);
            this.setPreferredSize(new Dimension(width, 26));
        }

        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if (command != null) {
                if (command.equals("closeButton")) {
                    TipWindow.this.dispose();
                }
            }
        }
    }

    public static void main(String[] args) {
        new TipWindow();
    }
}

  

你可能感兴趣的:(J2SE)