Application of Java Layout Manager in Real Engineering

zhaozj2021-02-16  57

Introduction: The emergence of Java is the result of programming art and computational environment, which is the product of INTENET development. It is also the driving force for the development of Internet. But as a new programming language, Java has many places that are not different from previous programming languages. Layout management is one of them. The purpose of this article is how to apply layout management in a reality engineering rather than acting as a reference for learning layout management.

Layout management is a relatively new concept for numerous programming people, because many programming tools previously used have solved this problem well, simply do not use programmers to consider the layout of controls, only need simple drag Option. But after contacting Java, especially because of tools such as Elipse, this layout problem has to be taken seriously. And I personally think that this tool development using Elipse has its strengths. One of the advantages is that the developer can really think about the structure of each class during the development process, which can make the code more excellent, and make a programmer It is easier to transform one program designer.

Since the purpose of this article is to introduce the application of layout management in real-in-law, the basic content of layout management, this is no longer introduced, and readers are positioned in the programmer that has basically learned how to use the method of layout management, without mastering layout management basics The reader can refer to the Java API provided by Sun.

The programmer who started contact layout may feel that the layout management is relatively difficult to deal with, adding the developer's burden outside the business logic consideration, this is indeed true, but it is not as good as the people who have just contacted the layout management. Because from the perspective of engineering perspective, a user interface is not very complicated, of course, I refer to a general database application, an extremely complex tool software. Generally speaking, the main application of the database application is to maintain the maintenance of the database, recall the project we have previously taken, and the user interface is basically consisting of a button, a data list, a data editing component, a set of buttons used to submit the user's work. The following is discussed based on the user interface of the above components as a database application.

First we pose these forms content, then put each set of components in a container (this is the method of handling user interface during the development process, personal thinking can simplify the development of user interface), so we will put the above Components are divided into two groups (menu no longer layout), which is data components and buttons, and then we divide the data components into data lists, data editing components are divided into two groups, place the above groups in their respective containers, use The procedures are shown in:

JFrame Sampleframe = New Jframe ("Sample Frame for Manager Layout);

JPanel PanelDataContent = new jPanel (); // data component container

JPanel PanelAlist = new jPanel (); // data list container jlist listdata = new jlist (); / * Note 1 Here you should use layout management to put ListData on PanelataList * /

JPanel PanelDataEditor = new jPanel (); // Data Editing Component Container JTextField EditData1 = New JtextField (); // ...... // Some Other Edit Controls / * Note 2 Here you should use the layout management to edit the data editing components Put on PanelataEditor * /

/ * Note 3 Here you should use the layout manager to put PanelDatAlist and PanelDataEditor

PanelDataContent * /

JPanel PanelButton = new jPanel (); // Button Container JButton ButtonModify = New JButton ("Modify"); // ...... // Some Other Buttons / * Note 4 Here you should use the layout manager to put the button On Panelbutton * / Container C = Sampleframe.getContentPane (); / * Note 5 Here you should use the layout manager to put PanelButton and PanelDatAntent on C * /

In this way, we can consider a form into two parts when we start considering the layout. This way we have two structures that can be selected, up and down structures and left and right structures. Let's take into account the upper and lower structure first.

Considering my personal aesthetic point of view and convenience, I will choose to put PanelButton below, and we should consider that PanelDataContent should occupy all Sampleframe space except Panelbutton, just like using delphi, we will take a TPANEL. The align of the instance is set to AlClient. This way BorderLayout becomes a C's Layout Manager. Because in BorderLayout, if BorderLayout is used.

The way CENTER has achieved this purpose. Then PanelButton should use BorderLayout.SOURTH to layout. So I generated code

C.SetLayout (New BorderLayout ()); // This sentence can be omitted, because the default layout of Frame is

BorderLayout. C.ADD (PanelDataContent, BorderLayout.center); C.Add (PanelButton, BorderLayout.South);

We only need to replace the above code 5, and the layout of the first group is realized. Then we use the same layout management to implement the second group, that is, the packet of the data component.

PanelDataContent.setLayout (New BorderLayout ()); // Is not possible, JPanel's default layout is

FlawLayout. PanelDataContent.add (PanelDataList, BorderLayout.center); PanelDataEditor, BorderLayout.South;

The above code can be replaced by the position of the comment 3.

Let's take three JPanel (PanelDatalist, PanelDataEditor, Panelbutton), respectively.

The layout of the control. I also revealed (not explanation) a bit of Java layout management on the size of the space size before processing this problem. I don't have to "explain" because I just learned this, and there is no in-depth research principle. FlowLayout keeps the size of the control on the container he managed (should be preferred size) [1], GridLayout never rating the size of these spaces, just filled the control by GridLayout [2], but we should notice GridLayout Talking about a lattice of a container into an area of ​​size, it is not suitable for us to apply. BorderLayout's control is between the above two [3]. BorderLayout is placed in the appropriate height of the control of North and South, which is placed in the control of East and WEST to keep the appropriate width, making the remaining space in the center [4].

In this way, there are two kinds of the placement of DataList to use BorderLayout layout management, put DataList in the BorderLayout.center, one is to divide the PanelDatAlist with GridLayout. This is also true. Developers can choose one from these two layout according to their own preferences. //PanelDataList.setLayout (New borderLayout ()); // panelDataList.add (listdata, borderlayout.center); PanelDataList.setLayout (New GridLayout (1, 1)); PanelDataList.Add (ListData);

As for the layout format of PanelButton, because of the option of FlowLayout, there may be a reader who will make GridLayout, because if this layout method is used, the area can guarantee that the button is all in a row, but when the user changes the form, the button will appear. Too long or too short. However, the disadvantage of using FlowLayout is that the user makes some buttons that the width is less than the button.

PanelButton.setLayout (New flowLayout ()); // can be omitted. JPanel because FlowLayout

Default layout mode. PanelButton.Add (Buttonnew);

For the layout management of PanelDataEditor, it is necessary to perform judgment according to different situations, and I think the best choice is GridbagLayout. Of course, if the space is discharged is quite simple, it does not reflect its superiority using GridbagLayout. Due to this part of the randomness, this is no longer introduced. Let me write all the code when writing such an article, and is provided to everyone for reference.

/ * * CREATED ON 2003-7-14 * / Import javax.swing. *; Import java.awt. *; / ** * @Author iDilent * supporting the paper Which name is "Java Layout Manager in reality Application "* / public class layoutsample {

Public static void main (string [] args) {jframe sampleframe = new jframe ("Sample Frame for Manager

Layout "); jPanel paneldatacontent = new jPanel (); // data component container

JPanel PanelAlist = new jPanel (); // data list container jlist listdata = new jlist (); // Here you can use //PanelDataList.setLayout (New borderLayout ()); //paneldatalist.add (ListData, BorderLayout .Cter;

PanelDataList.setLayout (New GridLayout (1, 1)); PanelDataList.Add (ListData); / * Note 1 Here you should use layout management to put Listdata on PanelataList * /

JPanel PanelDataEditor = new jPanel (); // Data Editing Component Container JtextField EditData1 = New JtextField (20); PanelDataEditor.Add (EditData1); // ... // Some Other Edit Controls / * Note 2 Here you should Is to use layout management to put data editing components in PanelDataEditor

* // * Note 3 Here you should use the layout manager to put the PanelDatAlist and

PanelDataEditor is placed on the PanelDataContent.setLayout (New BorderLayout ()); // is not omission,

JPanel's default layout is FlawLayout. PanelDataContent.add (PanelDataList, BorderLayout.center); PanelDataEditor, BorderLayout.South;

JPanel panelButton = new JPanel (); // button container JButton buttonNew = new JButton ( "New"); JButton buttonModify = new JButton ( "Modify"); JButton buttonDelete = new JButton ( "Delete"); JButton buttonSave = new JButton ("Save"); // ... // Some Other button / * Note 4 Here you should use the Layout Manager to put the button on PanelButton (Buttonnew); PanelButton.Add (ButtonModify) PanelButton.Add (Buttondlete); PanelButton.add (Buttonsave); Container C = Sampleframe.getContentPane (); / * Note 5 Here you should use the layout manager to put PanelButton and PanelDataContent

Put on C * / C.setLayout (new borderLayout ()); // This sentence can be omitted because the default layout of Frame is BorderLayout. C.ADD (PanelDataContent, BorderLayout.center); C.Add (PanelButton, BorderLayout.South); Sampleframe.setsize (300, 300); // Do not use Pack () reason is to make Jlist

Display sampleframe.show ();}}

[1] The Flow Layout Manager Always Honors A Component's Preferred Size.The Grid Layout Manager Never DoSe.the Border Layout Manager Does Something in Between.

[2] The layout manager honors the preferred heitht of the North and Sourth components and forces them to be exactly as wide ad the container.The East and West regions are almost the opposite of North and South.Center is simply the area that is left Over after space Has Been Given to the Other region.

[1] [2] Complete JavaTM 2 Certification Study Guide

Copyright: IDILENT website reproduced please indicate the author's other reprint, please contact the author (iDilent@yahoo.com.cn).

转载请注明原文地址:https://www.9cbs.com/read-24379.html

New Post(0)