简体   繁体   English

为什么我不能用这种方法从JFrame中删除JPanel?

[英]Why can't i remove a JPanel from the JFrame in this method?

I am using a JPanel subclass called CardPanel to display my images. 我正在使用一个名为CardPanel的JPanel子类来显示我的图像。 In the hitYourself() method I am trying to remove the CardPanel called currentPanel, assign a new instance of CardPanel to the currentPanel field and then add the new CardPanel object to the JFrame. 在hitYourself()方法中,我尝试删除称为currentPanel的CardPanel,将新的CardPanel实例分配给currentPanel字段,然后将新的CardPanel对象添加到JFrame。 For some reason I cant remove the panel from the hitYourself() method or add the new CardPanel. 由于某些原因,我无法从hitYourself()方法中删除面板或添加新的CardPanel。 When i put the code from hitYourself in the play method, it works as expected. 当我将hitYourself中的代码放入play方法时,它可以按预期工作。 I am not sure if its relevent but the hitYourself method is being called through an ActionListener i added to a JButton. 我不确定是否通过添加到JButton的ActionListener调用hitYourself方法的相关性。 I have verified that the ActionListener is working and the method does get called. 我已经验证了ActionListener是否可以正常工作并且该方法确实得到了调用。 Why can't i remove a JPanel from the JFrame in hitYourself()? 为什么我不能从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);

    }

您是否尝试过重新粉刷屏幕?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM