介绍

The class GridBagLayout arranges components in a horizontal and vertical manner.

类声明

Following is the declaration for java.awt.GridBagLayout class:

  1. public class GridBagLayout
  2. extends Object
  3. implements LayoutManager2, Serializable

Field

Following are the fields for java.awt.BorderLayout class:

  • double[] columnWeights — This field holds the overrides to the column weights.

  • int[] columnWidths — This field holds the overrides to the column minimum width.

  • protected Hashtable comptable — This hashtable maintains the association between a component and its gridbag constraints.

  • protected GridBagConstraints defaultConstraints — This field holds a gridbag constraints instance containing the default values, so if a component does not have gridbag constraints associated with it, then the component will be assigned a copy of the defaultConstraints.

  • protected java.awt.GridBagLayoutInfo layoutInfo — This field holds the layout information for the gridbag.

  • protected static int MAXGRIDSIZE — The maximum number of grid positions (both horizontally and vertically) that can be laid out by the grid bag layout.

  • protected static int MINSIZE — The smallest grid that can be laid out by the grid bag layout.

  • protected static int PREFERREDSIZE — The preferred grid size that can be laid out by the grid bag layout.

  • int[] rowHeights — This field holds the overrides to the row minimum heights.

  • double[] rowWeights — This field holds the overrides to the row weights.

类的构造函数

S.N.Constructor & Description
1GridBagLayout()Creates a grid bag layout manager.

类方法

S.N.Method & Description
1void addLayoutComponent(Component comp, Object constraints) Adds the specified component to the layout, using the specified constraints object.
2void addLayoutComponent(String name, Component comp) Adds the specified component with the specified name to the layout.
3protected voidadjustForGravity(GridBagConstraints constraints, Rectangle r) Adjusts the x, y, width, and height fields to the correct values depending on the constraint geometry and pads.
4protected voidAdjustForGravity(GridBagConstraints constraints, Rectangle r) This method is obsolete and supplied for backwards compatability only; new code should call adjustForGravity instead.
5protected voidarrangeGrid(Container parent)Lays out the grid.
6protected voidArrangeGrid(Container parent) This method is obsolete and supplied for backwards compatability only; new code should call arrangeGrid instead.
7GridBagConstraints getConstraints(Component comp)Gets the constraints for the specified component.
8float getLayoutAlignmentX(Container parent) Returns the alignment along the x axis.
9float getLayoutAlignmentY(Container parent)Returns the alignment along the y axis.
10int[][]getLayoutDimensions() Determines column widths and row heights for the layout grid.
11protected java.awt.GridBagLayoutInfogetLayoutInfo(Container parent, int sizeflag) Fills in an instance of GridBagLayoutInfo for the current set of managed children.
12protected java.awt.GridBagLayoutInfoGetLayoutInfo(Container parent, int sizeflag) This method is obsolete and supplied for backwards compatability only; new code should call getLayoutInfo instead.
13Point getLayoutOrigin() Determines the origin of the layout area, in the graphics coordinate space of the target container.
14double[][] getLayoutWeights() Determines the weights of the layout grid's columns and rows.
15protected DimensiongetMinSize(Container parent, java.awt.GridBagLayoutInfo info) Figures out the minimum size of the master based on the information from getLayoutInfo().
16protected DimensionGetMinSize(Container parent, java.awt.GridBagLayoutInfo info) This method is obsolete and supplied for backwards compatability only; new code should call getMinSize instead.
17void invalidateLayout(Container target) Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
18void layoutContainer(Container parent) Lays out the specified container using this grid bag layout.
19Point location(int x, int y) Determines which cell in the layout grid contains the point specified by (x, y).
20protected GridBagConstraintslookupConstraints(Component comp) Retrieves the constraints for the specified component.
21Dimension maximumLayoutSize(Container target) Returns the maximum dimensions for this layout given the components in the specified target container.
22Dimension minimumLayoutSize(Container parent) Determines the minimum size of the parent container using this grid bag layout.
23Dimension preferredLayoutSize(Container parent) Determines the preferred size of the parent container using this grid bag layout.
24void removeLayoutComponent(Component comp) Removes the specified component from this layout.
25void setConstraints(Component comp, GridBagConstraints constraints) Sets the constraints for the specified component in this layout.
26String toString() Returns a string representation of this grid bag layout's values.

方法继承

This class inherits methods from the following classes:

  • java.lang.Object

GridBagLayout Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >AwtLayoutDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtLayoutDemo {
  7. private Frame mainFrame;
  8. private Label headerLabel;
  9. private Label statusLabel;
  10. private Panel controlPanel;
  11. private Label msglabel;
  12.  
  13. public AwtLayoutDemo(){
  14. prepareGUI();
  15. }
  16.  
  17. public static void main(String[] args){
  18. AwtLayoutDemo awtLayoutDemo = new AwtLayoutDemo();
  19. awtLayoutDemo.showGridBagLayoutDemo();
  20. }
  21.  
  22. private void prepareGUI(){
  23. mainFrame = new Frame("Java AWT Examples");
  24. mainFrame.setSize(400,400);
  25. mainFrame.setLayout(new GridLayout(3, 1));
  26. mainFrame.addWindowListener(new WindowAdapter() {
  27. public void windowClosing(WindowEvent windowEvent){
  28. System.exit(0);
  29. }
  30. });
  31. headerLabel = new Label();
  32. headerLabel.setAlignment(Label.CENTER);
  33. statusLabel = new Label();
  34. statusLabel.setAlignment(Label.CENTER);
  35. statusLabel.setSize(350,100);
  36.  
  37. msglabel = new Label();
  38. msglabel.setAlignment(Label.CENTER);
  39. msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
  40.  
  41. controlPanel = new Panel();
  42. controlPanel.setLayout(new FlowLayout());
  43.  
  44. mainFrame.add(headerLabel);
  45. mainFrame.add(controlPanel);
  46. mainFrame.add(statusLabel);
  47. mainFrame.setVisible(true);
  48. }
  49.  
  50. private void showGridBagLayoutDemo(){
  51. headerLabel.setText("Layout in action: GridBagLayout");
  52.  
  53. Panel panel = new Panel();
  54. panel.setBackground(Color.darkGray);
  55. panel.setSize(300,300);
  56. GridBagLayout layout = new GridBagLayout();
  57.  
  58. panel.setLayout(layout);
  59. GridBagConstraints gbc = new GridBagConstraints();
  60.  
  61. gbc.fill = GridBagConstraints.HORIZONTAL;
  62. gbc.gridx = 0;
  63. gbc.gridy = 0;
  64. panel.add(new Button("Button 1"),gbc);
  65.  
  66. gbc.gridx = 1;
  67. gbc.gridy = 0;
  68. panel.add(new Button("Button 2"),gbc);
  69.  
  70. gbc.fill = GridBagConstraints.HORIZONTAL;
  71. gbc.ipady = 20;
  72. gbc.gridx = 0;
  73. gbc.gridy = 1;
  74. panel.add(new Button("Button 3"),gbc);
  75.  
  76. gbc.gridx = 1;
  77. gbc.gridy = 1;
  78. panel.add(new Button("Button 4"),gbc);
  79.  
  80. gbc.gridx = 0;
  81. gbc.gridy = 2;
  82. gbc.fill = GridBagConstraints.HORIZONTAL;
  83. gbc.gridwidth = 2;
  84. panel.add(new Button("Button 5"),gbc);
  85.  
  86. controlPanel.add(panel);
  87.  
  88. mainFrame.setVisible(true);
  89. }
  90. }

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

  1. D:\AWT>javac com\tutorialspoint\gui\AwtlayoutDemo.java

If no error comes that means compilation is successful. Run the program using following command.

  1. D:\AWT>java com.tutorialspoint.gui.AwtlayoutDemo

Verify the following output

awt_gridbaglayout.jpg