开发者

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

开发者 https://www.devze.com 2023-03-20 18:35 出处:网络
How can i keep the icon always alive, on click it does not get reloaded. public static class SoftButton extends Button

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

精彩评论

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