不好意思,刚才没把代码贴出来,代码如下:
import java.awt.*;
import javax.swing.*;public class ToolTipTest extends JFrame {
private MyButton button; //声明
private JPanel panel;

public ToolTipTest() {

button  = new MyButton();
panel = new JPanel();
panel.add(button);

this.getContentPane().add(panel);
ToolTipManager ttm = ToolTipManager.sharedInstance();
ttm.setInitialDelay(10); // 设置工具提示显示的时间
ttm.registerComponent(button); // 注册button,以保证能显示工具提示

//---------设置窗体的基本属性---------------//
this.setSize(400, 300);
this.setLocation(300, 100);
this.setTitle("ToolTip");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//--------------------------------------//
}

public static void main(String[] args) {
ToolTipTest frame = new ToolTipTest();
}
}// 定义自己的按钮类
class MyButton extends JButton { public MyButton() {
super("Button One");

}

public JToolTip createToolTip() {
JToolTip toolTip = new JToolTip();
toolTip.setTipText("我是谁"); // 设置工具提示的显示文字
toolTip.setComponent(MyButton.this); // 这一句不知道有没用,
                                       // 我是看JComponent源码中有这一句
System.out.println(toolTip.getComponent().toString());

return toolTip;
}

}

解决方案 »

  1.   

    在MyButton里增加    public String getToolTipText(MouseEvent event)
        {
            return "新的提示!!!";
        }
      

  2.   

    另:送你一个程序:)class ListTest extends JList
    {
        DefaultListModel model;
        Properties tipProps;    public ListTest(Properties props)
        {
            model = new DefaultListModel();
            setModel(model);
            ToolTipManager.sharedInstance().registerComponent(this);        tipProps = props;
            Enumeration enum = props.propertyNames();
            while(enum.hasMoreElements())
            {
                model.addElement(enum.nextElement());
            }
        }    public String getToolTipText(MouseEvent event)
        {
            Point p = event.getPoint();
            int location = locationToIndex(p);
            String key = (String)model.getElementAt(location);
            String tip = tipProps.getProperty(key);
            return tip;
        }    public static void main(String args[])
        {
            JFrame frame = new JFrame("Custom Tips");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Properties props = System.getProperties();
            ListTest list = new ListTest(props);
            JScrollPane scrollPane = new JScrollPane(list);
            frame.getContentPane().add(scrollPane);
            frame.setSize(300,300);
            frame.show();
        }
    }
      

  3.   

    YuLimin(阿敏总司令:人,是要靠自己的!简单就是美!) 谢谢!但现在我想在JToolTip中加入组件,如JButton,JLabel之类的把图片加上去,但是我直接在JToolTip实例中加入组件后,组件不能显示,请问我该如何做呢?
      

  4.   

    关注,如想 qq上的那样!up