如何使左或右的GridBagLayout单元里面? [英] How to align left or right inside GridBagLayout cell?

查看:220
本文介绍了如何使左或右的GridBagLayout单元里面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的GridBagLayout 位置是儿童细胞内的中心对齐。如何对齐左边还是右边?

更新

构建code(我知道我可以重复使用 C

  //按键面板
    JPanel的button_panel =新的JPanel();
    button_panel.add(ok_button);
    button_panel.add(cancel_button);

    //将控件对话框
    的GridBagConstraints℃;

    GridBagLayout的布局=新的GridBagLayout();
    的setLayout(布局);

    C =新的GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;

    加(inputSource_label,C);

    C =新的GridBagConstraints();
    c.gridx = 1;
    c.gridy = 0;
    加(inputSource_combo,C);

    C =新的GridBagConstraints();
    c.gridx = 0;
    c.gridy = 1;
    加(output_label,C);

    C =新的GridBagConstraints();
    c.gridx = 1;
    c.gridy = 1;
    加(output_combo,C);

    C =新的GridBagConstraints();
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 2;
    加(button_panel,C);
 

解决方案

在使用的GridBagLayout JLabel的一个表格显示:JTextField中,我希望有一种方法,使我的GridBagConstraints我根据对x,y位置。例如像这样:

 私人的GridBagConstraints createGbc(INT X,int y)对{
      GridBagConstraints的GBC =新的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;
      返回GBC;
   }
 

下面code使一个图形用户界面,如下所示:

 进口java.awt.GridBagConstraints中;
进口java.awt.GridBagLayout中;
进口java.awt.Insets中;
进口的java.util.HashMap;
进口的java.util.Map;

进口的javax.swing *。

公共类GridBagEg {
   私有静态无效createAndShowGui(){
      PlayerEditorPanel playerEditorPane =新PlayerEditorPanel();

      INT结果= JOptionPane.showConfirmDialog(空,playerEditorPane,
            编辑播放器,JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.PLAIN_MESSAGE);
      如果(结果== JOptionPane.OK_OPTION){
         // TODO:做一些与信息

         对于(PlayerEditorPanel.FieldTitle fieldTitle:
            PlayerEditorPanel.FieldTitle.values​​()){
            System.out.printf(%10秒:%S%N,fieldTitle.getTitle()
                  playerEditorPane.getFieldText(fieldTitle));
         }
      }
   }

   公共静态无效的主要(字串[] args){
      SwingUtilities.invokeLater(新的Runnable(){
         公共无效的run(){
            createAndShowGui();
         }
      });
   }
}

@燮pressWarnings(串行)
类PlayerEditorPanel继承JPanel {
   枚举FieldTitle {
      名称(名称),速度(速度),力量(力量);
      私人字符串名称;

      私人FieldTitle(字符串名称){
         this.title =称号;
      }

      公共字符串的getTitle(){
         返回称号;
      }
   };

   私有静态最后插图WEST_INSETS =新的插图(5,0,5,5);
   私有静态最后插图EAST_INSETS =新的插图(5,5,5,0);
   私人地图< FieldTitle,JTextField的> fieldMap =新的HashMap< FieldTitle,JTextField的>();

   公共PlayerEditorPanel(){
      的setLayout(新的GridBagLayout());
      setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder(玩家编辑器),
            BorderFactory.createEmptyBorder(5,5,5,5)));
      GridBagConstraints的GBC;
      的for(int i = 0; I< FieldTitle.values​​()的长度;我++){
         FieldTitle fieldTitle = FieldTitle.values​​()[我]
         GBC = createGbc(0,I);
         添加(新的JLabel(fieldTitle.getTitle()+:,JLabel.LEFT),GBC);
         GBC = createGbc(1,I);
         的JTextField textField的=新的JTextField(10);
         加(textField的,GBC);

         fieldMap.put(fieldTitle,textfield的情况);
      }
   }

   私人GridBagConstraints的createGbc(INT X,int y)对{
      GridBagConstraints的GBC =新的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;
      返回GBC;
   }

   公共字符串getFieldText(FieldTitle fieldTitle){
      返回fieldMap.get(fieldTitle).getText();
   }

}
 

在这个例子中,我显示的JPanel中的JOptionPane,但它可以很容易地显示在一个JFrame或JApplet的或JDialog的,或...

I see that GridBagLayout positions it's children with center alignment within cells. How to align left or right?

UPDATE

Constructing code (I know I could reuse 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);

解决方案

When using GridBagLayout for a tabular display of JLabel : JTextField, I like to have a method that makes my GridBagConstraints for me based on the x, y position. For example something like so:

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

The following code makes a GUI that looks like this:

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

}

In this example, I display the JPanel in a JOptionPane, but it could just as easily be displayed in a JFrame or JApplet or JDialog or ...

这篇关于如何使左或右的GridBagLayout单元里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆