动态删除JPanel

时间:2012-02-16 18:28:49

标签: java swing

我已经阅读了很多关于这个主题的线索和问题,但似乎无法找到解决问题的可行办法。下面是用于添加和删除组件的方法的代码。

public void addAuxPanel (String pType, int pStart, int pEnd) {

    int id = mPanels.size();
    int xPos = PANEL_START_X;
    int yPos = PANEL_START_Y + (PANEL_HEIGHT * id) + 15;

    if (id == 0) {
        xPos = PANEL_START_X;
    }

    String type = pType;
    int start = pStart;
    int end = pEnd;

    AuxPanel p = new AuxPanel(type, start, end, id, this);
    mPanels.add(p);

    p.setBounds(xPos, yPos, 300, 25);

    getRootPane().add(p);
    getRootPane().repaint();
    getRootPane().revalidate();
}

public void removeAuxPanel (AuxPanel pPanel) {

            getRootPane().remove(pPanel);
            getRootPane().revalidate();
            getRootPane().repaint();

}

2 个答案:

答案 0 :(得分:2)

您可以执行frameObject.remove(yourPanel);

比普通的revalidate()和repaint()。

这里有一个小代码供您使用:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TwoPanels extends JFrame {

    public TwoPanels() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JPanel panel1 = new JPanel();
        panel1.setBackground(Color.RED);
        final JPanel panel2 = new JPanel();
        panel2.setBackground(Color.BLUE);

        JButton button = new JButton("ADD AND REMOVE PANEL");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (panel1.isShowing()) {
                    remove(panel1);
                    add(panel2, BorderLayout.CENTER);
                    revalidate();
                    repaint();
                } else if (panel2.isShowing()) {
                    remove(panel2);
                    add(panel1, BorderLayout.CENTER);
                    revalidate();
                    repaint();
                }
            }
        });

        add(panel1, BorderLayout.CENTER);
        add(button, BorderLayout.PAGE_END);

        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new  Runnable() {
            @Override
            public void run() {
                new TwoPanels();
            }
        });
    }
}

如果您想从JPanel中删除组件,请参阅此代码remove()仍然适用于此:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TwoPanels extends JFrame {

    public TwoPanels() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JPanel panel1 = new JPanel();
        final JLabel label = new JLabel("I am a JLabel");
        panel1.setBackground(Color.WHITE);
        panel1.add(label);

        JButton button = new JButton("ADD AND REMOVE PANEL");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (label.isShowing()) {
                    panel1.remove(label);
                    panel1.revalidate();
                    panel1.repaint();
                } else {
                    panel1.add(label);
                    panel1.revalidate();
                    panel1.repaint();
                }
            }
        });

        add(panel1, BorderLayout.CENTER);
        add(button, BorderLayout.PAGE_END);

        pack();
            setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new  Runnable() {
            @Override
            public void run() {
                new TwoPanels();
            }
        });
    }
}

答案 1 :(得分:1)

如何拨打panel.setVisible (false)