简体   繁体   中英

Align JLabel to the left of the panel in a gridbaglayout

I have a panel in which i have specified with a grid bag layout. I basically have 1 column and 4 rows. The labels are aligned to the center by default. How do i align them to the left?

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

The caller lines are:

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

try

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

Or you could do the same on an already created JLabel by calling myLabel.setHorizontalAlignment(SwingConstants.LEFT);

edit: for example:

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

Here is how I solved this. The important part is

gbc.anchor = GridBagConstraints.WEST;

The above is called right after you specify the layout cell you want and right before you add the component to the panel.

GridBagConstraints anchor

This field is used when the component is smaller than its display area. It determines where, within the display area, to place the component. ...

Below is basically how I implemented it.

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

In this case I put a label below some other components, but most importantly that label is left (west) aligned within its cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM