如何从 JFrame 中删除 JscrollPane?

How to remove JscrollPane from JFrame?

我在从 jFrame 中删除 JScrollPanel 时遇到问题。

这是点击后添加JScrollPanel的代码:

jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {

                if(count_games == 0)
                    {
                        Game game = new Game();
                        game.setPreferredSize(new Dimension(1000,1000));
                        game1 = new JScrollPane(game);
                        frame.add(game1);

                        game1.setBounds(0, 40, 1000, 960);


                        count_games ++;
                    }else if(count_games == 1)...

这是删除 JScrollPanel 的代码:

jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    if(count_games == 1)
                    {


                        game1.removeAll();
                        game1.revalidate();
                        game1.repaint();

                        count_games --;

                    }else if(count_games == 2)...

删除空线框后保留在 JFrame 中。 例子: Before remove

After remove

game1.removeAll();

这将删除添加到 JScrollPanel 的所有组件。如果你想删除整个面板,你需要从 JFrame.

中删除它

因此将此语句替换为:

frame.remove(game1);

或者如果你想让你的框架为空调用frame.removeAll();方法。