如何保持图标始终处于活动状态,点击它不会重新加载。
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);
}
}
答案 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);
}
}