在GridBagLayout中将JLabel对齐到面板的左侧

3

我有一个面板,其中我使用网格包布局指定了它。基本上我只有1列和4行。默认情况下标签是居中对齐的。怎样让它们左对齐?

  private void addLabel(String name, int gridx, int gridy, int anchor ){
            gbc.gridx=gridx;
            gbc.gridy=gridy;
            JLabel label=new JLabel(name);
            gbc.fill=GridBagConstraints.HORIZONTAL;
            gbag.setConstraints(label, gbc);
            panel1.add(label);

调用者行为:
gbc=new GridBagConstraints();
        gbag=new GridBagLayout();
panlel1.setLayout(gbag);
addLabel("    Exemption type", 0, 0,anchor );

此外,将gbc.anchor = GridBagConstraints.WEST设置为我的解决方法... - m13r
2个回答

2
尝试
JLabel myLabel = new JLabel("Fubar", SwingConstants.LEFT);

您可以通过调用myLabel.setHorizontalAlignment(SwingConstants.LEFT);来更改已创建的JLabel的水平对齐方式。注意:此方法适用于IT技术相关内容。
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.*;

public class GridBagExample {
   private static void createAndShowUI() {
      String[] data = {"one", "two", "three", "four"};

      JPanel panel = new JPanel(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = 1.0;  // **** comment this line out to see effect ****
      gbc.weighty = 1.0;  // **** comment this line out to see effect ****

      for (int i = 0; i < data.length; i++) {
         JLabel label = new JLabel(data[i]);
         gbc.gridy = i;
         panel.add(label, gbc);
      }

      JFrame frame = new JFrame("GridBagExample");
      frame.getContentPane().add(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

我没能工作。我认为这与Gridbag布局有关。 - Kaushik Balasubramanain
@Kaushik:如果它不起作用,那么我们需要更多的信息。请考虑创建并发布一个最小可编译代码示例(SSCCE)来演示问题(请查看链接)。 - Hovercraft Full Of Eels
@Kaushik:另外,你是否正确设置了GridBagConstraints.fill、anchor和GridBagConstraints.weightx? - Hovercraft Full Of Eels
它起作用了。你能详细解释一下weightx和weighty是什么吗?你也能解释一下fill和anchor是做什么的吗? - Kaushik Balasubramanain
@Kaushik:你会在这里找到所有需要的解释:如何使用GridBagLayout - Hovercraft Full Of Eels
显示剩余2条评论

1
这是我解决这个问题的方法。重要的部分是:
gbc.anchor = GridBagConstraints.WEST;

在指定布局单元并将组件添加到面板之前,需要调用上述方法。

GridBagConstraints anchor

当组件小于其显示区域时,使用此字段确定在显示区域内放置组件的位置...

下面是我实现它的基本方法。
public class ControlPanel extends JPanel{...    
        ControlPanel(){
          this.setLayout(new GridBagLayout());
          //create components
          ...

          GridBagConstraints gbc = new GridBagConstraints(); 

          //space components out a little
          gbc.insets = new Insets(5,5,5,5);

          gbc.gridx = 0;
          gbc.gridy = 0;
          this.add(button_btn,gbc);

          gbc.gridx = 1;
          gbc.gridy = 0;
          this.add(spinner1_pnl,gbc);

          gbc.gridx = 2;
          gbc.gridy = 0;
          this.add(spinner2_pnl,gbc);

          gbc.gridx = 3;
          gbc.gridy = 0;
          this.add(checkbox1_chk,gbc);

          gbc.gridx = 4;
          gbc.gridy = 0;
          this.add(checkbox2_chk,gbc);

          gbc.gridx = 0;
          gbc.gridy = 1;
          gbc.gridwidth = 3;  //span 3 columns
          gbc.anchor = GridBagConstraints.WEST;
          this.add(results_lbl,gbc);
        }
    }

在这种情况下,我在一些其他组件下面放置了一个标签,但最重要的是该标签在其单元格内左对齐(西对齐)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接