Application of Java Layout manager in real-world engineering

Source: Internet
Author: User

Content Introduction: The appearance of Java is the result of the change of programming art and computing environment, it is the product of intenet development and also the impetus of promoting internet development. But Java as a new programming language, there are many different from the previous programming language places. Layout management is one of them, and the purpose of this article is to explain how to apply layout management to real-world engineering, not as a reference for learning layout management.

Layout management For many programmers is a relatively new concept, because the use of a variety of programming tools has been a good solution to this problem, the programmer does not have to consider the layout of the control problem, simply drag and drop it. But after exposure to Java, especially because of the use of tools such as Elipse, you have to seriously deal with this layout problem. And I personally think that the use of Elipse tool development has its advantages, one of the advantage is that developers can really think in the development process of each class structure, can make the code more excellent, but also make it easier for a programmer to change to a program designers.

As the purpose of this article is to introduce the application of layout management in practical engineering, the basic content of layout management, no longer introduced here, and the reader is positioned in a basic understanding of the layout management of the use of the programmer, not master the basic layout management knowledge of the reader can refer to the Java API provided by Sun

Programmers who start contacting layout management may feel that layout management is more difficult to deal with, adding to the burden that developers have on business logic, which is true, but is far less difficult to imagine than people who have just come in contact with the layout management. Because from the engineering point of view, a user interface is not very complex, of course, I refer to the general database applications, interface extremely complex tool class software. Generally speaking, the main application of database application is to maintain the database, recall our previous work, the user interface is basically composed of menu, data list, data edit component, a set of buttons to submit user's work. The following is a discussion based on the above component as a user interface for a database application.

Let's start by grouping these forms, then put each set of components in a container (this is my previous development process to deal with the user interface of the method, personally think that can simplify the development of the user interface), so we divided the above components into two groups (menu no longer layout considerations) that is, data components and buttons, Then we divide the data components into data lists, the data editing components are divided into two groups, the above groups are placed in their respective containers, the use of the program to express the following:

JFrame sampleFrame = new JFrame("Sample Frame for Manager Layout");
JPanel panelDataContent = new JPanel(); // 数据组件容器
JPanel panelDataList = new JPanel(); // 数据列表容器
JList listData = new JList();
/*
注释1 这里应该是使用布局管理把listData放在panelDataList上
*/
JPanel panelDataEditor = new JPanel(); // 数据编辑组件容器
JTextField editData1 = new JTextField();
//...... // some other edit controls
/*
注释2 这里应该是使用布局管理把数据编辑组件放在panelDataEditor上
*/
/*
注释3 这里应该是使用布局管理器把panelDataList和panelDataEditor放在
panelDataContent上
*/
JPanel panelButton = new JPanel(); // 按钮容器
JButton buttonModify = new JButton("Modify");
//...... //some other buttons
/*
注释4 这里应该是使用布局管理器把按钮放在panelButton上
*/
Container c = sampleFrame.getContentPane();
/*
注释5 这里应该是使用布局管理器把panelButton和panelDataContent放在c上
*/

In this way, we can consider dividing a form into two parts when we start thinking about the layout. So we have two kinds of structure to choose from, the upper and lower structure and the left and right structure. Let's consider the upper and lower structure first.

In my personal aesthetic and facilitative perspective, I will choose to put Panelbutton below, and we should consider that paneldatacontent should occupy all sampleframe space except Panelbutton, Like using Delphi, we set the align of a tpanel instance to alclient. So BorderLayout becomes the layout manager for C. Because in BorderLayout, if you use BorderLayout.

The way of the center is to achieve this. Then Panelbutton should use the Borderlayout.sourth layout. So the code is generated.

c.setLayout(new BorderLayout());//这一句可以省略,因为frame的默认布局是
Borderlayout。
c.add(panelDataContent,BorderLayout.CENTER);
c.add(panelButton,BorderLayout.SOUTH);

We just need to replace annotation 5 with the above code to achieve the first grouping layout. We then use the same layout management to achieve the second grouping, which is the grouping of data components.

panelDataContent.setLayout(new BorderLayout());//不可省略,JPanel的默认布局是
FlawLayout。
panelDataContent.add(panelDataList,BorderLayout.CENTER);
panelDataContent.add(panelDataEditor,BorderLayout.SOUTH);

Replace the above code with the position of comment 3.

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.