简体   繁体   中英

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. 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. For some reason I cant remove the panel from the hitYourself() method or add the new CardPanel. When i put the code from hitYourself in the play method, it works as expected. I am not sure if its relevent but the hitYourself method is being called through an ActionListener i added to a JButton. I have verified that the ActionListener is working and the method does get called. Why can't i remove a JPanel from the JFrame in hitYourself()?

    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);

    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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