JAVA GridBagLayout layout manager application details, javatedbaglayout

Source: Internet
Author: User

JAVA GridBagLayout layout manager application details, javatedbaglayout

In many cases, you do not need to write code to implement a graphical interface of an application. Instead, you can easily use a powerful IDE tool to drag and drop the Code with simple event processing code. However, we have to deal with some problems in this operation. Sometimes we want to freely change the size of a program interface. The interface generated by the drag-and-drop interface often does not provide this function, once a custom interface changes its shape, the layout between components will become messy.

The layout manager in Java applies Strategy to provide a good model for different types of component la S. The Grid group layout manager (GridBagLayout) is considered to be the most powerful among all layout managers. The following uses an example to describe how to use it.

 

1. Implement a frame of drawing board under WindowsXP. below is the design model diagram.


We can regard GridBagLayout as a grid layout without any constraints or restrictions. A component can occupy several rows and columns, and the size setting is free. We can clearly see that the entire graphic board interface is divided into four rows and two columns of a table. We don't have to worry about the size of a cell, just divide it.

In this way, the five specific panels (we use the JPanel object to fill in) constitute the entire interface, with the top side occupying one row and two columns of the Tool Selection Panel (toolSelectPanel ), toolConcretePanel, drawPanel on the right, colorPanel and statePanel on the bottom

 

2. perform the following steps for GridBagLayout layout:

1) set the layout manager of the main interface to GridBagLayout (No rows or columns need to be specified)

2) specify a GridBagConstraints object for each component in the interface (JPanel object here), and indicate that the component is in

Layout Scheme in Manager

3). Add the extremely restrictive condition (GridBagConstraints object) of the component through the following call)

Add (Component, constraints );

 

It is necessary to understand the specific meaning of each attribute in GridBagConstraints so that we can better customize the layout.

@ Gridx, gridy:

Position of the component in the upper left corner. For example, if the panel on the left is in the column 0 in the row 1, gridy = 0 and gridx = 1. The row corresponds to gridy, and the column corresponds to gridx.

@ Gridwidth, gridheight

The number of rows and columns occupied by the component. For example, if the top panel occupies one row and two columns, gridwidth = 2, gridheight = 1

@ Weightx, weighty

It can be simply understood as the increment value of the component size change. For example, if weightx = 100 is set, the component size changes with the cell. When weightx is set to 0, the component size does not change. Of course, weightx and weighty can also be set to other values, but it is of little significance and will not be described in detail.

@ Fill

Filling mode of components in the grid (Allocation Area)

For example, if fill = HORIZONTAL is used, the component fills up the full cells horizontally. If fill = BOTH is used, the entire grid is filled.

@ Anchor

When a component is located in the grid, anchor = EAST indicates the right alignment.

@ Ipadx, ipady

Internal filling refers to adding ipadx to the x direction and ipady to the y direction based on the preferred size of the component. This ensures that the component will not contract to ipadx, the size determined by ipady is below the size, so we can use the value of ipadx and ipady to specify the size of the component, instead of the size of the component, otherwise it will have an unexpected effect.

@ Insets

The filling area is the part between the component and the grid border. Four parameters are left, top, right, and bottom. However, when the fill of the component is set to NONE, the specified insects value is meaningless.

 

3. The code is implemented below. Let's take a look at it first.

This is the interface generated by running.


This is the interface after stretching



The key code is as follows:

 

Java code
  1. Private void addGridBagPanes (){
  2. // Select the upper-side tool panel
  3. JPanel toolSelectPanel = new JPanel ();
  4. ToolSelectPanel. setBackground (Color. green );
  5. This. add (toolSelectPanel, new GBC (0, 0, 2, 1 ).
  6. SetFill (GBC. BOTH). setIpad (200, 50). setWeight (100, 0 ));
  7. // Specific tool panel on the left
  8. JPanel toolConcretePanel = new JPanel ();
  9. ToolConcretePanel. setBackground (Color. YELLOW );
  10. This. add (toolConcretePanel, new GBC (0, 1 ).
  11. SetFill (GBC. BOTH). setIpad (70, 90). setWeight (0,100 ));
  12. // Drawing panel on the right
  13. JPanel drawPanel = new JPanel ();
  14. DrawPanel. setBackground (Color. WHITE );
  15. This. add (drawPanel, new GBC (1,1). setFill (GBC. BOTH ));
  16. // Lower-side Color Selection Panel
  17. JPanel colorPanel = new JPanel ();
  18. ColorPanel. setBackground (Color. LIGHT_GRAY );
  19. This. add (colorPanel, new GBC (0, 2, 2, 1 ).
  20. SetFill (GBC. BOTH). setIpad (100, 50). setWeight (, 0 ));
  21. // Status panel on the lower side
  22. JPanel statePanel = new JPanel ();
  23. StatePanel. setBackground (Color. CYAN );
  24. This. add (statePanel, new GBC (0, 3, 2, 1 ).
  25. SetFill (GBC. BOTH). setIpad (200, 20). setWeight (100, 0 ));
  26. }

 

The GBC class inherits from GridBagConstraints. This aims to simplify the complexity of each direct operation on the GridBagConstraints object, each set method in GBC returns a GBC object, so you can call the set Method in succession. The GBC code is as follows:

 

Java code
    1. Public class GBC extends GridBagConstraints
    2. {
    3. // Initialize the upper left corner
    4. Public GBC (int gridx, int gridy)
    5. {
    6. This. gridx = gridx;
    7. This. gridy = gridy;
    8. }
    9. // Initialize the position and number of rows and columns in the upper left corner.
    10. Public GBC (int gridx, int gridy, int gridwidth, int gridheight)
    11. {
    12. This. gridx = gridx;
    13. This. gridy = gridy;
    14. This. gridwidth = gridwidth;
    15. This. gridheight = gridheight;
    16. }
    17. // Alignment
    18. Public GBC setAnchor (int anchor)
    19. {
    20. This. anchor = anchor;
    21. Return this;
    22. }
    23. // Whether to stretch and stretch
    24. Public GBC setFill (int fill)
    25. {
    26. This. fill = fill;
    27. Return this;
    28. }
    29. // Increment in the x and y directions
    30. Public GBC setWeight (double weightx, double weighty)
    31. {
    32. This. weightx = weightx;
    33. This. weighty = weighty;
    34. Return this;
    35. }
    36. // External filling
    37. Public GBC setInsets (int distance)
    38. {
    39. This. insets = new Insets (distance, distance );
    40. Return this;
    41. }
    42. // Fill out
    43. Public GBC setInsets (int top, int left, int bottom, int right)
    44. {
    45. This. insets = new Insets (top, left, bottom, right );
    46. Return this;
    47. }
    48. // Inner Filling
    49. Public GBC setIpad (int ipadx, int ipady)
    50. {
    51. This. ipadx = ipadx;
    52. This. ipady = ipady;
    53. Return this;
    54. }
    55. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.