嵌套Jpanel over Jframe中的问题

时间:2011-09-09 07:29:47

标签: java swing jframe jpanel

我有一个JFrame和一个Jpanel,其中various buttons被放置。所以点击一个按钮我叫{{1}这是new class。所以我想also having containers placed in a Jpanel show超过new class panel。我怎么能这样做?

如果我们在其中使用卡片布局,那么我如何使用它作为点击按钮我已经调用了一个新类的对象。 如

main Jframe panel

我们可以在其中嵌套Jpanels吗?

请建议我这样做的正确方法?

这是SSCCE:

Card layout consider each component in a container as card and i want  whole Jpanel as a card so is it possible to do that???

}

// this is the  main class on which i want to use panel of other class
public class mymain 
{
    JFrame jframe =  new JFrame();
    JPanel panel = new JPanel();
    BorderLayout borderlayout = new BorderLayout();

public mymain()
{
    jframe.setLayout(borderlayout);
    JMenuBar menubar = new JMenuBar();
    jframe.setJMenuBar(menubar);

    JButton home_button = new JButton("HOME");
    menubar.add(home_button);
    jframe.getContentPane().add(panel,BorderLayout.CENTER);             
    panel.setLayout(new GridBagLayout());

    //here used containers over that frame              
             and call it from main()

}

所以现在我想要here is another class to manage category is public class manageCategory { JPanel panel = new JPanel(); GridBagLayout gridbglayout = new GridBagLayout(); GridBagConstraints gridbgconstraint = new GridBagConstraints(); public manageCategory() { panel.setLayout(new BorderLayout()); // i have again here used containers placed with grid bag layout } 类中使用click on home buttonmymain panel中使用的manageCategory()应该在同一个面板上displayed 。当我再次点击home按钮时,mymain panel显示出来。我可以这样做吗?

3 个答案:

答案 0 :(得分:4)

我建议您使用CardLayout执行此任务。


更新了JPanel和“类”的示例:

static class MainPanel extends JPanel {
    public MainPanel(final Container frame) {
        add(new JButton(new AbstractAction("Click to view next") {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.add(new NextPanel(), "NextPanel");
                ((CardLayout) frame.getLayout()).show(frame, "NextPanel");
            }
        }));
    }
}

static class NextPanel extends JPanel {
    public NextPanel() {
        add(new JLabel("Next page in the card layout"));
    }
}

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame("Test");
    frame.setLayout(new CardLayout());
    frame.add(new MainPanel(frame.getContentPane()), "MainPanel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
}

答案 1 :(得分:1)

你可以

 JFrame myFrame = new JFrame();
 JPanel panel1 = new JPanel();
 Panel1.setVisible(true);
 myFrame.add(panel1);


 JPanel panel2 = new JPanel();
 Panel2.setVisible(false);
 myFrame.add(panel2);

 //Here you setup your panels and your actionlisteners etc and when
 //you wish for your second panel to show up just run the code below.

 panel1.setVisible(false);
 panel2.setVisible(true);

显然,您首先必须将两个面板添加到Jframe中。 Panel1将首先显示,因为它是默认显示的那个。必须将Panel2设置为在开头不可见。

答案 2 :(得分:1)

CardLayout是可能的方法之一,但大多数已完成的GUI

还有其他选项有效或需要

1)BorderLayout,因为只有一个JComponent可以占用决策区域

someContainer.add(myPanel, BorderLayout.CENTER) 
revalidate();
repaint();

2)GridBagLayout

在你必须从my GridBagConstraints

的myOldComponent声明GridBagLayout之前
myContainer.setVisible(myOldComponent);
//or
myContainer.remove(myOldComponent);

myContainer.add(myNewComponent, gbc);
revalidate();
repaint();