Java:MigLayout Center Center无法正常工作绝对定位的替代方法

时间:2018-08-03 00:25:36

标签: java swing user-interface miglayout

该元素不会垂直居中,我也不知道为什么。我已经尝试过“中心中心”,但是它不起作用。 vertical problem with components

MigLayout

panel.add(header, "span");
panel.add(input, "split 4");
panel.add(unit1);
panel.add(to);
panel.add(unit2, "wrap");
panel.add(convert, "span");
panel.add(output, "span");

组件

chdir()

我想使组件居中,但使用绝对定位(因为我知道如何使用此方法来居中)

1 个答案:

答案 0 :(得分:2)

TL; DR

可耻的是你

  • center center不是center, center
  • center, center, filly-filly减轻了垂直中心
  • 要对齐各个组件,您需要align center-例如panel.add(new JLabel("Header"), "span, align center");

假设您要询问如何水平对齐组件,则需要为每个组件指定该对齐方式。但是,首先,最好看看下面的代码行实际上是做什么的。

private JPanel panel = new JPanel(new MigLayout("center center, filly"));

或更具体地说,以下摘录是什么意思。

new MigLayout("center center, filly")

center center-将JPanel上的打包组件水平对齐。

enter image description here

假设您要使组件垂直和水平居中,则实际上需要center, center。注意两个,关键字之间的center

enter image description here

filly-引用白皮书“声明容器中用于列和/或行的所有可用空间”

这几乎减轻了“居中,居中”的影响,您真正需要的只是center, filly


现在要使组件在多列中水平对齐,您需要在要执行此操作的每个组件上添加关键字align center。例如

panel.add(new JLabel("Header"), "span, align center");

与您的组件有关的示例是

String[] dummyList  = new String[] {
    "Val 1",
    "Val 2"
};
JPanel migPanel = new JPanel(new MigLayout("center, filly")); //Maybe you wanted center, center. I'm not sure
migPanel.add(new JLabel("Header"), "span, align center");
migPanel.add(new JTextField("Input"), "split 4");
migPanel.add(new JComboBox<String>(dummyList));
migPanel.add(new JLabel("To"), "span");
migPanel.add(new JComboBox<String>(dummyList), "wrap");
migPanel.add(new JButton("Convert"), "span, align center");
migPanel.add(new JTextField("Output"), "span, align center");

MadProgrammer所述,还有其他布局可用于实现与您所拥有的外观非常相似的外观; GridBagLayout是最简单的。

使用GridBagConstraints的混合形式,您可以获得非常相似的布局。

例如,

String[] dummyList  = new String[] {
    "Val 1",
    "Val 2"
};
JPanel gridBagPanel = new JPanel(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();

gc.weighty = 1; //Delete this line if you are after an equivalent of `center, center` from the MigLayout
gc.gridwidth = 4;
gridBagPanel.add(new JLabel("Header"), gc);

gc.gridy = 1;
gc.gridwidth = 1;
gridBagPanel.add(new JTextField("Input"), gc);

gc.gridx = 1;
gridBagPanel.add(new JComboBox<String>(dummyList), gc);

gc.gridx = 2;
gridBagPanel.add(new JLabel("To"), gc);

gc.gridx = 3;
gridBagPanel.add(new JComboBox<String>(dummyList), gc);

gc.gridy = 2;
gc.gridx = 0;
gc.gridwidth = 4;
gridBagPanel.add(new JButton("Convert"), gc);

gc.gridy = 3;
gridBagPanel.add(new JTextField("Output"), gc);

可运行示例- LayoutExample.java

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
public class LayoutExample extends JFrame {
    private JTabbedPane tabs;
    private JPanel migPanel;
    private JPanel gridBagPanel;
    private JPanel migPanelCC;
    private JPanel gridBagPanelCC;
    private String[] dummyList  = new String[] {
        "Val 1",
        "Val 2"
    };

    private LayoutExample() {
        super("Layout Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setUpTabs();
        add(tabs);

        setSize(800, 500);
        setVisible(true);
    }

    private void setUpTabs() {
        tabs = new JTabbedPane();

        setUpMig();
        tabs.add(migPanel, "Mig Panel");

        setUpMigCC();
        tabs.add(migPanelCC, "Mig Panel Center Center");

        setUpGridBag();
        tabs.add(gridBagPanel, "Grid Bag Panel");

        setUpGridBagCC();
        tabs.add(gridBagPanelCC, "Grid Bag Panel Center Center");
    }

    private void setUpMig() {
        migPanel = new JPanel(new MigLayout("center, filly"));
        addToMigPanel(migPanel);
    }

    private void setUpMigCC() {
        migPanelCC = new JPanel(new MigLayout("center, center"));
        addToMigPanel(migPanelCC);
    }

    private void addToMigPanel(JPanel mPanel) {
        mPanel.add(new JLabel("Header"), "span, align center");
        mPanel.add(new JTextField("Input"), "split 4");
        mPanel.add(new JComboBox<String>(dummyList));
        mPanel.add(new JLabel("To"), "span");
        mPanel.add(new JComboBox<String>(dummyList), "wrap");
        mPanel.add(new JButton("Convert"), "span, align center");
        mPanel.add(new JTextField("Output"), "span, align center");
    }

    private void setUpGridBag() {
        gridBagPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        gc.weighty = 1;
        addToGridBagPanel(gridBagPanel, gc);
    }

    private void setUpGridBagCC() {
        gridBagPanelCC = new JPanel(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        addToGridBagPanel(gridBagPanelCC, gc);
    }

    private void addToGridBagPanel(JPanel gblPanel, GridBagConstraints gc) {
        gc.gridwidth = 4;
        gblPanel.add(new JLabel("Header"), gc);

        gc.gridy = 1;
        gc.gridwidth = 1;
        gblPanel.add(new JTextField("Input"), gc);

        gc.gridx = 1;
        gblPanel.add(new JComboBox<String>(dummyList), gc);

        gc.gridx = 2;
        gblPanel.add(new JLabel("To"), gc);

        gc.gridx = 3;
        gblPanel.add(new JComboBox<String>(dummyList), gc);

        gc.gridy = 2;
        gc.gridx = 0;
        gc.gridwidth = 4;
        gblPanel.add(new JButton("Convert"), gc);

        gc.gridy = 3;
        gblPanel.add(new JTextField("Output"), gc);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new LayoutExample());
    }
}

最后,使用绝对定位是个坏主意。这是因为当容器更改大小时,容器内的组件将不会自动调整。对于大多数事情来说,这可能效果不佳,因为您可能会很快丢失重要的UI组件,或者可能会有大量的空白。