Replacement of a JPanel element without adding to the end of the panel?

拈花ヽ惹草 提交于 2019-12-24 16:02:55

问题


I posted a question earlier about this but the solution never worked and now it's under different circumstances. I'm making a "penny pitch" program, that, when the "confirm" button is pressed, a randomized number will dictate which spot on the board(the board is fill with image icons) the "penny" will fall, and in the process it removes the image icon that use to occupant the chosen space.

I set up a GridBagLayout to constrain each icon down, and my button has no problem removing the chosen spot, but it can not find a way for it to add a new icon in it's place. It just gets adds onto the end of the JPanel.

Heres my coding for the button:

private class AddListener implements ActionListener {
public void actionPerformed(ActionEvent a){
    if (a.getSource()== confirm) {
        if (numberToss >0){
                thrown = pitch.nextInt(25) + 1;
                System.out.println(thrown);
           //kol is an array to check for repeated numbers in randomization
        if (kol.contains(thrown)==false){               
            input.remove(spot.get(thrown));
            //spot is a map to set icons down with a association with number
            spot.put(thrown, bSet);
            input.add((spot.put(thrown, bSet)));
            repaint();
            kol.add(thrown);
            }
        else {
            JOptionPane.showMessageDialog(null, "Your toss landed onto an occupied spot; you receive no points");
            }
        numberToss--;   
        }
        else{
        JOptionPane.showMessageDialog(null, "Out of tosses.");  
        }

      }

    }

Anyone happen to know how to replace the new icon (bSet) with the former? Thanks in advance!

来源:https://stackoverflow.com/questions/36003787/replacement-of-a-jpanel-element-without-adding-to-the-end-of-the-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!