简体   繁体   中英

How can I add more Jpanels to JFrame?

I have added my all UI components to my JPanel and added that panels to JFrame when a specific event occur from JMenu from Jframe.and my JFrame is having JTollbar also.

however the problem is when ever I trying to add a Panel class Object I need to call removeAll() in order to remove previously added Jpanel .but this method is removing my Jtoolbar also . what should I do for this problem.

You should take a look at layouts and possibly even the card layout . The card layout allows you to stack panels and then decide which one is visible.

使用Swing库提供的布局,例如BorderLayoutGroupLayouthttp://docs.oracle.com/javase/tutorial/uiswing/layout/group.html

As others say use a layout to arrange your panels and may be add a new panel to the view. For example you can use BorderLayout to assign Toolbar to PageStart and the panel to the center of the JFrame. Later you can change the content of the center position of your layout

If the layout of the new panels are totally different card layout may help.

A very simple (may be bad) solution can be to first add a large JPanel to your JFrame as the main content panel. Then add your panels to that and later removeall the components in that main content panel. In that case, your Toolbar is safe.

I will show you short code , I hope you will able to integrate it into your Application..

class MyClass extends JFrame implements ActionListener,ItemListener
{
    JMenuBar menubar1;
    JMenu menu1,menu2;
    JMenuItem item1,item2,item3;
    JCheckBoxMenuItem jcbmi;

    PanelFont pf = new PanelFont();
    PanelShape ps = new PanelShape();
    PanelIcon pi = new PanelIcon();

................

    MyClass()
    {
        menubar1=new JMenuBar();
        menu1=new JMenu("Application");
        menu2=new JMenu("Window");

        item1=new JMenuItem("Font & Color");
        item2=new JMenuItem("Shape");
        item3=new JMenuItem("Image & IconButton");

        jcbmi=new JCheckBoxMenuItem("Resize");

        menu1.add(item1);
        menu1.add(item2);
        menu1.add(item3);
        menu2.add(jcbmi);

        menubar1.add(menu1);
        menubar1.add(menu2);

        setJMenuBar(menubar1);

        getContentPane().add(pf);

        item1.addActionListener(this);
        item2.addActionListener(this);
        item3.addActionListener(this);
        jcbmi.addItemListener(this);

    }

...............

    public void actionPerformed(ActionEvent e)
    {
        Object source =(JMenuItem) e.getSource();

        if(source == item1 )
        {
            System.out.println("Font & Color...");

            pf=new PanelFont();
            getContentPane().removeAll();
            getContentPane().add(pf);
            validate();
        }

        else if(source == item2 )
        {
            System.out.println("Shape...");

            ps =new PanelShape();
            getContentPane().removeAll();
            getContentPane().add(ps);
            validate();     
        }

        else if(source == item3 )
        {
            pi =new PanelIcon();
            getContentPane().removeAll();
            getContentPane().add(pi);

            Image picture = pi.myFrameImage();
            setIconImage(picture);

            validate();
        }

    }


............


}//End of class

class PanelFont extends JPanel implements ItemListener
{

..........

JComboBox fontSize = new JComboBox (size);
JComboBox fontColor = new JComboBox (color);
JComboBox fontFamily;

JCheckBox ckBold = new JCheckBox ("Bold");
JCheckBox ckItalic = new JCheckBox ("Italic");

PanelFont()
    {
        GraphicsEnvironment ge ;
        ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] family = ge.getAvailableFontFamilyNames();
        fontFamily = new JComboBox (family);

        add(fontFamily);        
        add(fontSize);
        add(fontColor);
        add(ckBold);
        add(ckItalic);

}//End of class

//Start of PanelShape
class PanelShape extends JPanel implements ItemListener
{
JComboBox cbShape;
String[] shape1 = {"Rectangle","RoundRectangle","Line","Polygon","Oval","Arc"};
Shape sp = null;

    PanelShape()
    {
        cbShape = new JComboBox(shape1);
        add(cbShape);

............

}//End of PanelShape


//Start of Panel Icon & Image
class PanelIcon extends JPanel
{
Image picture;
ImageIcon btnImage,FrameImage;

JButton btnHello = new JButton("BHUSHAN");
Toolkit tk ;
    PanelIcon()
    {
        tk = Toolkit.getDefaultToolkit();
        picture  = tk.getImage("Image\\JavaLogo1.jpg");

        btnImage  =new ImageIcon("Image\\Icon.jpg");
        btnHello.setIcon(btnImage);

        add(btnHello);
        repaint();
    }
.....

In this example , I used three different Panels & It is also be changed at menu change event occurs...

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