如何在我的AWT按钮()中使图标生动?点击该图标就会消失

时间:2011-07-17 22:14:12

标签: java user-interface awt

如何保持图标始终处于活动状态,点击它不会重新加载。

public static class SoftButton extends Button 
{
    private Image image;

    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);
    }

} 

1 个答案:

答案 0 :(得分:1)

创建一个存储Icon的局部变量。像你几乎一样:

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);
    }

}