如何保持图标始终活跃,点击它不重新加载。
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);
}
}
创建一个本地变量来存储图标。就像你差点那样:
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);
}
}