简体   繁体   English

在Java Swing中使用MigLayout

[英]Using MigLayout in Java Swing

I'm curious as to how I can use MigLayout to evenly distribute components on a grid when placing the components: 我很好奇如何在放置组件时使用MigLayout将组件均匀分布在网格上:

For example, if I were to place FOUR JRadioButtons in a ButtonGroup to span 3 rows and 2 columns (2, 3), and subsequently placed THREE JButtons, the distribution is weighted unequally, I would end up with this: 例如,如果我将四个JRadioButtons放在一个ButtonGroup中以跨越3行和2列(2、3),然后又放置三个JButton,则分配的权重将不相等,我将得出以下结论:

https://i.imgur.com/iGayxn4.png

As seen in the image, A and B are the same size and C holds the remaining space: I would prefer A, B, C to be equal thirds vertically. 如图中所示,A和B的大小相同,而C则保留了剩余空间:我希望A,B,C垂直相等。

If this is possible in GridBagLayout I can utilize that as well, however I run into the same issue with either layout. 如果在GridBagLayout中可以做到这一点,我也可以利用它,但是无论哪种布局我都会遇到相同的问题。

Here lies an example of trying to further implement this in an application 这是一个尝试在应用程序中进一步实现此功能的示例 https://i.imgur.com/b3YrVCF.png

(I'd prefer to have all buttons disregarding the ButtonGroups, the decimal, and equals to be the same size) (我宁愿让所有按钮都忽略ButtonGroups,十进制,并且等于相同的大小)

Much thanks. 非常感谢。

EDIT: (Thanks, to Andrew for the fixes) Here's the example code of adding the buttons using MigLayout: 编辑:(感谢安德鲁的修复程序)这是使用MigLayout添加按钮的示例代码:

    bp.add(buttonGroup1, "grow, span 2 3");
    bp.add(btnQuot );
    bp.add(btnMod);
    bp.add(btnA);
    bp.add(emptyBtn1, "grow");
    bp.add(emptyBtn2, "grow");
    bp.add(emptyBtn3, "grow");
    bp.add(emptyBtn4, "grow");
    bp.add(emptyBtn5, "grow, wrap");
    bp.add(emptyBtn6, "grow");
    bp.add(emptyBtn7, "grow");
    bp.add(btnB, "grow");
    bp.add(btnBackSpace);
    bp.add(btnClrEntr);
    bp.add(btnClear);
    bp.add(btnPlMns);
    bp.add(btnSqrt, "grow,wrap");
    bp.add(emptyBtn8, "grow");
    bp.add(emptyBtn9, "grow");
    bp.add(btnC, "grow");
    bp.add(btn7, "grow");
    bp.add(btn8, "grow");
    bp.add(btn9, "grow");
    bp.add(btnDiv, "grow");
    bp.add(btnPerc, "grow, wrap");
    bp.add(buttonGroup2, "span 2 3");
    bp.add(emptyBtn10, "grow");
    bp.add(emptyBtn11, "grow");
    bp.add(btnD, "grow");
    bp.add(btn4, "grow");
    bp.add(btn5, "grow");
    bp.add(btn6);
    bp.add(btnMult, "grow");
    bp.add(btnOneOverX, "wrap");
    bp.add(emptyBtn12, "grow");
    bp.add(emptyBtn13, "grow");
    bp.add(btnE, "grow");
    bp.add(btn1, "grow");
    bp.add(btn2, "grow");
    bp.add(btn3);
    bp.add(btnSub, "grow");
    bp.add(btnEquals, "grow,wrap, span 1 2");
    bp.add(emptyBtn14, "grow");
    bp.add(emptyBtn15, "grow");
    bp.add(btnF, "grow");
    bp.add(btn0, "grow, span 2");
    bp.add(btnDecimal, "grow");
    bp.add(btnPlus, "grow");

The issue may very well be trying to represent the button groups as a 2x3 cell, so I will attempt the multiple panels when next possible. 问题很可能是试图将按钮组表示为2x3单元格,因此在下一次可能的情况下,我将尝试使用多个面板。

You can do this with MigLayout. 您可以使用MigLayout执行此操作。 Check out the section on "Merging and Splitting Cells" . 请查看“合并和拆分单元格”部分 Also don't be afraid to use multiple JPanels. 另外,不要害怕使用多个JPanel。

Here's a simplified example of what you're trying to do: 这是您要执行的操作的简化示例:

public class MigSample
{   
    public static void main (String [] args)
    {
        JFrame frame = new JFrame();

        // create radio buttons
        JRadioButton radio1 = new JRadioButton("1");
        JRadioButton radio2 = new JRadioButton("2");
        JRadioButton radio3 = new JRadioButton("3");
        JRadioButton radio4 = new JRadioButton("4");

        // create buttons
        JButton buttonA = new JButton("A");
        JButton buttonB = new JButton("B");
        JButton buttonC = new JButton("C");
        JButton buttonD = new JButton("D");
        JButton buttonE = new JButton("=");

        // create the panel that contains the radio buttons
        JPanel radioPanel = new JPanel(new MigLayout("wrap 1")); // wrap 1 let's us avoid adding the wrap constraing to every component
        radioPanel.add(radio1);
        radioPanel.add(radio2);
        radioPanel.add(radio3);
        radioPanel.add(radio4);

        // create the panel that contains the other buttons
        JPanel buttonPanel = new JPanel(new MigLayout("filly, wrap 2")); // filly tells it to take up all the vertical space
        // add growy to all components so they will fit the cell height
        buttonPanel.add(buttonA, "growy");
        buttonPanel.add(buttonD, "growy");
        buttonPanel.add(buttonB, "growy");
        buttonPanel.add(buttonE, "span 1 2, growy"); // span 1 2 tells this component to span 1 column and 2 rows
        buttonPanel.add(buttonC, "growy");

        // create the main contentPane
        JPanel contentPane = new JPanel(new MigLayout("filly")); // again, we want to fill the vertical space so the 2 panels will have the same height
        contentPane.add(radioPanel);
        contentPane.add(buttonPanel, "growy");

        frame.setContentPane(contentPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM