将JLabel对齐到gridbaglayout中面板的左侧

时间:2011-07-31 15:15:38

标签: 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=GridBagConstraints.HORIZONTAL;
            gbag.setConstraints(label, gbc);
            panel1.add(label);

来电者是:

gbc=new GridBagConstraints();
        gbag=new GridBagLayout();
panlel1.setLayout(gbag);
addLabel("    Exemption type", 0, 0,anchor );

2 个答案:

答案 0 :(得分:1)

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

或者您可以通过调用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();
         }
      });
   }
}

答案 1 :(得分:0)

以下是我解决这个问题的方法。 重要的是

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

在这种情况下,我在一些其他组件下方放置了一个标签,但最重要的是,标签在其单元格内保持对齐。