Comment aligner à gauche ou à droite dans GridBagLayout cell?

je vois que GridBagLayout positionne ses enfants avec un alignement central à l'intérieur des cellules. Comment aligner à gauche ou à droite?

UPDATE

construire du code (je sais que je pourrais réutiliser 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);
26
demandé sur Dims 2012-03-24 16:19:19

2 réponses

lorsque J'utilise GridBagLayout pour un affichage tabulaire de JLabel: JTextField, j'aime avoir une méthode qui rend mes GridBagConstraints pour moi basé sur la position x, Y. Par exemple quelque chose comme ça:

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

Le code suivant fait une interface graphique qui ressemble à ceci:

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

}

dans cet exemple, j'affiche le JPanel dans un JOptionPane, mais il pourrait tout aussi bien être affiché dans un JFrame ou un JApplet ou un JDialog or ...

40
répondu Hovercraft Full Of Eels 2012-03-24 14:39:43

Par exemple

public class DimsPanel extends JPanel
{
    public static void main(String[] args){
        JFrame main = new JFrame("Dims");
        JPanel myPanel = new DimsPanel();
        main.setContentPane(myPanel);
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setSize(400, 400);
        main.setLocationRelativeTo(null);
        main.setVisible(true);
    }

    JButton ok_button = new JButton("OK"), cancel_button = new JButton("Cancel");
    JLabel inputSource_label = new JLabel("Input source:"), 
                output_label = new JLabel("Output:");
    JComboBox inputSource_combo = new JComboBox(new String[]{"A", "B", "C"}), 
                output_combo = new JComboBox(new String[]{"A", "B", "C"});

    public DimsPanel(){
        super(new BorderLayout());
        Box main = new Box(BoxLayout.Y_AXIS);

        Dimension labelsWidth = new Dimension(100, 0);
        JPanel inputPanel = new JPanel(new BorderLayout()); 
        inputSource_label.setPreferredSize(labelsWidth);
        inputPanel.add(inputSource_label, BorderLayout.WEST);
        inputPanel.add(inputSource_combo);

        JPanel outputPanel = new JPanel(new BorderLayout()); 
        output_label.setPreferredSize(labelsWidth);
        outputPanel.add(output_label, BorderLayout.WEST);
        outputPanel.add(output_combo);
        // button panel
        JPanel button_panel = new JPanel();
        button_panel.add(ok_button);
        button_panel.add(cancel_button);

        main.add(inputPanel);
        main.add(outputPanel);

        add(main, BorderLayout.NORTH);
        add(button_panel);
    }
}

Vous pouvez courir et le voir. Redimensionnement fonctionne comme un charme et le code de mise en page a seulement 18 lignes. Le seul inconvénient est que vous devez spécifier la largeur des étiquettes à la main. Si vous ne voulez vraiment pas voir setPreferredSize() dans le code, alors allez-y Avec GridBag. Mais personnellement, je préfère ce code.

3
répondu Jakub Zaverka 2012-03-24 17:07:38