Personal notes on Java
For other technologies like HTML, CSS, .NET, PHP, etc. check my other blog

Border Layout

Ex.:
myJFrame.setLayout(new BorderLayout(10, 5));
Container pane = myJFrame.getContentPane();
JButton button = new JButton("yeahhhh");
pane.add(button, BorderLayout.PAGE_START);

A BorderLayout object has five areas. These areas are specified by the BorderLayout constants:
  • PAGE_START
  • PAGE_END
  • LINE_START
  • LINE_END
  • CENTER
NOTE: Before JDK release 1.4, the preferred names for the various areas were different, ranging from points of the compass (for example, BorderLayout.NORTH for the top area, or SOUTH, EAST, WEST).
But now the constants described above are preferred because they are standard and enable programs to adjust to languages that have different orientations.

BoxLayout

ref: How to Use BoxLayout

javax.swing.BoxLayout arranges components either on top of each other or in a row.
You might think of it as a version of FlowLayout, but with greater functionality.
As the box layout arranges components, it takes the components' alignments and minimum, preferred, and maximum sizes into account.

Set vertical(BoxLayout.PAGE_AXIS) or horizontal (BoxLayout.LINE_AXIS) flow:
myJFrame.setLayout(new BoxLayout(myJFrame.getContentPane(), BoxLayout.Y_AXIS));
Note: LINE_AXIS and PAGE_AXIS are preferred over the old X_AXIS and Y_AXIS because they enable programs to adjust to languages that have different orientations.

BoxLayout tries to size each component at the component's preferred height. If the vertical space of the layout does not match the sum of the preferred heights, then BoxLayout tries to resize the components to fill the space.
Alignments affect not only the components' positions relative to each other, but also the location of the components (as a group) within their container.

To define space between the components you can use invisible components (fillers) like:
Constructor or Method Purpose
Box(int) Creates a Box — a container that uses a BoxLayout with the specified axis. As of release 1.3, Box extends JComponent. Before that, it was implemented as a subclass of Container.
static Box createHorizontalBox()
(in Box)
Creates a Box that lays out its components from left to right.
static Box createVerticalBox()
(in Box)
Creates a Box that lays out its components from top to bottom.
Component createRigidArea(Dimension) Create a rigid component.
Component createHorizontalGlue()
Component createVerticalGlue()
Component createGlue()
Create a glue component. Horizontal glue and vertical glue can be very useful.
Component createHorizontalStrut()
Component createVerticalStrut()
Create a "strut" component. We recommend using rigid areas instead of struts.
Box.Filler(Dimension, Dimension, Dimension) Creates a component with the specified minimum, preferred, and maximum sizes (with the arguments specified in that order). See the custom Box.Filler discussion, earlier in this section, for details.

GridLayout

A very simple layout that creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size.
One, but not both, of rows and cols can be zero, meaning that rows or columns will be automatically created.
Example (rows are created automatically and each row has 2 columns):
GridLayout experimentLayout = new GridLayout(0,2);
(...)
compsToExperiment.setLayout(experimentLayout);

compsToExperiment.add(new JButton("Button 1"));
compsToExperiment.add(new JButton("Button 2"));
compsToExperiment.add(new JButton("Button 3"))

GridBagLayout

ref: How to Use GridBagLayout

Its a flexible layout manager but also very verbose.
The components are disposed according to constraints that you define for each one using the java.awt.GridBagConstraints class.
You can reuse the same GridBagContraint for each component but you'll need to reset all the previous values that are different for the new component.
Example:
GridBagLayoutTest teste = new GridBagLayoutTest();
myJFrame.setContentPane(teste);
(...)
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
c.fill = GridBagConstraints.BOTH;
this.add(b1, c);

JButton b2 = new JButton("B");
c.gridx = 1;
c.gridy = 0;
c.gridheight = 1;
c.fill = GridBagConstraints.NONE;
this.add(b2, c); 



You can set the following java.awt.GridBagConstraints instance variables:
  • gridx and gridy:
     - takes an int that specifies the row (y) and column (x) at the upper left of the component.
     - takes GridBagConstraints.RELATIVE (the default value) to specify that the component be placed just to the right of (for gridx) or just below (for gridy).
     
  • gridwidth, gridheight:
     - takes an int (default is 1) that specify the number of columns (for gridwidth) or rows (for gridheight) in the component's display area (works similar to a span if you have the fill property set accordingly).
     - takes GridBagConstraints.REMAINDER to specify that the component be the last one in its row (for gridwidth) or column (for gridheight).
     - takes GridBagConstraints.RELATIVE to specify that the component be the next to last one in its row (for gridwidth) or column (for gridheight).
     
  • fill: determine whether and how to resize the component.
    Valid values (defined as GridBagConstraints constants) are: NONE (the default); HORIZONTAL; VERTICAL; BOTH;
     
  • ipadx, ipady: takes an int (default is 0) that specifies the internal padding in pixels (how much to add to the size of the component);
     
  • insets: Specifies the external padding of the component -- the minimum amount of space between the component and the edges of its display area. The value is specified as an Insets object. By default, each component has no external padding.
     
  • anchor: Used when the component is smaller than its display area to determine where (within the area) to place the component. Valid values (defined as GridBagConstraints constants) are CENTER (the default), PAGE_START, PAGE_END, LINE_START, LINE_END, FIRST_LINE_START, FIRST_LINE_END, LAST_LINE_END, and LAST_LINE_START.

Sem comentários:

Enviar um comentário