使用 MiGLayout 设置 JPanel 的大小

发布于 2024-12-19 11:16:05 字数 1572 浏览 1 评论 0原文

我正在使用 MiGLayout 创建几个不同的 JPanel,但是在调整其中一个的大小时遇到​​问题。下图显示了我想要实现的目标:

-------------------------------------------------------
|                                                     |
|                        Panel 1                      |
|                                                     |
-------------------------------------------------------
--------------------------- ---------------------------
|                         | |                         |
|         Panel 2         | |          Panel 3        |
|                         | |                         |
--------------------------- ---------------------------

我想要实现的关键是使面板 1 分布在其他两个面板上。我似乎已经实现了这种布局,但是我遇到了一个问题,即面板 1 的边框没有显示,如图所示。我尝试过使用 setSize 方法,但似乎不起作用。

这是我用来创建面板的代码(请注意,我没有在面板内包含小部件):

    // Create Panel 1
    JPanel panelOne = new JPanel();
    panelone.setSize(600, 50);
    panelOne.setBorder(BorderFactory.createTitledBorder("Panel 1"));
    panelOne.setLayout(new MigLayout());

    // Create Panel 2
    JPanel panelTwo = new JPanel();
    panelTwo.setBorder(BorderFactory.createTitledBorder("Panel 2"));
    panelTwo.setLayout(new MigLayout());

    // Create Panel 3
    JPanel panelThree = new JPanel();
    panelThree.setBorder(BorderFactory.createTitledBorder("Panel 3"));
    panelThree.setLayout(new MigLayout());

    // Add the panels to the main frame
    mainFrame.add(panelOne, "span, wrap, align center");
    mainFrame.add.add(panelTwo);
    mainFrame.add.add(panelThree);

请问有人可以建议一种方法来让边框显示在我的图表中吗?

I'm using the MiGLayout to create several different JPanels, however I'm having a problem resizing one of them. Below is a diagram showing what I want to achieve:

-------------------------------------------------------
|                                                     |
|                        Panel 1                      |
|                                                     |
-------------------------------------------------------
--------------------------- ---------------------------
|                         | |                         |
|         Panel 2         | |          Panel 3        |
|                         | |                         |
--------------------------- ---------------------------

The key thing I want to achieve is to make Panel 1 spread across the two other panels. I seem to have achieved this layout, however I've got an issue such that the border isn't showing up for Panel 1 as shown in my diagram. I have tried using the setSize method but it didn't seem to work.

Here is the code I'm using to create the panels (please note I haven't included the widgets inside the panel):

    // Create Panel 1
    JPanel panelOne = new JPanel();
    panelone.setSize(600, 50);
    panelOne.setBorder(BorderFactory.createTitledBorder("Panel 1"));
    panelOne.setLayout(new MigLayout());

    // Create Panel 2
    JPanel panelTwo = new JPanel();
    panelTwo.setBorder(BorderFactory.createTitledBorder("Panel 2"));
    panelTwo.setLayout(new MigLayout());

    // Create Panel 3
    JPanel panelThree = new JPanel();
    panelThree.setBorder(BorderFactory.createTitledBorder("Panel 3"));
    panelThree.setLayout(new MigLayout());

    // Add the panels to the main frame
    mainFrame.add(panelOne, "span, wrap, align center");
    mainFrame.add.add(panelTwo);
    mainFrame.add.add(panelThree);

Please can someone suggest a way to get the border to be shown as in my diagram?

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

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

发布评论

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

评论(2

奢望 2024-12-26 11:16:05

试试这个:

mainFrame.getContentPane().setLayout(new MigLayout());
getContentPane().add(panelTwo);
getContentPane().add(panelThree,  "wrap");   // Wrap to next row
getContentPane().add(panelOne, "dock north");

另外,确保将 GUI 元素添加到主框架的内容窗格而不是框架本身。

编辑来自以下评论:
您还可以使用 new CC.wrap() 或 new CC.dockNorth()
感谢@Andrew Thompson 和@Howard 的提示:)

Try this:

mainFrame.getContentPane().setLayout(new MigLayout());
getContentPane().add(panelTwo);
getContentPane().add(panelThree,  "wrap");   // Wrap to next row
getContentPane().add(panelOne, "dock north");

Also, ensure you are adding your GUI elements to the main frame's content pane not to the frame itself.

EDIT From the comments below:
You can also use new CC.wrap() or new CC.dockNorth()
Thanks to @Andrew Thompson and @Howard for the tip :)

飘过的浮云 2024-12-26 11:16:05
panelone.setSize(600, 50);  // from original code

我从未使用过该布局,但布局通常更可能尊重(强制)首选尺寸而不是尺寸。

panelone.setSize(600, 50);  // from original code

I've never used that layout, but layouts in general are more likely to respect (enforce) the preferred size than the size.

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