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

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


我要实现的关键是使Panel 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);


请有人可以提出一种方法来使边框显示在我的图中吗?

最佳答案

尝试这个:

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的技巧:)

09-10 10:01
查看更多