Java Swing:FlowLayout JPanel 彼此相邻?

发布于 2024-10-03 21:45:28 字数 324 浏览 0 评论 0原文

我在 JFrame 上绘制了三个 JPanel。目前这些都设置为使用默认的 FlowLayout。我希望这些在一个列中一个在另一个之上。

然而,我发现它们在同一条线上彼此相邻浮动,只要它们内部的组件一致。

FlowLayout JPanel 的自然宽度是其内容的总和吗?如果是这样,有什么方法可以强制该区域的宽度为 JFrame 的宽度吗?

有趣的是,我发现,如果“顶部”和“底部”面板的内容跨越 JFrame 的整个宽度,并且“中间”面板留空,则“中间”面板会在两个面板之间创建一个空间,很像旧的 html 的“

”。

谢谢,

Ben

I have three JPanels painted onto a JFrame. These are currently all set to use the default FlowLayout. I would like for these to be one above the other in a single column.

However, I am finding that these are floating next to each other on the same line, so long as the components within them.

Is the natural width of a FlowLayout JPanel the sum of its contents? If so, is there any way to force the width of the region to be the width of the JFrame?

Interestingly, I am finding that, if the "top" and "bottom" panels have content which spans the entire width of the JFrame, and the "middle" panel is left empty, that the "middle" panel creates a space between the two, much like the old "

of html.

Thanks,

Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枫以 2024-10-10 21:45:28

正如 Jim 所说,如果您需要线性对齐组件,BoxLayout 是正确的选择。

这是一个示例:

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author nicholasdunn
 */
public class BoxLayoutExample extends JPanel {

    public BoxLayoutExample() {
        JPanel topPanel = new JPanel();
        JPanel middlePanel = new JPanel();
        JPanel bottomPanel = new JPanel();

        topPanel.setBorder(BorderFactory.createEtchedBorder());
        middlePanel.setBorder(BorderFactory.createEtchedBorder());
        bottomPanel.setBorder(BorderFactory.createEtchedBorder());

        topPanel.add(new JLabel("Top"));
        middlePanel.add(new JLabel("Middle"));
        bottomPanel.add(new JLabel("Bottom"));

        BoxLayout boxLayout = new BoxLayout(this, BoxLayout.PAGE_AXIS);
        setLayout(boxLayout);
        add(topPanel);
        add(middlePanel);
        add(bottomPanel);

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new BoxLayoutExample();
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

alt text

您最好仔细阅读 布局管理器简介,了解 LayoutManager 的基本集。当需要进行复杂布局时,请使用 MigLayout 而不是尝试学习 GridBagLayout - 你会感谢我。

As Jim stated, BoxLayout is the correct choice if you need to linearly align components.

Here is an example:

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author nicholasdunn
 */
public class BoxLayoutExample extends JPanel {

    public BoxLayoutExample() {
        JPanel topPanel = new JPanel();
        JPanel middlePanel = new JPanel();
        JPanel bottomPanel = new JPanel();

        topPanel.setBorder(BorderFactory.createEtchedBorder());
        middlePanel.setBorder(BorderFactory.createEtchedBorder());
        bottomPanel.setBorder(BorderFactory.createEtchedBorder());

        topPanel.add(new JLabel("Top"));
        middlePanel.add(new JLabel("Middle"));
        bottomPanel.add(new JLabel("Bottom"));

        BoxLayout boxLayout = new BoxLayout(this, BoxLayout.PAGE_AXIS);
        setLayout(boxLayout);
        add(topPanel);
        add(middlePanel);
        add(bottomPanel);

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new BoxLayoutExample();
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

alt text

You would do well to really read through the introduction to layout managers to understand the base set of LayoutManagers. When it comes time to do complex layouts, please use MigLayout instead of trying to learn the GridBagLayout - you will thank me.

栀子花开つ 2024-10-10 21:45:28

如果您希望创建垂直布局,您可能需要考虑使用 BoxLayout 作为封闭容器。可以将其设置为沿 y 轴布局组件。

If you wish to create a vertical layout you may want to look into using BoxLayout for the enclosing container. This can be set to layout components along the y axis.

套路撩心 2024-10-10 21:45:28

您可以使用 GridLayout 在组件中创建 YxZ 网格。要更精确地构建布局,您可以使用 GridBagLayout ,它提供了对嵌套组件的定位和大小调整的完全控制。

You can use GridLayout to create YxZ grid in a component. To construct layout more precisely you can use GridBagLayout which provides the full control of positioning and sizing for nested components.

糖果控 2024-10-10 21:45:28

我总是使用 NetBeans。因为,我可以进行自由设计,而不会在将组件放置在 JPanel 或 JFrame 上时遇到任何麻烦:) 您可以考虑一下。

I am always go with NetBeans. Since, I can make Free Design with out having any trouble of placing the components on the JPanel or JFrame :) You may consider it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文