Java Interface Programming (3)------Control layout

Source: Internet
Author: User

This article is your own study notes, welcome reprint, but please indicate the source: http://blog.csdn.net/jesson20121020

In Java, the way components are placed on a form may not be the same as other GUI systems. First, it is completely code-based and has no "resources" to control the layout of the components. Second, the way a component rotates on a form is not controlled by absolute coordinates, but rather by the layout manager, which determines the location based on the order in which the component is joined. With different layout managers, the size, shape and position of the component will vary greatly, and the layout manager can also accommodate the size of the applet or application window, so if the size of the window changes, the size, shape, and position of the component can be changed accordingly.

Japplet,jframe, JDialog. JPanel can include and display components. There is a method called setlayout () in container, which can be used to select different managers. This section demonstrates the effects of each of the different layout managers.

Layout Manager:
1. BorderLayout

JFrame uses BorderLayout as the default layout mode. It will accept the Add () method to put the component in the center, and then stretch the component in each direction until it aligns with the border.

The BorderLayout has a concept of four border areas and a central area, and when you add components to a BorderLayout-managed panel, you can use the overloaded Add () method, whose first parameter accepts a constant value, which can be any of the following:

Constant Explain
BorderLayout. North Top
BorderLayout. South Bottom
BorderLayout. EAST Right end
Borderlayout.west Leftmost
BorderLayout. CENTER Can be filled from the center until it meets the other component's frame

If no placement is specified, the default is to place it in the center.

The following code directly demonstrates the 5 locations of the above table:

public class Swingtest extends Jframe{public swingtest () {super ("Hello Swing"); Setdefaultcloseoperation (Jframe.exit_ On_close); SetSize (+); setvisible (true)            ; Add (borderlayout.north,new JButton ("North")), Add (Borderlayout.south,new JButton ("South")), add (Borderlayout.east, New JButton ("EAST"), Add (Borderlayout.west,new JButton ("WEST")), Add (Borderlayout.center,new JButton ("CENTER"));    public static void Main (string[] args) throws Interruptedexception {Swingutilities.invokelater (new Runnable () {@ overridepublic void Run () {//TODO auto-generated method Stubnew swingtest ();});}
The effect is as follows:


2.FlowLayout

 The FlowLayout layout directly "flows" the component from left to right onto the form until it fills the upper space, then moves down one line and continues to flow. With the flowlayout layout, the component will render a "fit" size, such as the size of the JButton is the size of its label.

The main code is as follows:

FlowLayout layout setlayout (New FlowLayout ()), for (int i = 0;i < 20;i++) {Add (New JButton ("button" +i));}
Effect:

At this point, you can drag the form to zoom the form, and the components inside will change as the form changes.

3. GridLayout

  GridLayout allows you to build a table of rotated components that are added from left to right and from top to bottom when you add components to a table. In the constructor, you specify the number of rows and columns that you want, and they are evenly distributed on the form.

GridLayout layout setlayout (new GridLayout (7,3)), for (int i = 0;i < 20;i++) {Add (New JButton ("button" +i));}
The effect is as follows:

4. GridBagLayout

  GridLayout provides powerful controls, including the ability to accurately determine how the window area is laid out, and how to reposition the component when the window size changes. However, it is also the most complex layout manager, so you might consider using Tablelayout instead.

5. Absolute positioning

You can also set the absolute position of the graphics component:

1) Use the setlayout (null) method to set the window's layout manager to empty.

2) call SetBounds () for each component and pass the parameters of the bounding rectangle in pixel coordinates for the method.

6. BoxLayout

BoxLayout has many benefits of gridbaglayout, but not as complex as gridbaglayout. The boxlayout can control the position of the component horizontally or vertically, and control the component spacing through the so-called "brackets and glue" mechanism.

Summary:

With so many layout managers, in fact, in some cases, it is not appropriate to hand-write GUI forms, which is too complex and does not take full advantage of programming time, so for more complex forms or to use the GUIs construction tool, we only need to understand the layout and how to handle events.




Java Interface Programming (3)------Control layout

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.