简体   繁体   中英

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:

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:

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.

If this is possible in GridBagLayout I can utilize that as well, however I run into the same issue with either layout.

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)

Much thanks.

EDIT: (Thanks, to Andrew for the fixes) Here's the example code of adding the buttons using 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.

You can do this with MigLayout. Check out the section on "Merging and Splitting Cells" . Also don't be afraid to use multiple JPanels.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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