swing storm

java桌面应用

导航

<2009年1月>
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

统计

常用链接

留言簿(15)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜

swing components-会话框ToolTip

效果:
一、
AzEllipseToolTip1.gif
二、
AzPolygonToolTip.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.Area;
import java.awt.geom.Ellipse2D;

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

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

    
public void init() {
        HalfOpaqueToolTip tooltip 
= new HalfOpaqueToolTip("让我再吻一吻你的脸!",
                
new Color(250250200), Color.RED, new Color(95145145),
                
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(new JButton(""));
        
this.add(new JButton(""));
        
this.add(new JButton(""));
        
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.setSize(380200);
        
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
this.setLocationRelativeTo(null);
        
this.setVisible(true);
    }

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

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

        
private Color foregroundColorCol = null;

        
private Color borderCol = null;

        
private String content = null;

        
private JFrame frame = null;

        
private int contentEllipseX = 30;

        
private int contentEllipseY = 20;

        
private int contentEllipseW = 0;

        
private int contentEllipseH = 0;

        
private int LEFT = 1;

        
private int RIGHT = 2;

        
private int direction = 2;

        
public HalfOpaqueToolTip(String tooltipContent, Color backGroundColor,
                Color foregroundColor, Color borderColor, JFrame frame) {
            content 
= tooltipContent;
            backGroundCol 
= backGroundColor;
            foregroundColorCol 
= foregroundColor;
            borderCol 
= borderColor;
            
/*
             * 调整tooltip的大小
             
*/
            content 
= tooltipContent;
            contentEllipseW 
= tooltipContent.getBytes().length * 6 + 10;
            contentEllipseH 
= 40;
            
this.setSize(contentEllipseX + contentEllipseW + 190);

            
this.setOpaque(false);
            
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);
            AlphaComposite composite 
= AlphaComposite.getInstance(
                    AlphaComposite.SRC_OVER, 
70 / 100.0F);
            g2d.setComposite(composite);
            g2d.fill(getArea());
            
// 恢复到默认情况
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_OFF);

            g2d.setColor(foregroundColorCol);
            
if (direction == LEFT) {
                g2d.drawString(content, (
intthis.getSize().getWidth()
                        
- contentEllipseX - contentEllipseW + 10,
                        contentEllipseY 
+ contentEllipseH / 2 + 5);
            } 
else {
                g2d.drawString(content, contentEllipseX 
+ 10, contentEllipseY
                        
+ contentEllipseH / 2 + 5);
            }
        }

        
protected void paintBorder(Graphics g) {
            
super.paintBorder(g);
            Graphics2D g2d 
= (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(borderCol);
            g2d.draw(getArea());
        }

        
private Area getArea() {
            
if (direction == LEFT) {
                
return getLeftArea();
            } 
else {
                
return getRightArea();
            }
        }

        
private Area getLeftArea() {
            Area area 
= new Area(new Ellipse2D.Float((intthis.getSize()
                    .getWidth() 
- 6 - 1, (intthis.getSize().getHeight() - 20,
                    
66));
            area.add(
new Area(new Ellipse2D.Float((intthis.getSize()
                    .getWidth() 
- 10 - 30 + 1,
                    (
intthis.getSize().getHeight() - 353015)));
            area.add(
new Area(new Ellipse2D.Float((intthis.getSize()
                    .getWidth()
                    
- contentEllipseX - contentEllipseW + 1, contentEllipseY,
                    contentEllipseW, contentEllipseH)));
            
return area;
        }

        
private Area getRightArea() {
            Area area 
= new Area(new Ellipse2D.Float(0, (intthis.getSize()
                    .getHeight() 
- 2066));
            area.add(
new Area(new Ellipse2D.Float(10, (intthis.getSize()
                    .getHeight() 
- 353015)));
            area.add(
new Area(new Ellipse2D.Float(contentEllipseX,
                    contentEllipseY, contentEllipseW, contentEllipseH)));

            
return area;
        }

        
public void mouseEntered(MouseEvent e) {
            Point p 
= e.getLocationOnScreen();
            SwingUtilities.convertPointFromScreen(p, e.getComponent()
                    .getParent());
            
if (p.x + this.getWidth() > frame.getX()) {
                direction 
= LEFT;
                
this.setLocation(new Point(p.x - this.getWidth(), p.y
                        
- this.getHeight()));
            } 
else {
                direction 
= RIGHT;
                
this.setLocation(new Point(p.x, p.y - this.getHeight()));
            }
            
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) {

        }

    }
}


二、
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.Polygon;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Area;
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;

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

    
public void init() {
        HalfOpaqueToolTip tooltip 
= new HalfOpaqueToolTip(
                
"让我再吻一吻你的脸!"new Color(250250200),
                Color.RED, 
new Color(95145145), 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(new JButton(""));
        
this.add(new JButton(""));
        
this.add(new JButton(""));
        
this.add(new JButton(""));
        
this.add(new JButton(""));
        
this.add(new JButton(""));
        
this.add(new JButton(""));
        
this.add(new JButton(""));
        
this.add(button);

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

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

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

        
private Color foregroundColorCol = null;

        
private Color borderCol = null;

        
private String content = null;

        
private JFrame frame = null;

        
private int LEFT = 1;

        
private int RIGHT = 2;

        
private int direction = 2;

        
public HalfOpaqueToolTip(String tooltipContent, Color backGroundColor,
                Color foregroundColor, Color borderColor, JFrame frame) {
            content 
= tooltipContent;
            backGroundCol 
= backGroundColor;
            foregroundColorCol 
= foregroundColor;
            borderCol 
= borderColor;
            
/*
             * 调整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()
                            
+ 20new StringTokenizer(tooltipContent, "\n")
                            .countTokens() 
* 20 + 20);// 左右各留10的空白空间
            this.setOpaque(false);
            
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);
            AlphaComposite composite 
= AlphaComposite.getInstance(
                    AlphaComposite.SRC_OVER, 
70 / 100.0F);
            g2d.setComposite(composite);
            g2d.fill(getArea());
            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;
            }

        }

        
protected void paintBorder(Graphics g) {
            
super.paintBorder(g);
            Graphics2D g2d 
= (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(borderCol);
            g2d.draw(getArea());
        }

        
private Area getArea() {
            
if (direction == LEFT) {
                
return getLeftArea();
            } 
else {
                
return getRightArea();
            }
        }

        
private Area getLeftArea() {
            Area area 
= new Area(new RoundRectangle2D.Float(00this
                    .getWidth() 
- 1this.getHeight() - 101010));
            Polygon polygon 
= new Polygon();
            polygon.addPoint(
this.getWidth() - 25this.getHeight() - 10);
            polygon.addPoint(
this.getWidth() - 10this.getHeight() - 10);
            polygon.addPoint(
this.getWidth(), this.getHeight());
            area.add(
new Area(polygon));

            
return area;
        }

        
private Area getRightArea() {
            Area area 
= new Area(new RoundRectangle2D.Float(00this
                    .getWidth()
-1this.getHeight() - 101010));
            Polygon polygon 
= new Polygon();
            polygon.addPoint(
25this.getHeight() - 10);
            polygon.addPoint(
10this.getHeight() - 10);
            polygon.addPoint(
0this.getHeight());
            area.add(
new Area(polygon));

            
return area;
        }

        
public void mouseEntered(MouseEvent e) {
            Point p 
= e.getComponent().getLocationOnScreen();
            SwingUtilities.convertPointFromScreen(p, e.getComponent()
                    .getParent());
            
if (p.x + this.getWidth() > frame.getX()) {
                direction 
= LEFT;
                
this.setLocation(new Point(p.x - this.getWidth() + 10, p.y
                        
- this.getHeight() + 10));
            } 
else {
                direction 
= RIGHT;
                
this.setLocation(new Point(p.x + e.getComponent().getWidth(),
                        p.y 
- this.getHeight() + 10));
            }
            
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-14 16:30 azure 阅读(2995) 评论(10)  编辑  收藏

评论

# re: swing components-会话框ToolTip 2007-03-14 17:04 BeanSoft

哥们不去 Sun 公司改进 Swing 真是可惜了! 现在 SWT 凭借简单的拖放和外观正在蚕食 Swing 的市场.  回复  更多评论   

# re: swing components-会话框ToolTip 2007-03-15 00:42 催月泪

真是大开眼界呀!受益无穷!  回复  更多评论   

# re: swing components-会话框ToolTip 2007-03-15 10:13 dianying

受教了,多谢……  回复  更多评论   

# re: swing components-会话框ToolTip 2007-03-17 09:31 cnodin

什么玩意,不就是一段画图的代码嘛。  回复  更多评论   

# re: swing components-会话框ToolTip[未登录] 2007-04-15 02:33 αβγ

受益了。  回复  更多评论   

# re: swing components-会话框ToolTip 2007-05-15 09:30 小飞

代码是不是不全啊?怎么报错啊!
huxf5189@hotmail.com  回复  更多评论   

# re: swing components-会话框ToolTip 2007-07-17 14:23 williamchen

应该使用自定义PopupFactory,这样能对所有的Popup窗口起作用。
http://blog.sina.com.cn/swingjava
我的博客专门写swing高级编程,从原理到源代码下载都有。
或者在google或者百度上搜williamchen
欢迎大家来我的博客讨论切磋。
  回复  更多评论   

# re: swing components-会话框ToolTip 2008-09-04 18:26 日月雨林@gmail.com

真是太漂亮了!  回复  更多评论   

# re: swing components-会话框ToolTip[未登录] 2009-01-09 11:22

谢谢,
不过在显示长字符串时需要换行处理,另外tooltip显示位置还要看与框架边框的距离。  回复  更多评论   

# re: swing components-会话框ToolTip[未登录] 2009-12-02 10:40 qq

不错的效果  回复  更多评论   


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


网站导航: