我不明白以下代码的第一行,请你解释一下
JButton b1 = new JButton("Hello, World") {
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setBackground(Color.YELLOW);
tip.setForeground(Color.RED);
return tip;
}
};
答案 0 :(得分:1)
代码使用覆盖的createToolTip()
方法创建JButton的新实例。此方法通常在JComponent
类中定义,什么是JButton
的超类。
首先调用super.createToolTip()
调用JComponent
此方法,然后将工具提示的前景色和背景色更改为特定于JButton
实例的内容。< / p>
另一种方法是创建一个扩展MyNewButtonWithSpecialToolTip
的新类JButton
,并仅覆盖createToolTip()
方法。但是,如果只有一个具有此特殊功能的按钮,则需要更多代码和一个额外的类文件。