如何向JFrame添加更多Jpanel?

时间:2013-04-12 16:07:22

标签: java swing

我已将我的所有UI组件添加到我的JPanel,并在特定时将这些面板添加到JFrame 事件来自Jframe的JMenu。我的JFrame也有JTollbar。

然而问题是当我尝试添加一个Panel类对象时,我需要调用removeAll()来删除以前添加的Jpanel。但是这个方法也删除了我的Jtoolbar。 我该怎么办呢。

4 个答案:

答案 0 :(得分:0)

您应该查看layouts,甚至card layout。卡片布局允许您堆叠面板,然后决定哪一个可见。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

正如其他人所说,使用布局来排列面板,可能会在视图中添加一个新面板。 例如,您可以使用BorderLayout将工具栏分配给PageStart,将面板分配到JFrame的中心。稍后您可以更改布局中心位置的内容

如果新面板的布局完全不同,卡布局可能会有所帮助。

一个非常简单(可能不好)的解决方案可以是首先将大型JPanel添加到JFrame作为主要内容面板。然后将面板添加到该面板,稍后删除该主要内容面板中的所有组件。在这种情况下,您的工具栏是安全的。

答案 3 :(得分:0)

我将向您展示简短的代码,我希望您能够将它集成到您​​的应用程序中..

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...
相关问题