Swing JPanel的JFrame具有不同的布局

Swing JPanel的JFrame具有不同的布局,swing,user-interface,jframe,jpanel,layout-manager,Swing,User Interface,Jframe,Jpanel,Layout Manager,我想知道一个JPanel是否可以有一个不同于其父JFrame的布局。例如如果我有带边框布局的JFrame,我们有一个内嵌的JPanel,它有不同的布局。可能吗 我正试着去做。但这样,JPanel的组件就不会显示出来 具体问题如下: 我有一个JFrame和布局,因为它是边框布局。我在这个框架上添加了一个JPanel。如果我没有为JPanel设置任何布局。JPanel的所有组件都显示在窗口上,但当我为JPanel设置网格布局时,JPanel的组件不可见。我正在向JPanel添加布局,以便对齐组件。下

我想知道一个JPanel是否可以有一个不同于其父JFrame的布局。例如如果我有带边框布局的JFrame,我们有一个内嵌的JPanel,它有不同的布局。可能吗

我正试着去做。但这样,JPanel的组件就不会显示出来

具体问题如下:

我有一个JFrame和布局,因为它是边框布局。我在这个框架上添加了一个JPanel。如果我没有为JPanel设置任何布局。JPanel的所有组件都显示在窗口上,但当我为JPanel设置网格布局时,JPanel的组件不可见。我正在向JPanel添加布局,以便对齐组件。下面是我的代码:

我有一个main类、一个frame类和一个Jpanel类

    public class AppMain {

    public static void main(String[] args) {

        AppPage1 page1 = new AppPage1("test");
        page1.setVisible(true);
    }
}



public class AppPage1 extends JFrame {

    public AppPage1(String title) throws HeadlessException {

        super(title);
        this.setLayout(new BorderLayout());

        addWindowListener(new WindowAdapter() {

            public void windowOpened(WindowEvent e) {
                setExtendedState(MAXIMIZED_BOTH);
            }
        });

        //Panel for logo
        JLabel testLogo = new JLabel("");
        testLogo.setIcon(new javax.swing.ImageIcon("test.JPG"));
        List<JComponent> componentList = new ArrayList<JComponent>();
        componentList.add(testLogo);


        PagePanel logoPanel = new PagePanel(componentList,null);
        this.add(logoPanel, BorderLayout.NORTH);


        //Panel for Button and checkboxes
        JLabel panelTitle = new JLabel("test Wizard");
        JRadioButton rdButton_ExistingConfigurationFile = new JRadioButton("Existing Configuration File");
        JRadioButton rdButton_ConfigureNewPropertyFile = new JRadioButton("Configure new Property File");

        componentList = new ArrayList<JComponent>();
        componentList.add(panelTitle);
        componentList.add(rdButton_ExistingConfigurationFile);
        componentList.add(rdButton_ConfigureNewPropertyFile);

        PagePanel buttonPanel = new PagePanel(componentList,new GroupLayout(this));
        this.add(buttonPanel, BorderLayout.CENTER);

        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        validate();
    }
}



    public class PagePanel extends JPanel {

    public PagePanel(List<JComponent> componentList, LayoutManager layOutManager) {

        this.setBackground(Color.decode("#4E6987"));

        if (layOutManager != null) {
            this.setLayout(null);
        }
        for (JComponent jComponent : componentList) {

            jComponent.setBackground(Color.decode("#4E6987"));
            this.add(jComponent);
        }
    }
}
公共类AppMain{
公共静态void main(字符串[]args){
AppPage1 page1=新的AppPage1(“测试”);
第1页设置可见(真);
}
}
公共类AppPage1扩展了JFrame{
公共AppPage1(字符串标题)抛出HeadlessException{
超级(标题);
此.setLayout(新的BorderLayout());
addWindowListener(新的WindowAdapter(){
公共无效窗口已打开(WindowEvent e){
setExtendedState(最大化两个);
}
});
//标志板
JLabel testLogo=新的JLabel(“”);
setIcon(新的javax.swing.ImageIcon(“test.JPG”);
List componentList=新的ArrayList();
添加(testLogo);
PagePanel logoPanel=新的PagePanel(组件列表,空);
添加(标识面板,BorderLayout.NORTH);
//按钮和复选框的面板
JLabel panelTitle=新的JLabel(“测试向导”);
JRadioButton rdButton_ExistingConfigurationFile=新的JRadioButton(“现有配置文件”);
JRadioButton rdButton_ConfigureNewPropertyFile=新JRadioButton(“配置新属性文件”);
componentList=新的ArrayList();
组件列表。添加(面板标题);
添加(rdButton\u现有配置文件);
添加(rdButton\u配置NewPropertyFile);
PagePanel buttonPanel=新的PagePanel(组件列表,新的组布局(此));
添加(buttonPanel,BorderLayout.CENTER);
这个包();
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
验证();
}
}
公共类PagePanel扩展了JPanel{
公共页面面板(列表组件列表、布局管理器布局管理器){
此.setBackground(彩色解码(#4E6987”);
if(layOutManager!=null){
此.setLayout(null);
}
for(JComponent JComponent:componentList){
jComponent.setBackground(彩色解码(#4E6987”);
this.add(jComponent);
}
}
}
提前谢谢
拉维·库马尔(Ravi Kumar)

我可以写一个例子来证明这是完全可能的,但是已经有了一个很好的“嵌套布局”的例子,所以就足够了。

问题是您在
页面面板
类中设置了一个空布局管理器,而不是设置布局管理器

if (layOutManager != null) {
    this.setLayout(null);
}
如果设置
null
布局,则必须“手动”定位和调整组件的大小,因此尽量避免这种情况,并正确使用

这一行也没有意义:

PagePanel buttonPanel = new PagePanel(componentList,new GroupLayout(this));

GroupLayout构造函数的参数应该是设置布局的容器,而不是随机组件。

是,这是可能的。虽然为什么它不会发生在你这一边,但为此你需要准确地展示你在做什么!!这将有助于任何人回答您的问题,因此您这边的一些代码将非常感谢。默认情况下,
JFrame有BorderLayout
,而
JPanel有FLowLayout
,因此不需要做任何事情,只需在
JPanel
上添加组件,并将其添加到
JFrame
中,而不需要任何其他行,您将得到“是”作为对您问题的回答:-)当然,您可以为层次结构的每个组件使用不同的布局管理器。默认情况下,JPanel使用FlowLayout。如果遇到麻烦,请发布一条消息以寻求更好的帮助。哎呀!。。我没注意到。这是我犯的最愚蠢的错误之一。谢谢你纠正它。