开发者

Java JFrame refuses to refresh

I am making a monopoly game in Java and for some reason I cant seem to get this JFrame popup to refresh. When I click the button, the popup will appear, but when I close it and click it again later on it the game, it continues to show the features of the 1st JFrame. I assume that a totally new JFrame is created every time you click the button, but I cant seem to refresh the content. How do I fix this?

final static JButton tradeButton = new JButton("Click to Trade Property");
tradeButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                final JPanel tradePanelcombo = new JPanel();
                for(int x = 1; x< Player.Props.size(); x++)
                {
                    if(x != Player.CurrentPlayer)
                    {
                        ArrayList<Property> props = Player.Props.get(x);
                        ArrayList<String> propnames = new ArrayList<String>();
                        for(P开发者_Go百科roperty prop : props)
                        {
                            propnames.add(prop.getName());
                        }
                        String[] strings = new String[propnames.size()];
                        for (int y = 0; y<propnames.size(); y++)
                        {
                            strings[y] = propnames.get(y);
                        }
                        JLabel confirmLabel = new JLabel("Player :" + x);
                        JComboBox petList = new JComboBox(strings);
                        petList.setPreferredSize(new Dimension(100, 150));

                        tradeFrame.add(confirmLabel);
                        tradePanelcombo.add(petList);
                    }
                }
                tradeFrame.add(tradePanelcombo, BorderLayout.PAGE_END);
                tradeFrame.setPreferredSize(new Dimension(500,500));

                tradeFrame.setResizable(true);
                tradeFrame.pack();
                tradeFrame.setVisible(true);
            }
        });


It appears to me that a new frame and components are created every time.

So, if the frame doesn't update, then I guess it would be because your Properties are not updated so the data added to the components doesn't change.

Edit:

You need to create a new JFrame every time, or you need to remove all the old components from the frame before adding the new components.

When you add a component to a BorderLayout, the previous component doesn't get removed. So you actually have two components in the container. When Swing paints components it actually paints the components in the reverse order that they where added to the container (this is how Z-Ordering works).

If you remove the setResizable() method, and then resize the frame I believe you should see the new components start to show given that they should increase in size as the frame size increases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜