如何将工具提示添加到JButton

时间:2017-06-28 15:45:34

标签: java swing

我不明白以下代码的第一行,请你解释一下

JButton b1 = new JButton("Hello, World") {
public JToolTip createToolTip() {
     JToolTip tip = super.createToolTip();
     tip.setBackground(Color.YELLOW);
     tip.setForeground(Color.RED);
     return tip;
  } 
};

1 个答案:

答案 0 :(得分:1)

代码使用覆盖的createToolTip()方法创建JButton的新实例。此方法通常在JComponent类中定义,什么是JButton的超类。

首先调用super.createToolTip()调用JComponent此方法,然后将工具提示的前景色和背景色更改为特定于JButton实例的内容。< / p>

另一种方法是创建一个扩展MyNewButtonWithSpecialToolTip的新类JButton,并仅覆盖createToolTip()方法。但是,如果只有一个具有此特殊功能的按钮,则需要更多代码和一个额外的类文件。