在 Java Swing 中使用 MigLayout
Using MigLayout in Java Swing
很好奇放置组件时如何使用MigLayout在网格上均匀分布组件:
例如,如果我在 ButtonGroup 中放置四个 JRadioButtons 以跨越 3 行和 2 列 (2, 3),然后放置三个 JButtons,分配的权重不均,我最终会这样:
如图所示,A 和 B 大小相同,C 占据剩余部分 space:我希望 A、B、C 在垂直方向上相等。
如果这在 GridBagLayout 中可行,我也可以利用它,但是我 运行 遇到了同样的问题。
这是一个尝试在应用程序中进一步实现它的示例
(我希望所有按钮都忽略 ButtonGroups、小数点和等于相同的大小)
非常感谢。
编辑:(感谢 Andrew 的修复)这是使用 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");
问题很可能在于尝试将按钮组表示为 2x3 单元格,因此我将在下次可能时尝试使用多个面板。
您可以使用 MigLayout 完成此操作。查看有关 "Merging and Splitting Cells" 的部分。也不要害怕使用多个 JPanel。
这是您要执行的操作的简化示例:
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);
}
}
很好奇放置组件时如何使用MigLayout在网格上均匀分布组件:
例如,如果我在 ButtonGroup 中放置四个 JRadioButtons 以跨越 3 行和 2 列 (2, 3),然后放置三个 JButtons,分配的权重不均,我最终会这样:
如图所示,A 和 B 大小相同,C 占据剩余部分 space:我希望 A、B、C 在垂直方向上相等。
如果这在 GridBagLayout 中可行,我也可以利用它,但是我 运行 遇到了同样的问题。
这是一个尝试在应用程序中进一步实现它的示例
(我希望所有按钮都忽略 ButtonGroups、小数点和等于相同的大小)
非常感谢。
编辑:(感谢 Andrew 的修复)这是使用 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");
问题很可能在于尝试将按钮组表示为 2x3 单元格,因此我将在下次可能时尝试使用多个面板。
您可以使用 MigLayout 完成此操作。查看有关 "Merging and Splitting Cells" 的部分。也不要害怕使用多个 JPanel。
这是您要执行的操作的简化示例:
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);
}
}