gpt4 book ai didi

Java:MigLayout Center 中心不工作 |绝对定位的替代方案

转载 作者:行者123 更新时间:2023-11-30 02:04:51 28 4
gpt4 key购买 nike

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

Mig布局

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

组件

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

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

最佳答案

TL;DR

真为你感到羞耻

  • 中心中心不是中心,中心
  • center, center, filly - filly 减轻垂直中心
  • 要对齐各个组件,您需要对齐中心 - 例如,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,center”的影响,而您真正需要的是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 组件,或者可能有大量额外的空白。

关于Java:MigLayout Center 中心不工作 |绝对定位的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51663790/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com