2013-12-08 26 views
1

我所试图做的有,当我将创建一个CButton实例,它将返回我其中有一个自定义的工具提示和图像层一个JButton实例。创建自定义的JButton surppress CustomTooltip

我的应用程序运行完美,没有任何错误,自定义按钮tooltip corectly工作,但图像层不存在的按钮(我的问题是为什么?),因为图像对象参数发送到JButton。

import java.awt.Color; 
import java.awt.Font; 

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

public class CButton extends JButton 
{ 
    CButton(String text, String source) 
    { 
     ImageIcon iconButton = new ImageIcon(source); 
     new JButton(text, iconButton) 
     { 
      public JToolTip createToolTip() 
      { 
       JToolTip toolTip = super.createToolTip(); 
       toolTip.setForeground(Color.BLACK); 
       toolTip.setBackground(Color.WHITE); 
       toolTip.setFont(new Font("Arial", Font.PLAIN, 12)); 
       return toolTip; 
      } 
     }; 
    } 
}; 

回答

3

要定制组件的行为,您应该重写方法,而不是创建要扩展的类的新实例。

喜欢的东西:

public class CButton extends JButton 
{ 
    public CButton(String text, Icon icon) 
    { 
     super(text, icon); 
    } 

    @Override 
    public JToolTip createToolTip() 
    { 
     JToolTip toolTip = super.createToolTip(); 
     toolTip.setForeground(Color.BLACK); 
     toolTip.setBackground(Color.WHITE); 
     toolTip.setFont(new Font("Arial", Font.PLAIN, 12)); 
     return toolTip; 
    } 
}; 
+0

是的,我读到超和我取得了相同的结果。谢谢! – LXSoft