不好意思,刚才没把代码贴出来,代码如下:
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;
}
}
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;
}
}
{
return "新的提示!!!";
}
{
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();
}
}