【问题标题】:How to have one component on the left and one on the right by FlowLayoutFlowLayout如何让一个组件在左边,一个在右边
【发布时间】:2015-03-30 17:59:20
【问题描述】:

在我的软件的一部分中,我在底部有一个布局,其中包含两个JButtons 和一个JLabel。我想在面板的右侧保留一个按钮,在左侧保留标签。我可以设法将按钮放在右侧,但不知道如何将 JLabel 保留在左侧。

代码如下:

bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        
ftpBack = new JButton("Back");
ftpNext = new JButton("Next");
label = new JLabel("Text);

bottomPanel.add(label);
bottomPanel.add(ftpBack);
bottomPanel.add(ftpNext);

mainPanel.add(bottomPanel, BorderLayout.SOUTH);

这就是我想要实现的目标:

知道怎么做吗?

【问题讨论】:

    标签: java swing layout-manager flowlayout


    【解决方案1】:

    FlowLayout 无法做到这一点。

    你可以使用水平的BoxLayout

    Box box = Box.createHorizontalBox();
    box.add(label);
    box.add(Box.createHorizontalGlue());
    box.add(backButton);
    box.add(Box.createHorizontalStrut(5));
    box.add(nextButton);
    

    阅读 How to Use BoxLayout 上的 Swing 教程部分,了解更多信息和示例。

    或者另一种方法是嵌套布局管理器:

    JPanel main = new JPanel( new BorderLayout() );
    main.add(label, BorderLayout.WEST);
    JPanel buttonPanel= new JPanel();
    buttonPanel.add(back);
    buttonPanel.add(next);
    main.add(buttonPanel, BorderLayout.EAST);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2021-06-17
      相关资源
      最近更新 更多