java - 使用 GridBagLayout 将跨列居中

标签 java swing layout-manager gridbaglayout jdialog

我有一个 JDialog,其中有一系列列,我想将 JLabel 置于其中心。我似乎找不到任何关于如何做到这一点的信息。我可以将其放在单元格内,但不能放在一组跨列内。此图像中的标签“目标”和“更改”需要居中。 Time Line Settings

这是生成此 JDialog 的 SSCCE。

package stokerMonitor;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Dialog.ModalityType;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;


public class test {

static JDialog timeLineDialog;
static int row=0;

public static void main(String[] args) {
    timeLineDialog = new JDialog();
    timeLineDialog.setLayout(new GridBagLayout());
    timeLineDialog.setModalityType(ModalityType.MODELESS);
    timeLineDialog.setTitle("Time Line Settings");
    timeLineDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    JLabel timeLabel = new JLabel("Time");
    JLabel actionLabel = new JLabel("Action");
    JLabel probeTempLabel=null;
    JLabel pitTempLabel=null;
    JLabel targetHeader=new JLabel("Target");
    Font boldFont=targetHeader.getFont().deriveFont(Font.BOLD, (float) 14);
    targetHeader.setFont(boldFont);
    JLabel changeHeader=new JLabel("Change");
    changeHeader.setFont(boldFont);
    if (Configuration.getInstance().celsius) {
        probeTempLabel = new JLabel("Temp (\u00B0 C)");
        pitTempLabel = new JLabel("Temp (\u00B0 C)");
    }
    else {
        probeTempLabel = new JLabel("Temp (\u00B0 F)");
        pitTempLabel = new JLabel("Temp (\u00B0 F)");
    }
    JLabel meatLabel=new JLabel("Meat");
    JLabel cookTimeLabel=new JLabel("Est. Time");
    JLabel weightLabel=new JLabel("Weight");
    JLabel probeLabel=new JLabel("Probe");
    JLabel pitLabel=new JLabel("Pit");
    setNewSeparator(1,row);
    GridBagConstraints gbc=makeGbc(2, row);
    gbc.gridwidth=7;
    gbc.fill=GridBagConstraints.HORIZONTAL;
    gbc.anchor=GridBagConstraints.CENTER;
    timeLineDialog.add(targetHeader,gbc);
    setNewSeparator(9,row);
    timeLineDialog.add(changeHeader,makeGbc(10, row));
    setNewSeparator(12,row++);
    setNewSeparator(1,row);
    timeLineDialog.add(timeLabel, makeGbc(2, row));
    timeLineDialog.add(probeTempLabel,makeGbc(3, row));
    timeLineDialog.add(meatLabel,makeGbc(4, row));
    timeLineDialog.add(weightLabel,makeGbc(5, row));
    timeLineDialog.add(cookTimeLabel,makeGbc(6, row));
    timeLineDialog.add(probeLabel,makeGbc(7, row));
    timeLineDialog.add(actionLabel, makeGbc(8, row));
    setNewSeparator(9,row);
    timeLineDialog.add(pitLabel,makeGbc(10, row));
    timeLineDialog.add(pitTempLabel, makeGbc(11, row++));
    setNewSeparator(12,row);
    timeLineDialog.pack();
    timeLineDialog.setLocationRelativeTo(GUI.getInstance().getFrame());
    timeLineDialog.setVisible(true);
}

static void setNewSeparator(int column_,int row_) {
    JSeparator sep=new JSeparator(SwingConstants.VERTICAL);
    sep.setPreferredSize(new Dimension(1,1));
    GridBagConstraints gbc=makeGbc(column_, row_);
    gbc.fill=GridBagConstraints.VERTICAL;
    gbc.weighty=1.;
    timeLineDialog.add(sep,gbc);
}

static GridBagConstraints makeGbc(int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      Insets WEST_INSETS=new Insets(5,0,5,5);
      Insets EAST_INSETS=new Insets(5,5,5,0);
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;

      gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
      gbc.fill = (x == 0) ? GridBagConstraints.BOTH
            : GridBagConstraints.HORIZONTAL;

      gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
      gbc.weightx = (x == 0) ? 0.1 : 1.0;
      gbc.weighty = 1.0;
      return gbc;
   }
}

有人可以解释一下我需要做什么才能使这些标题居中吗? TIA。

最佳答案

提示:在调试布局时,我发现将每个组件设置为具有不同的背景颜色很有用。然后我可以看到分配给该特定组件/由该特定组件填充的区域。大多数组件默认为透明背景,这意味着它们的边界不可见。 (例如targetHeader.setOpaque(true); targetHeader.setBackground(Color.RED);)

布局的情况是 JLabel 填充了您指定的 7 列。但是,标签本身会在其填充的空间的最左侧部分绘制其包含的文本。

有两种替代解决方案:

  1. 告诉标签您希望它使文本在标签内居中: targetHeader.setHorizo​​ntalAlignment(JLabel.CENTER);

  2. 或者,您可以告诉布局填充空间。 gbc.fill=GridBagConstraints.NONE;

关于java - 使用 GridBagLayout 将跨列居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40530274/

相关文章:

java - 当找到特定字母时减去前面的字符

java - 使用 Spring Framework Cloud 时无法将 Artifact 从中央 Maven 存储库传输到中央 Maven 存储库

Java暂停程序执行

java - 如何设置 JTextArea 的最大必要大小

java - 如何使 JPanel 居中(包含 JBox,但不一定)?

java - 在 Swing 中更改 JScrollPane 高度

java - 单例模式的正确使用

java - 如何在默认表模型上添加行

java - 列标题未显示在 JTable 中

java - JTextField 定位/布局问题