java - 如何在 GridBagLayout 单元格内左对齐或右对齐?

标签 java swing gridbaglayout alignment

我看到 GridBagLayout 将它的子元素定位为在单元格内居中对齐。如何左对齐或右对齐?

更新

构造代码(我知道我可以重用 c)

    // button panel
    JPanel button_panel = new JPanel();
    button_panel.add(ok_button);
    button_panel.add(cancel_button);

    // placing controls to dialog
    GridBagConstraints c;

    GridBagLayout layout = new GridBagLayout();
    setLayout(layout);

    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;

    add(inputSource_label, c);

    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 0;
    add(inputSource_combo, c);

    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 1;
    add(output_label, c);

    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 1;
    add(output_combo, c);

    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 2;
    add(button_panel, c);

最佳答案

当使用 GridBagLayout 以表格形式显示 JLabel : JTextField 时,我喜欢有一种方法可以根据 x、y 位置为我生成 GridBagConstraints。例如这样的:

   private GridBagConstraints createGbc(int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      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;
   }

以下代码创建了一个如下所示的 GUI:

enter image description here

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

public class GridBagEg {
   private static void createAndShowGui() {
      PlayerEditorPanel playerEditorPane = new PlayerEditorPanel();

      int result = JOptionPane.showConfirmDialog(null, playerEditorPane,
            "Edit Player", JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.PLAIN_MESSAGE);
      if (result == JOptionPane.OK_OPTION) {
         // TODO: do something with info

         for (PlayerEditorPanel.FieldTitle fieldTitle : 
            PlayerEditorPanel.FieldTitle.values()) {
            System.out.printf("%10s: %s%n", fieldTitle.getTitle(),
                  playerEditorPane.getFieldText(fieldTitle));
         }
      }
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

@SuppressWarnings("serial")
class PlayerEditorPanel extends JPanel {
   enum FieldTitle {
      NAME("Name"), SPEED("Speed"), STRENGTH("Strength");
      private String title;

      private FieldTitle(String title) {
         this.title = title;
      }

      public String getTitle() {
         return title;
      }
   };

   private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
   private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);
   private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>();

   public PlayerEditorPanel() {
      setLayout(new GridBagLayout());
      setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("Player Editor"),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));
      GridBagConstraints gbc;
      for (int i = 0; i < FieldTitle.values().length; i++) {
         FieldTitle fieldTitle = FieldTitle.values()[i];
         gbc = createGbc(0, i);
         add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc);
         gbc = createGbc(1, i);
         JTextField textField = new JTextField(10);
         add(textField, gbc);

         fieldMap.put(fieldTitle, textField);
      }
   }

   private GridBagConstraints createGbc(int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      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;
   }

   public String getFieldText(FieldTitle fieldTitle) {
      return fieldMap.get(fieldTitle).getText();
   }

}

在这个例子中,我在 JOptionPane 中显示 JPanel,但它也可以很容易地显示在 JFrame 或 JApplet 或 JDialog 或 ...

关于java - 如何在 GridBagLayout 单元格内左对齐或右对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9851688/

相关文章:

java - gwt-可视化依赖不起作用?

java - 运行java程序时出现FileNotFoundException错误

java - 我们真的需要EDT吗?

java - 如何将 Web 服务返回的集合 (ArrayList) 绑定(bind)到 Swing 中的 JTable?

java - JTextField 在 GridBagLayout 中未对齐

java - setComponentOrientation 对表单设计器没有影响

java - 使用不同的java更新版本|这对申请有何影响?

java - 我应该使用什么设置来解决这个 GridBagLayout 大小调整问题?

java - Swing 中的 GridBagLayout 格式设置

java - GridBagLayout:多个网格方 block 中的组件