开发者

How can make the icon alive in my AWT button()? On click the icon goes away

How can i keep the icon always alive, on click it does not get reloaded.

public static class SoftButton extends Button 
{
    private Image image;开发者_JAVA技巧

    public SoftButton() 
    {            
        setLabel("test");
        setBackground(Color.red);
    }

    public void paint(Graphics g) 
    {  
        super.paint(g);
        image = Toolkit.getDefaultToolkit().getImage("/tmp/world.gif");
        g.drawImage(image, 0, 0, this);
    }

} 


Create a local variable in which you store the Icon. Like you almost did:

public static class SoftButton extends Button 
{
    private Image image;

    public SoftButton() 
    {            
        setLabel("test");
        setBackground(Color.red);
        // Load the icon once in the constructor:
        image = Toolkit.getDefaultToolkit().getImage("/tmp/world.gif");
    }

    public void paint(Graphics g) 
    {  
        super.paint(g);
        g.drawImage(image, 0, 0, this);
    }

} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜