防止JPanel重复添加ImageIcon
Prevent JPanel repeatedly adding ImageIcon
我在空闲时间创建一个国际象棋游戏,在用户执行一个动作(即移动棋子)后,我更新 window(JFrame) 以显示新的棋子位置。但是,在我的更新函数中,我使用 add(Component) 函数将 JLabel 添加到 JPanel。因此,每次更新时,都会将多个 JLabel 添加到组件中,因为 add() 函数会堆叠 JLabel。
BufferedImage img = null;
try{
img = ImageIO.read(getClass().getResource(theTiles.get(i).getPiece().getImagePath()));
}catch(IOException e){
e.printStackTrace();
}
ImageIcon icon = new ImageIcon(img);
JLabel label = new JLabel();
label.setIcon(icon);
//Here is where the error is:
theTiles.get(i).add(label);
label.repaint();
this.window.validate();
this.window.repaint();
因为每当有更新时都会调用此函数,所以 "theTiles.get(i).add(label)" 每次调用时都会向 JPanel 添加多个 JLabel。我试图将一个唯一的 JLabel 设置为 class 的私有变量,这样它就可以替换 JLabel 而不是在需要更新时添加更多,例如:
public class TilePanel extends JPanel{
//JLabel variable
private JLabel someLabel = new JLabel();
TilePanel(){
//Add the component to the Panel
this.add(someLabel);
}
public Jlabel setLabel(JPanel newLabel){
//setLabel function to use in the "update" function
this.someLabel = newLabel
}
...
//Use setLabel instead of add(Component)
theTiles.get(i).setLabel(label);
但是,这样做会导致没有图像出现。我哪里错了?
(注意:这是我第一次使用 GUI)
感谢 MadProgrammer 的提示;这是我找到的解决方案:
在我的更新函数中,我只是调用:
theTiles.get(i).setImage();
并且在我的 class 中我有以下内容:
public class TilePanel extends JPanel{
//A JLabel for each tile
private JLabel theLabel = new JLabel();
TilePanel(int i, int j){
//constructor add the Label to itself
this.add(theLabel);
//A function to "setIcon" instead of using .add() multiple times
public void setImage(){
//assign in an icon
if (this.pieceAtTile != null){
BufferedImage img = null;
try{
img = ImageIO.read(getClass().getResource(this.pieceAtTile.getImagePath()));
}catch(IOException e){
e.printStackTrace();
}
ImageIcon icon = new ImageIcon(img);
this.theLabel.setIcon(icon);
}
else{this.theLabel.setIcon(null);}
theLabel.repaint();
}
我在空闲时间创建一个国际象棋游戏,在用户执行一个动作(即移动棋子)后,我更新 window(JFrame) 以显示新的棋子位置。但是,在我的更新函数中,我使用 add(Component) 函数将 JLabel 添加到 JPanel。因此,每次更新时,都会将多个 JLabel 添加到组件中,因为 add() 函数会堆叠 JLabel。
BufferedImage img = null;
try{
img = ImageIO.read(getClass().getResource(theTiles.get(i).getPiece().getImagePath()));
}catch(IOException e){
e.printStackTrace();
}
ImageIcon icon = new ImageIcon(img);
JLabel label = new JLabel();
label.setIcon(icon);
//Here is where the error is:
theTiles.get(i).add(label);
label.repaint();
this.window.validate();
this.window.repaint();
因为每当有更新时都会调用此函数,所以 "theTiles.get(i).add(label)" 每次调用时都会向 JPanel 添加多个 JLabel。我试图将一个唯一的 JLabel 设置为 class 的私有变量,这样它就可以替换 JLabel 而不是在需要更新时添加更多,例如:
public class TilePanel extends JPanel{
//JLabel variable
private JLabel someLabel = new JLabel();
TilePanel(){
//Add the component to the Panel
this.add(someLabel);
}
public Jlabel setLabel(JPanel newLabel){
//setLabel function to use in the "update" function
this.someLabel = newLabel
}
...
//Use setLabel instead of add(Component)
theTiles.get(i).setLabel(label);
但是,这样做会导致没有图像出现。我哪里错了? (注意:这是我第一次使用 GUI)
感谢 MadProgrammer 的提示;这是我找到的解决方案:
在我的更新函数中,我只是调用:
theTiles.get(i).setImage();
并且在我的 class 中我有以下内容:
public class TilePanel extends JPanel{
//A JLabel for each tile
private JLabel theLabel = new JLabel();
TilePanel(int i, int j){
//constructor add the Label to itself
this.add(theLabel);
//A function to "setIcon" instead of using .add() multiple times
public void setImage(){
//assign in an icon
if (this.pieceAtTile != null){
BufferedImage img = null;
try{
img = ImageIO.read(getClass().getResource(this.pieceAtTile.getImagePath()));
}catch(IOException e){
e.printStackTrace();
}
ImageIcon icon = new ImageIcon(img);
this.theLabel.setIcon(icon);
}
else{this.theLabel.setIcon(null);}
theLabel.repaint();
}