Java Interface Programming (3), java Interface Programming

Source: Internet
Author: User

Java Interface Programming (3), java Interface Programming

This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020

In java, components may be placed on forms in different ways than other GUI systems. First, it is fully code-based and does not have any "Resources" to control components ". Second, the way the component rotates on the form is not controlled by absolute coordinates, but determined by the layout manager according to the order in which the component is added. The size, shape, and position of components vary with different layout managers. In addition, the layout manager can adapt to 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 also be changed accordingly.

JApplet, JFrame, and JDialog. JPanel can contain and display components. There is a setLayout () method in the Container. You can use this method to select different only managers. This section demonstrates the effects of different layout managers.

Layout manager:
1. BorderLayout

JFrame uses BorderLayout as the default Layout mode. It will accept the add () method to place the added component in the center, and then stretch the component to each direction until it is aligned with the border.

BorderLayout has the concept of four border areas and a central area. When adding a component to the Panel managed by BorderLayout, you can use the add () method of overload, its first parameter accepts a constant value, which can be any of the following:

Constant Explanation
BorderLayout. NORTH Top
BorderLayout. SOUTH Bottom
BorderLayout. EAST Right side
BorderLayout. WEST Left side
BorderLayout. CENTER Fill in from the center until it meets the work border of other components

If no location is specified, the data is placed in the center by default.

The following code directly demonstrates the five locations in the preceding table:

public class SwingTest extends JFrame{public SwingTest() {super("Hello Swing");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(300, 200);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 moves the component from left to right to the form until it occupies the upper part of the space, moves down a row, and continues to flow. When FlowLayout is used, the component will display an appropriate size. For example, the size of 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:

In this case, you can drag the form to scale the form, and the components in the form will also change with the changes in the form.

3. GridLayout

GridLayout allows you to create a table for rotating components. When adding components to the table, they are added from left to right, from top to small. In the constructor, specify the required number of rows and columns, which 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 control functions, including precisely determining how the window area is laid out and how to re-place components when the window size changes. However, it is also the most complex layout manager, so you can consider using TableLayout instead.

5. Absolute Positioning

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

1) Use setLayout (null) to set the window layout manager to null.

2) Call setBounds () for each component and pass the boundary rectangle parameter in pixel coordinates for this method.

6. BoxLayout

BoxLayout has many advantages of GridBagLayout, but it is not as complex as GridBagLayout. BoxLayout can control the component Position in the horizontal or vertical direction and control the component interval through the so-called "bracket and glue" mechanism.

Summary:

In fact, in some cases, it is not appropriate to manually write GUI forms. This is too complicated and cannot take full advantage of programming time, therefore, for complex forms, we still need to use the GUIS constructor. We only need to understand the layout method and how to handle events.




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.