Java 具有不同轴的嵌套BoxLayout

Java 具有不同轴的嵌套BoxLayout,java,swing,layout-manager,boxlayout,Java,Swing,Layout Manager,Boxlayout,我在使用BoxLayout创建嵌套面板时遇到问题。我有一个布局设置为BoxLayout(PAGE\u AXIS)的容器,在这个容器中,我要生成面板(postedPanel),也要使用BoxLayout(PAGE\u AXIS)。在这个面板中,我有两个面板,分别是PAGE\u轴和LINE\u轴 带有LINE\u轴的面板(postActionspanel)似乎弄乱了带有PAGE\u轴的面板的宽度和对齐方式。当我将postActions的轴设置为PAGE\u axis时,其他面板将拉伸到容器的全宽,但

我在使用
BoxLayout
创建嵌套面板时遇到问题。我有一个布局设置为
BoxLayout
PAGE\u AXIS
)的容器,在这个容器中,我要生成面板(
postedPanel
),也要使用
BoxLayout
PAGE\u AXIS
)。在这个面板中,我有两个面板,分别是
PAGE\u轴
LINE\u轴

带有
LINE\u轴的面板(
postActions
panel)似乎弄乱了带有
PAGE\u轴的面板的宽度和对齐方式。当我将
postActions
的轴设置为
PAGE\u axis
时,其他面板将拉伸到容器的全宽,但当它是
LINE\u axis
时,其他面板将压缩到容器宽度的一半。有没有办法解决这个问题

代码如下:

public void generateFeed(JPanel container) {
    JPanel postedPanel = new JPanel();
    postedPanel.setBackground(Color.WHITE);
    postedPanel.setLayout(new BoxLayout(postedPanel, BoxLayout.PAGE_AXIS));

    // sample post

    JPanel postContent = new JPanel();
    postContent.setLayout(new BoxLayout(postContent, BoxLayout.PAGE_AXIS));
    postContent.setBackground(Color.WHITE);
    JLabel usernameLabel = new JLabel("Name");
    postContent.add(usernameLabel);

    JLabel dateLabel = new JLabel("Date posted");
    postContent.add(dateLabel);

    JLabel status = new JLabel("<html>Content Content Content ContentContent</html>");
    postContent.add(status);

    postedPanel.add(postContent);

    // like, comment panel

    JPanel postActions = new JPanel();
    postActions.setLayout(new BoxLayout(postActions, BoxLayout.LINE_AXIS));
    postActions.setBackground(Color.WHITE);

    JButton likeButton = new JButton("Like");
    postActions.add(likeButton);


    JLabel likesLabel = new JLabel("0 Likes");
    postActions.add(likesLabel);

    JButton commentButton = new JButton("Comment");
    postActions.add(commentButton);


    JLabel commentsLabel = new JLabel("0 Comments");
    postActions.add(commentsLabel);

    postedPanel.add(postActions);

    // sample comment

    JPanel addCommentPanel = new JPanel();
    addCommentPanel.setLayout(new BoxLayout(addCommentPanel, BoxLayout.PAGE_AXIS));
    addCommentPanel.setBackground(new Color(246,247,248));

    JLabel commentUser = new JLabel("Name");
    addCommentPanel.add(commentUser);

    JLabel commentText = new JLabel("<html>Comment Comment Comment Comment Comment Comment </html>");
    addCommentPanel.add(commentText);

    JLabel commentDateLabel = new JLabel("Date");
    addCommentPanel.add(commentDateLabel);

    postedPanel.add(addCommentPanel);


    container.add(postedPanel);

    pack();
}
public void generateFeed(JPanel容器){
JPanel postedPanel=新的JPanel();
后面板。立根地面(颜色。白色);
postedPanel.setLayout(新的BoxLayout(postedPanel,BoxLayout.PAGE_轴));
//样桩
JPanel postContent=新的JPanel();
postContent.setLayout(新的BoxLayout(postContent,BoxLayout.PAGE_轴));
后内容。背景(颜色。白色);
JLabel usernamelab=新的JLabel(“名称”);
添加(用户名标签);
JLabel dateLabel=新JLabel(“发布日期”);
添加(日期标签);
JLabel状态=新的JLabel(“内容”);
添加(状态);
postedPanel.add(postContent);
//比如,评论小组
JPanel postActions=新的JPanel();
postActions.setLayout(新的BoxLayout(postActions,BoxLayout.LINE_轴));
邮递.背景(颜色.白色);
JButton likeButton=新JButton(“Like”);
添加(如按钮);
JLabel likesLabel=新的JLabel(“0个喜欢”);
添加(如标签);
JButton commentButton=新JButton(“Comment”);
添加(评论按钮);
JLabel commentsLabel=新JLabel(“0条评论”);
添加(commentsLabel);
postedPanel.add(postActions);
//示例注释
JPanel addCommentPanel=新的JPanel();
addCommentPanel.setLayout(新的BoxLayout(addCommentPanel,BoxLayout.PAGE_轴));
addCommentPanel.setBackground(新颜色(24624727248));
JLabel commentUser=新JLabel(“名称”);
addCommentPanel.add(commentUser);
JLabel commentText=新JLabel(“注释”);
addCommentPanel.add(commentText);
JLabel commentDateLabel=新JLabel(“日期”);
addCommentPanel.add(commentDateLabel);
postedPanel.add(addCommentPanel);
container.add(postedPanel);
包装();
}
以及显示结果的图像:
阅读上Swing教程的部分


我相信您需要确保所有组件使用相同的
setAlignmentX(…)
值。我认为默认情况下,面板将使用0.5f,而其他组件将使用0.0f。

您是否考虑过玩游戏

BoxLayout.X_AXIS 


,因为这似乎是一种使事物显示出来的可能性。

我将所有面板的对齐设置为左对齐,这就成功了。谢谢如果这有助于解决问题,请告诉我。
BoxLayout.Y_AXIS