繁体   English   中英

如何向JFrame添加更多Jpanels?

[英]How can I add more Jpanels to JFrame?

我已将所有UI组件添加到JPanel中,并在Jframe的JMenu中发生特定事件时将该面板添加到JFrame中,并且JFrame也具有JTollbar。

但是问题是,当我尝试添加Panel类对象时,我需要调用removeAll()才能删除以前添加的Jpanel。但是此方法也要删除我的Jtoolbar。 我该怎么办?

您应该看一下布局 ,甚至看一下卡片布局 卡布局允许您堆叠面板,然后确定哪一个是可见的。

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

就像其他人所说的那样,使用布局来布置面板,并且可以向视图中添加新面板。 例如,您可以使用BorderLayout将Toolbar分配给PageStart,将面板分配给JFrame的中心。 稍后您可以更改布局中心位置的内容

如果新面板的布局完全不同,则可以使用不同的卡布局。

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

我将向您展示短代码,希望您能够将其集成到您的应用程序中。

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...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM