0

我有一个名为MapTileextends的类JLabel。我正在做的是在一个 GridLayout 中加载一个带有很多 MapTile 的 JPanel。我注意到当我加载 100x100 MapTile 时,该过程比加载 100x100 JLabel 所需的时间更长。这是我MapTile下面的课。

public class MapTile extends JLabel {
private static final long serialVersionUID = 1L;
private boolean selected;
private Point location;

public MapTile(){
    super();
    location = new Point();
}
public void setLocation(int x, int y){
    setLocation(new Point(x,y));
}
public void setLocation(Point p){
    location = p;
}
public Point getLocation(){
    return location;
}
public void setSelected(Boolean b){
    selected = b;
    if (selected){
        ((GridPanel)this.getParent()).addToSelection(this);
        this.setBorder(BorderFactory.createLineBorder(Color.RED));
    } else {
        this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    }
    this.repaint();
}
public boolean isSelected(){
    return selected;
}
}

没什么特别的,我只是重写了 setLocation 方法并添加了一些其他方法。我还只添加了三个实例变量。

扩展一个类会导致一堆额外的重量需要处理吗?

我不禁认为我的MapTile课程非常简单,但加载 100x100 MapTile 与加载 100x100 JLabel 之间的时间差别很大。与 JLabels 相比,大量加载 MapTile 所需的时间大约是 4-5 倍。

4

1 回答 1

2

您不仅覆盖了,而且每次调用该方法时都会setLocation创建一个新实例,这可能会对性能产生影响PointsetLocation(int,int)

除此之外:-您是否测试过,当您删除被覆盖的方法时,您不再会遇到减速-您是否对应用程序进行了分析以查看性能损失发生在哪里

哦,是的,我认为您的覆盖版本不setLocation正确。super.setLocation如果你想JLabel表现得正确,我担心你也必须打电话

于 2012-05-14T16:23:34.973 回答