0

すべての UI コンポーネントを JPanel に追加し、Jframe の JMenu から特定のイベントが発生したときにそのパネルを JFrame に追加しました。JFrame には JTollbar もあります。

ただし、問題は、Panel クラス オブジェクトを追加しようとするたびに、以前に追加した Jpanel を削除するために removeAll() を呼び出す必要があることですが、このメソッドは Jtoolbar も削除しています。この問題はどうすればいいですか。

4

4 に答える 4

0

レイアウトと、場合によってはカード レイアウトを確認する必要があります。カード レイアウトを使用すると、パネルを積み重ねてから、表示するパネルを決定できます。

于 2013-04-12T16:11:42.533 に答える
0

他の人が言うように、レイアウトを使用してパネルを配置し、新しいパネルをビューに追加する場合があります。たとえば、BorderLayout を使用してツールバーを PageStart に割り当て、パネルを JFrame の中央に割り当てることができます。後でレイアウトの中心位置の内容を変更できます

新しいパネルのレイアウトがまったく異なる場合、カード レイアウトが役立つ場合があります。

非常に単純な (悪いかもしれない) 解決策は、最初に大きな JPanel をメイン コンテンツ パネルとして JFrame に追加することです。次に、パネルを追加し、後でそのメイン コンテンツ パネルのすべてのコンポーネントを削除します。その場合、ツールバーは安全です。

于 2013-04-12T16:56:22.943 に答える
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...
于 2013-04-12T18:08:27.777 に答える