swing storm

java桌面应用

导航

<2007年3月>
25262728123
45678910
11121314151617
18192021222324
25262728293031
1234567

统计

常用链接

留言簿(15)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜

swing components-半透明ToolTip

效果:
AzHOpaqueToolTip.gifAzHOpaqueToolTip1.gif
AzHOpaqueToolTip2.gifAzHOpaqueToolTip3.gif

代码:

package  azure.ui.swing.components.tooltip;

import  java.awt.AlphaComposite;
import  java.awt.Color;
import  java.awt.FlowLayout;
import  java.awt.Graphics;
import  java.awt.Graphics2D;
import  java.awt.Point;
import  java.awt.RenderingHints;
import  java.awt.event.MouseEvent;
import  java.awt.event.MouseListener;
import  java.awt.geom.RoundRectangle2D;
import  java.util.StringTokenizer;

import  javax.swing.JButton;
import  javax.swing.JFrame;
import  javax.swing.JLayeredPane;
import  javax.swing.JPanel;
import  javax.swing.SwingUtilities;
import  javax.swing.border.LineBorder;

/**
 * 
@author  Azure
 * 
@version  1.0 13/03/07
 
*/
public   class  AzHOpaqueToolTipExample  extends  JFrame {
    
public  AzHOpaqueToolTipExample() {
        init();
    }

    
public   void  init() {
        HalfOpaqueToolTip tooltip 
=   new  HalfOpaqueToolTip(
                
" 我们还能不能能不能再见面\n我在佛前苦苦求了几千年\n当我在踏过这条奈何桥之前\n让我再吻一吻你的脸 " ,
                
new  Color( 250 250 200 ), Color.RED, Color.BLACK, OPAQUE,  this );
        JButton button 
=   new  JButton( " swing " );
        button.addMouseListener(tooltip);

        
this .setLayout( new  FlowLayout());
        
this .add( new  JButton( " " ));
        
this .add( new  JButton( " " ));
        
this .add( new  JButton( " " ));
        
this .add( new  JButton( " " ));
        
this .add( new  JButton( " " ));
        
this .add(button);
        
this .add( new  JButton( " " ));
        
this .add( new  JButton( " " ));
        
this .add( new  JButton( " " ));
        
this .add( new  JButton( " " ));

        
this .setSize( 360 200 );
        
this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
this .setLocationRelativeTo( null );
        
this .setVisible( true );
    }

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

    
public   final   static   int  NOT_OPAQUE  =   0 ; //  不透明

    
public   final   static   int  HALF_OPAQUE  =   1 ; //  半透明

    
public   final   static   int  OPAQUE  =   2 ; //  透明(有边)

    
public   final   static   int  COMPLETE_OPAQUE  =   3 ; //  完全透明

    
class  HalfOpaqueToolTip  extends  JPanel  implements  MouseListener {
        
private  Color backGroundCol  =   null ;

        
private  Color foregroundColorCol  =   null ;

        
private  JFrame frame  =   null ;

        
private  String content  =   null ;

        
private   int  opaqueT  =   0 ;

        
public  HalfOpaqueToolTip(String tooltipContent, Color backGroundColor,
                Color foregroundColor, Color borderColor, 
int  opaqueType,
                JFrame frame) {
            content 
=  tooltipContent;
            backGroundCol 
=  backGroundColor;
            foregroundColorCol 
=  foregroundColor;
            opaqueT 
=  opaqueType;
            
/*
             * 调整tooltip的大小
             
*/
            
int  fristRowIndex  =  tooltipContent.indexOf( " \n " );
            String tooltipStr 
=   "" ;
            
if  (fristRowIndex  >   0 ) {
                tooltipStr 
=  tooltipContent.substring( 0 , tooltipContent
                        .indexOf(
" \n " ));
            } 
else  {
                tooltipStr 
=  tooltipContent;
            }

            
this
                    .setSize(tooltipStr.length() 
*   this .getFont().getSize()
                            
+   20 new  StringTokenizer(tooltipContent,  " \n " )
                            .countTokens() 
*   20   +   15 ); //  左右各留10的空白空间

            
this .setOpaque( false );
            
if  (opaqueType  !=  COMPLETE_OPAQUE) {
                
this .setBorder( new  LineBorder(borderColor));
            }
            
this .setVisible( false );
            
//  将本控件置于界面的最顶层
            frame.getLayeredPane().add( this , JLayeredPane.POPUP_LAYER);
            
this .frame  =  frame;
        }

        
protected   void  paintComponent(Graphics g) {
            
super .paintComponent(g);
            Graphics2D g2d 
=  (Graphics2D) g;
            
//  开始画平滑的图形
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(backGroundCol);
            
if  (opaqueT  ==  HALF_OPAQUE) {
                
//  将控件的透明度设置为60%
                AlphaComposite composite  =  AlphaComposite.getInstance(
                        AlphaComposite.SRC_OVER, 
60   /   100.0F );
                g2d.setComposite(composite);
                g2d.fill(
new  RoundRectangle2D.Float( 0 0 this .getWidth(),  this
                        .getHeight(), 
0 0 ));
            } 
else   if  (opaqueT  ==  NOT_OPAQUE) {
                
//  将控件的透明度设置为不透明
                AlphaComposite composite  =  AlphaComposite.getInstance(
                        AlphaComposite.SRC_OVER, 
100   /   100.0F );
                g2d.setComposite(composite);
                g2d.fill(
new  RoundRectangle2D.Float( 0 0 this .getWidth(),  this
                        .getHeight(), 
0 0 ));
            }
            g2d.setColor(foregroundColorCol);
            
//  恢复到默认情况
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_OFF);
            
/*
             * 画上字符内容
             
*/
            StringTokenizer contentTokenizer 
=   new  StringTokenizer(content,
                    
" \n " );
            
int  contentStartY  =   20 ;
            
while  (contentTokenizer.hasMoreElements()) {
                g2d.drawString(contentTokenizer.nextToken(), 
10 , contentStartY);
                contentStartY 
=  contentStartY  +   20 ;
            }

        }

        
public   void  mouseEntered(MouseEvent e) {
            Point p 
=  e.getLocationOnScreen();
            SwingUtilities.convertPointFromScreen(p, e.getComponent()
                    .getParent());
            
if  (p.x  +   this .getWidth()  >  frame.getX()) {
                
this .setLocation( new  Point(p.x  -   this .getWidth(), p.y));
            } 
else  {
                
this .setLocation( new  Point(p.x, p.y));
            }
            
this .setVisible( true );
        }

        
public   void  mouseExited(MouseEvent e) {
            
this .setVisible( false );
        }

        
public   void  mouseClicked(MouseEvent e) {

        }

        
public   void  mousePressed(MouseEvent e) {

        }

        
public   void  mouseReleased(MouseEvent e) {

        }
    }

}

posted on 2007-03-13 18:32 azure 阅读(3627) 评论(4)  编辑  收藏

评论

# re: swing components-半透明ToolTip 2007-03-13 19:07 BeanSoft

Java 2D 画图值得关注.. 目前 SWT 这方面还是有点弱.  回复  更多评论   

# re: swing components-半透明ToolTip 2007-03-13 19:19 山风小子

博主,您好!
以后贴代码时能否不要附带‘展开’功能和‘行号’,这令我哭笑不得。看到好的代码想自己运行一下,复制来的都是带'012..9'的代码,还要用ultraedit来取范围,去除行号。先谢谢了 :)  回复  更多评论   

# re: swing components-半透明ToolTip 2007-03-15 00:56 催月泪

受不了!强!强!强!UI是我的最爱......
你住哪呀?你那里人?你吃了没?  回复  更多评论   

# re: swing components-半透明ToolTip 2007-03-15 08:58 小石头

顶呀顶呀顶!经典!  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: