Java 在gridbaglayout中将JLabel与面板左侧对齐

Java 在gridbaglayout中将JLabel与面板左侧对齐,java,swing,Java,Swing,我有一个面板,其中我指定了网格袋布局。我基本上有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=G

我有一个面板,其中我指定了网格袋布局。我基本上有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 );
试一试

或者,您也可以通过调用
myLabel.setHorizontalAlignment(SwingConstants.LEFT)对已经创建的JLabel执行相同的操作

编辑:例如:

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();
         }
      });
   }
}
导入java.awt.gridbag约束;
导入java.awt.GridBagLayout;
导入javax.swing.*;
公共类gridbag示例{
私有静态void createAndShowUI(){
字符串[]数据={“一”、“二”、“三”、“四”};
JPanel panel=newjpanel(newgridbagloayout());
GridBagConstraints gbc=新的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;//****注释这一行以查看效果****
gbc.weighty=1.0;//****注释这一行以查看效果****
对于(int i=0;i
以下是我如何解决这个问题的。 重要的是

gbc.anchor = GridBagConstraints.WEST;
在指定所需布局单元之后和将构件添加到配电盘之前,将立即调用上述命令

GridBagConstraints锚定

当零部件小于其显示时,使用此字段 地区它确定了在显示区域内放置 组件

下面是我如何实现它的

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

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

我没有工作。我想这和Gridbag有关layout@Kaushik:如果它不起作用,那么我们需要更多信息。考虑创建并发布一个演示问题的最小可编译代码示例,一个(检查链接)。@ Kasusik:还有,你是否设置了GridBagConstraints.fill、锚点和GridBagConstraints.weightx?我没有设置WexIX。horizontal@Kaushik:必须设置权重X和权重Y,否则,您的组件将全部聚集在容器的中心。请参阅上面的编辑,显示我的意思的代码示例。另外设置gbc.anchor=gridbagsconstraints.WEST对我有用。。。
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);
        }
    }