我使用名为CardPanel的JPanel子类来显示我的图像。在hitYourself()方法中,我试图删除名为currentPanel的CardPanel,将CardPanel的新实例分配给currentPanel字段,然后将新的CardPanel对象添加到JFrame。出于某种原因,我无法从hitYourself()方法中删除面板或添加新的CardPanel。当我在play方法中放入hitYourself的代码时,它按预期工作。我不确定它是否相关但是hitYourself方法是通过添加到JButton的ActionListener调用的。我已经验证ActionListener正在工作,并且该方法确实被调用。为什么我不能在hitYourself()中从JFrame中删除JPanel?
public void play(){
yourCards.add(deckValues[getAvailableCardIndex()]);
yourCards.add(deckValues[getAvailableCardIndex()]);
dealerCards.add(deckValues[getAvailableCardIndex()]);
dealerCards.add(deckValues[getAvailableCardIndex()]);
currentPanel = new CardPanel(yourCards,dealerCards);
frame.add(currentPanel);
}
public void hitYourself(){
yourCards.add(deckValues[getAvailableCardIndex()]);
frame.remove(currentPanel);
currentPanel = new CardPanel(yourCards,dealerCards);
frame.add(currentPanel);
}
答案 0 :(得分:0)
您是否尝试重新绘制屏幕?