ToolTip 对 就是那家伙 丑的要命..一个方块 里面几个字 =.=..... 那不是我的作为!!我要将它帅起来!!
(本来打算明天更新的..被群里一位先生拼命的催啊催啊催....)
射像机! 镜头向我这里看齐!!!!
效果图:

package ToolTip;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JToolTip;



//继承自JToolTip

class CustomToolTip extends JToolTip
{


public void paintComponent(Graphics g)
{

//形状 自己看着办..自己觉的什么漂亮就用什么
Shape round = new RoundRectangle2D.Float(4,4,
this.getWidth( )-1-8,
this.getHeight( )-1-8,
15,15);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.white);
g2.fill(round);


g2.setColor(Color.gray);
g2.setStroke(new BasicStroke(5));
g2.draw(round);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_DEFAULT);


String text = this.getComponent( ).getToolTipText( );

if(text != null)
{
FontMetrics fm = g2.getFontMetrics( );
int h = fm.getAscent( );
g2.setColor(Color.black);
g2.drawString(text,10,(this.getHeight( )+h)/2);
}
}

public Dimension getPreferredSize( )
{
Dimension dim = super.getPreferredSize( );
return new Dimension((int)dim.getWidth( )+20,
(int)dim.getHeight( )+20);
}

public CustomToolTip( )
{
super();
//不用介绍了,
this.setOpaque(false);
}
}
//用到时 就用这里 JButton jb = new CustomJButton ();
//可以自己更改 比如你不想要JButton 你想要JLabel 或者其他的 就 extends JLabel
// 然后 JLabel jl = new CustomJButton(); 当然 名字随便你换不换..(换了比较规范)

public class CustomJButton extends JButton
{

/** *//**
*
*/
private static final long serialVersionUID = 1L;
JToolTip _tooltip;


public CustomJButton(String string, ImageIcon icon)
{
//传入的JButton 字符串和JButton的Icon 如果不需要.可以直接删掉
//或者自己在加一个无参数的方法..
super(string,icon);
_tooltip = new CustomToolTip( );
_tooltip.setComponent(this);
}


public JToolTip createToolTip( )
{
return _tooltip;
}
}

下面写一个Main类来测试一下


import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class ToolTipsHack
{

public static void main(String[] args)
{
JButton button;

JFrame frame = new JFrame("Tool Tips Hack");
BoxLayout layout = new BoxLayout(
frame.getContentPane( ),
BoxLayout.Y_AXIS);
frame.getContentPane( ).setLayout(layout);

button = new CustomJButton("啊啊",null );
button.setText("Open");
button.setToolTipText("相信牌ToolTip");
frame.getContentPane( ).add(button);

button = new CustomJButton("啊啊啊",null);
button.setText("Save");
button.setToolTipText("Save the currently open file");
frame.getContentPane( ).add(button);

frame.getContentPane( ).add(new JLabel("a label"));
frame.getContentPane( ).add(new JLabel("a label"));
frame.getContentPane( ).add(new JLabel("a label"));

frame.pack( );
frame.setVisible(true);//或者直接 frame.show( );
}
}

