含動畫的java氣泡提示效果,右下角訊息提示窗

qq27456026發表於2010-08-18

訊息提示框類,包含動畫的設定,以及取工作列高度

TipWindow類由網上找來經過本人修改,現將修改後的小程式發給大家試用,你們可以改的更漂亮更豐富

 效果如圖

 

 

package test;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
 
/** 
 * 
 * @author lgh 
 */ 
public class TipWindow extends JDialog implements Runnable {  
 
    private static Dimension dim;  
    private int x,  y;  
    private int width,  height;  
    private static Insets screenInsets;      
 
  
    public TipWindow(int width,int height){
        dim = Toolkit.getDefaultToolkit().getScreenSize();  
        screenInsets   =   Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
     this.width=width;
     this.height=height;
        x = (int) (dim.getWidth() - width-3);  
        y = (int) (dim.getHeight()-screenInsets.bottom-3); 
     initComponents();
    } 
    /*
     * 開啟漸入效果
     * 開啟後3秒,視窗自動漸出
     * 若不需要漸出,註釋掉,sleep(3000)和close()方法
     */
    public void run() {  
        for (int i = 0; i <= height; i += 10) {  
            try {  
                this.setLocation(x, y - i);
                Thread.sleep(5);  
            } catch (InterruptedException ex) {  
                Logger.getLogger(TipWindow.class.getName()).log(Level.SEVERE, null, ex);  
            }  
        }
       
       
        try {
   Thread.sleep(3000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
        close();
    }    
    private void initComponents() {  
        this.setSize(width, height);  
        this.setLocation(x, y);  
        this.setBackground(Color.black);
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                close();    
            }
        });        
    }   
    public void close(){
     for (int i = 0; i <= height; i += 10) {  
            try {
                setLocation(x, y-height+i);
                Thread.sleep(5);                                  
            } catch (InterruptedException ex) {  
                Logger.getLogger(TipWindow.class.getName()).log(Level.SEVERE, null, ex);  
            }  
        }
 
     dispose();
    }
}
 

 

 

測試類 TipTest

 

package test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*
 * java氣泡提示效果
 * @author noobjava
 * @version 1.0
 * @since JDK1.6(建議)
 *
 */

public class TipTest extends Thread{
 private Map<String,String> feaMap=null;

 public TipTest(){
  feaMap=new HashMap<String,String>();
  feaMap.put("name", "noobjava氣泡提醒");
  feaMap.put("release", "2010-08-20 11:33:00");
  feaMap.put("feature", "1.含動畫漸入與漸出效果\n2.3秒後啟動動畫漸出效果");
  super.start();
 }
 public void run(){
        final TipWindow tw=new TipWindow(300,220);          
        tw.setTitle("noobjava動畫氣泡提示");
        JPanel headPan=new JPanel();
        JPanel feaPan=new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JPanel btnPan=new JPanel();
        JButton update=new JButton("確定");

//        feaPan.setBorder(BorderFactory.createMatteBorder(1, 2, 3, 0, Color.gray));
  JLabel head=new JLabel(feaMap.get("name")+",含以下功能");
  head.setPreferredSize(new Dimension(250,30));
  head.setForeground(Color.black);
  JTextArea feature=new JTextArea(feaMap.get("feature"));
  feature.setEditable(false);
  feature.setForeground(Color.red);
  feature.setFont(new Font("宋體",Font.PLAIN,13));
//  feature.setBackground(Color.ORANGE);
  
  feature.setPreferredSize(new Dimension(280,60));
  
        JScrollPane jfeaPan=new JScrollPane(feature);
        jfeaPan.setPreferredSize(new Dimension(283,80));
//        jfeaPan.setBorder(null);
        jfeaPan.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray));
       
        JLabel releaseLabel=new JLabel("釋出日期"+feaMap.get("release").substring(0,19));
        releaseLabel.setForeground(Color.gray);
       
       
  feaPan.add(jfeaPan);
  feaPan.add(releaseLabel);
  headPan.add(head);
  btnPan.add(update);
  tw.add(headPan,BorderLayout.NORTH);
  tw.add(feaPan,BorderLayout.CENTER);
  tw.add(btnPan,BorderLayout.SOUTH);
  
  update.addMouseListener(new MouseAdapter(){
   public void mouseClicked(MouseEvent e){
    JOptionPane.showMessageDialog(tw, "點這裡幹嗎?點那個XX關掉嘛");
   }
  });
  tw.setAlwaysOnTop(true);
  tw.setResizable(false);
  tw.setVisible(true);  
  tw.run();  
 }

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

}
 

相關文章