Java (6)-Swing programming (II), advanced swing

Source: Internet
Author: User

Java (6)-Swing programming (II), advanced swing
3. layout manager

In Swing, each component has a specific position and size in the container. It is difficult to determine the specific position and size when each component is placed in the container. Here we will introduce the layout manager, it provides basic layout functions to effectively process the layout of the entire form. Common Layout managers include flow layout manager, boundary layout manager, and grid layout manager.

1. Absolute Layout

Absolute layout already exists in the previous example. It is hard to specify the position and size of the component in the container. You can use absolute coordinates to specify the position of the component. The procedure is as follows:

(1) Use the Container. setLayout (null) method to cancel the layout manager.

(2) Use the Container. setBounds () method to set the position and size of each component.

Here is a simple example:

Container container = getContentPane (); // create Container JButton jb = new JButton ("button"); // create button jb. setBounds (10, 30,100, 30); // set the button position and size container. add (jb); // add the button to the container

In the setBounds () method, the first two parameters are the xy coordinate of the position, and the last two parameters are the length and width of the button.

2. Stream layout manager

The flow layout manager is the most basic layout manager in the layout manager. It uses the FlowLayout class to place components from left to right like a "stream" until it occupies all the space in this row, move a row down. By default, components are arranged in the center of each row. You can change the position.

In the parameter construction method of FlowLayout, when alignment is set to 0, the components of each row are arranged on the left alignment; When alignment is set to 2, components in each row are arranged in the right alignment. If the value is 1, the component is arranged in the center by default.

The following example shows how to create 10 buttons and arrange them with the stream layout manager.

1 import java. awt. container; 2 import java. awt. flowLayout; 3 4 import javax. swing. JButton; 5 import javax. swing. JFrame; 6 import javax. swing. windowConstants; 7 8 public class FlowLayoutDemo extends JFrame {9 10 public FlowLayoutDemo () {11 Container container = this. getContentPane (); 12 // set the stream layout manager. 2 is the right alignment, and the last two parameters are the horizontal and vertical intervals between components 13 this. setLayout (new FlowLayout (2, 10, 10); 14 15 // Add the button in a loop 16 for (int I = 0; I <10; I ++) {17 container. add (new JButton ("button" + I); 18} 19 20 this. setSize (300,200); 21 this. setVisible (true); 22 this. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); 23} 24 25 public static void main (String [] args) {26 new FlowLayoutDemo (); 27} 28 29}

The first parameter 2 is the right alignment. the horizontal and vertical intervals between each button are 10. The following two graphs are respectively centered with parameters 1 and left alignment with parameters 0. The running result is as follows:

          

3. Border layout manager

When the form layout is not specified, the default layout manager of the Swing component is the border layout manager, which uses the BorderLayout class. In the previous example, A JLabel label occupies the entire space. In fact, the border layout manager is used by default. The border layout manager can also divide containers into the east, south, west, north, and middle areas. You can add components to these five areas.

The following is an example.

1 import java. awt. borderLayout; 2 import java. awt. container; 3 4 import javax. swing. JButton; 5 import javax. swing. JFrame; 6 import javax. swing. windowConstants; 7 8 public class BorderLayoutDemo extends JFrame {9 10 private String [] border = {BorderLayout. CENTER, BorderLayout. NORTH, 11 BorderLayout. SOUTH, BorderLayout. WEST, BorderLayout. EAST}; // This array is used to store 12 private String [] button = {"medium", "North", "South", "West ", "East"}; // this array is used to store the button name 13 14 public BorderLayoutDemo () {15 Container container = this. getContentPane (); 16 this. setLayout (new BorderLayout (); // set the container as the border layout manager 17 18 // Add the button for loop 19 for (int I = 0; I <button. length; I ++) {20 iner. add (border [I], new JButton (button [I]); // The left parameter is set layout, and the right parameter is create button 21} 22 23 this. setVisible (true); 24 this. setSize (300,200); 25 this. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); 26} 27 28 public static void main (String [] args) {29 new BorderLayoutDemo (); 30} 31 32}

The running result is as follows:

  

4. grid layout manager

The grid layout manager divides containers into grids, and components are arranged in columns by row, using the GridLayout class. In this layout manager, each component has the same size and fills up the entire grid, changes the form size, and changes the component accordingly.

Let's look at an instance.

1 import java. awt. container; 2 import java. awt. gridLayout; 3 4 import javax. swing. JButton; 5 import javax. swing. JFrame; 6 import javax. swing. windowConstants; 7 8 public class GirdLayoutDemo extends JFrame {9 10 public GirdLayoutDemo () {11 Container container = this. getContentPane (); 12 this. setLayout (new GridLayout (7, 3, 5, 5); // The first two parameters are 7 rows and 3 columns, the last two parameters are 13 14 for (int I = 0; I <20; I ++) {15 iner. add (new JButton ("button" + I); 16} 17 18 this. setVisible (true); 19 this. setSize (300,300); 20 this. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); 21} 22 23 public static void main (String [] args) {24 new GirdLayoutDemo (); 25} 26 27}

The running result is as follows. The grid consists of seven rows and three columns, and the horizontal and vertical spacing between the grids is 5:

  

Iv. Panel

A panel is also a container that can be used as a container to accommodate other components, but must also be added to other containers. Common panels in Swing include JPanel and JScrollPane panel.

1. JPanel

The JPanel can aggregate components for layout. It inherits from the java. awt. Container class.

The following is an example.

1 import java. awt. container; 2 import java. awt. gridLayout; 3 4 import javax. swing. JButton; 5 import javax. swing. JFrame; 6 import javax. swing. JPanel; 7 import javax. swing. windowConstants; 8 9 public class JPanelDemo extends JFrame {10 11 public JPanelDemo () {12 Container container = this. getContentPane (); 13 container. setLayout (new GridLayout (2, 1, 10, 10); // The entire container is 2 rows, 1 column, 14 15 JPanel p1 = new JPanel (new GridLayout (1, 3); // initialize a panel and set the grid layout of one row and three columns. 16 JPanel p2 = new JPanel (new GridLayout (1, 2); // initialize a panel, set grid layout of 1 row and 2 columns 17 JPanel p3 = new JPanel (new GridLayout (2, 1); // initialize a panel, set the grid layout of two rows and one column 18 JPanel p4 = new JPanel (new GridLayout (3, 2); // initialize a panel, set the grid layout of 3 rows and 2 columns 19 20 p1.add (new JButton ("1 ")); // Add the button 21 p1.add (new JButton ("1") in the JPanel; // Add the button 22 p1.add (new JButton ("1") in the JPanel ")); // Add the button 23 p2.add (new JButton ("2") in the JPanel; // Add the button 24 p2.add (new JButton ("2") in the JPanel ")); // Add the button 25 p3.add (new JButton ("3") in the JPanel; // Add the button 26 p3.add (new JButton ("3") in the JPanel ")); // Add the button 27 p4.add (new JButton ("4") in the JPanel; // Add the button 28 p4.add (new JButton ("4") in the JPanel ")); // Add the button 29 p4.add (new JButton ("4") in the JPanel; // Add the button 30 p4.add (new JButton ("4") in the JPanel ")); // Add the button 31 p4.add (new JButton ("4") in the JPanel; // Add the button 32 p4.add (new JButton ("4") in the JPanel ")); // Add the 33 34 iner button in the JPanel. add (p1); // add panel 35 container to the container. add (p2); // add panel 36 container to the container. add (p3); // add panel 37 container to the container. add (p4); // add panel 38 39 in container this. setVisible (true); 40 this. set size (500,350); 41 this. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); 42} 43 44 public static void main (String [] args) {45 new JPanelDemo (); 46} 47 48}

The running result is as follows. You can compare the code and result to understand JPanel. The GridLayout layout of the container is configured with a spacing of 10 at the horizontal and vertical directions. The GridLayout layout of the JPanel does not have a grid spacing set.

  

2. JScrollPane

If a large part of content is displayed in a small container form, the JScrollPane panel is available. This is a panel with a scroll bar, just like a scroll bar that is frequently encountered when you browse a webpage.

If you need to place multiple components in the JScrollPane panel, you need to place these components on the JPanel panel, and then add the JPanel as a whole component to the JScrollPane panel.

The following is an example.

1 import java. awt. container; 2 3 import javax. swing. JFrame; 4 import javax. swing. JPanel; 5 import javax. swing. JScrollPane; 6 import javax. swing. JTextArea; 7 import javax. swing. scrollPaneConstants; 8 import javax. swing. windowConstants; 9 10 public class JScrollPaneDemo extends JFrame {11 12 public JScrollPaneDemo () {13 Container container = this. getContentPane (); 14 15 JTextArea tArea = new JTextArea (20, 50); // create the text area component 16 tArea. setText ("http://www.cnblogs.com/adamjwh/"); 17 18 JScrollPane sp = new JScrollPane (tArea); 19 container. add (sp); 20 21 this. setVisible (true); 22 this. setSize (300,150); 23 this. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); 24} 25 26 public static void main (String [] args) {27 new JScrollPaneDemo (); 28} 29 30}

JTextArea is used to create a text area component with a size of 20*50. The setText () method is used to fill in a value for the text area. Add the text area component to a new JScrollPane. The running result is as follows:

  

If you want to control the appearance of a scroll bar, you can view the API and use the corresponding method directly.

5. Button component 1. submit button component (JButton)

JButton has appeared many times in the previous example and is a common component used to trigger a specific action. You can display text labels and icons on the buttons, as shown below:

JButton jb = new JButton (); jb. setIcon (icon); // sets the icon jb. setToolTipText ("image button"); // sets the button prompt
2. Single-choice button component (JRadioButton)

By default, a single-choice button displays a circular icon, which usually contains descriptive text. When you select a single button, other buttons in the button group will be automatically canceled. In this case, you need a button group to put the buttons in the same group, you can select only one button in this button group, but not in this button. The syntax format is as follows:

JRadioButton jr1 = new JRadioButton();JRadioButton jr1 = new JRadioButton();JRadioButton jr1 = new JRadioButton();ButtonGroup group = new ButtonGroup();group.add(jr1);group.add(jr2);group.add(jr3);
3. Check box component (JCheckBox)

The check box is a square icon with a descriptive text. The difference between the checkbox and the single-choice button is that you can select multiple boxes. Each check box provides two statuses: "selected" and "not selected. The syntax format is as follows:

JCheckBox jrb  = new JCheckBox();
Vi. List components 1. drop-down list (JComboBox)

The drop-down list box is represented by a JComboBox class object. The Code is as follows:

JComboBox status = new JComboBox (); status. addItem (null); status. addItem ("coming soon"); status. addItem ("coming soon"); status. addItem ("dismounting ");

The display style is as follows:

  

2. list box (JList)

The list box only occupies a fixed size on the form. To make the list box scroll, you can put the list box into the scroll panel.

Parameters of the array initialization list box are as follows.

String[] contents = {"1", "2", "3"};JList jl = new JList(contents);

Use the data of the Vector type as the parameter for JList initialization as follows.

Vector contents = new Vector();JList jl = new JList(contents);contents.add("1");contents.add("2");contents.add("3");
VII. Text Component 1. Text Box (JTextField)

The text box is used to display or edit a single line of text. The syntax format is as follows:

JTextField jt = new JTextField ("aaa"); // create a text box with the value aaaJTextField jt2 = new JTextField ("aaa", 20 ); // create a text box with a length of 20 and a value of aaajt. setText (""); // empty the text box

For other constructor methods, refer to API.

2. Password box (JPasswordField)

The password box is similar to the definition and usage of the text box, but it encrypts the string you enter with a certain symbol. The following code is used:

JPasswordField jp = new JPasswordField (); jp. setEchoChar ('#'); // set the echo symbol
3. Text domain (JTextArea)

The text field component has already appeared in the above Code, as shown in the following code:

JTextArea tArea = new JTextArea (20, 50); // create the text area component tArea. setText ("http://www.cnblogs.com/adamjwh ");

 

The above is all about Swing, and there is not much in-depth content. This part of knowledge will be used for Java classes at school, and Swing is rarely used for external work, so I will introduce some general knowledge. As for Swing's advanced components, JTabel, advanced layout manager, and listener events are no longer introduced. Anyone who wants to know about them can view their own materials.

The advanced article is finished here. If there is time later, you may write more advanced articles, such as some threads and JDBC. If Java is fully written, you may have another article about JavaWeb, or write some Java-based mini-games.

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.