java JLabel不使用If-Else更新
我正在尝试创建jlabel来显示scrabble中商店的购物车。当购物车为空时,应显示“购物车为空”如果不是,则应在列表中列出内容。下面是我用来做这件事的代码
public void reset(){
removeAll();
}
public void update(){
reset();
JLabel cartHeader = new JLabel("<html><br><b>Current Player's Tile Cart</b></html>", JLabel.CENTER);
add(cartHeader);
ArrayList<SpecialTile> tileCart = gameStore.getTileCart();
if(tileCart.size() == 0){
JLabel emptyCart = new JLabel("<html><br><br>Cart is currently empty.</html>", JLabel.CENTER);
add(emptyCart);
}else{
System.out.println("tileCart size: "+tileCart.size());
for(SpecialTile currentTile : tileCart){
JLabel tileLabel = new JLabel("", JLabel.CENTER);
if(currentTile.getClass().equals(BoomTile.class)){
tileLabel.setText("BoomTile - "+exampleBoom.tilePrice());
}else if(currentTile.getClass().equals(ReverseOrder.class)){
tileLabel.setText("ReverseOrderTile - "+exampleReverse.tilePrice());
}else if(currentTile.getClass().equals(NegativePoints.class)){
tileLabel.setText("NegativePointsTile - "+exampleNegative.tilePrice());
}else if(currentTile.getClass().equals(PersonalTile.class)){
tileLabel.setText("ShareTile - "+exampleShare.tilePrice());
}else if(currentTile.getClass().equals(UnknownTile.class)){
tileLabel.setText("UnknownTile - "+exampleUnknown.tilePrice());
}
add(tileLabel);
}
}
JButton purchaseButton = new JButton("Purchase");
add(purchaseButton);
}
我遇到的问题是,它不是用购物车中的不同分幅替换文本,而是总是显示购物车是空的
这基本上就是正在发生的事情:http://i.imgur.com/Rn2gMUM.png
共 (0) 个答案