JAVA learning Swing is definitely a local simple learning, and wing is a local
Package com. swing; import java. awt. container; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. windowConstants;/*** 1: common 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 of various components in the container. The layout manager provides a way to arrange Swing components to be displayed in the container, it provides basic * layout functions * 2: Swing provides common layout managers including flow layout manager, border layout manager, and grid layout manager ** 3: in Swing, in addition to layout manager, you can also use absolute layout and absolute layout. As the name suggests, it is * hard to specify the position and size of the component in the container, you can use absolute coordinates to specify the component Position ** 4: follow the steps below to use absolute Layout * first use Container. setLayout (null) method cancel layout manager * Before using absolute layout, you must use the setLayout (null) method to inform the compiler that layout manager is no longer used here * Second, Component is used. setBounds () method to set the size and position of each component ** 5: Use the setBounds (int x, int y, int width, int height) method to set the absolute layout, * First, if the form object calls the setBounds () method * parameter x, y indicates the position of the form on the screen, width, height indicates the width and length of the form * Second, if the component in the form calls the setBounds () method, * the parameter x and y represent the position of the component in the entire form, width, height indicates the size of this component. *** @ author biexiansheng ***/public class AbsolutePosition extends JFrame {public AbsolutePosition () {// define a constructor setTitle ("this form uses absolute layout"); // set the title setLayout (null) of the form ); // cancel the setBounds (200,150,) Setting of the form layout manager; // absolutely locate the position and size of the form Container container = getContentPane (); // create container object JButton jb1 = new JButton ("button 1"); // create button JButton jb2 = new JButton ("button 2 "); // create the button jb1.setBounds (,); jb2.setBounds (,); // set the position and size of the button on the form container. add (jb1); // add the button to the container. add (jb2); // add the button to the container setVisible (true); // make the form visible // set the form closing method setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE);} public static void main (String [] args) {// TODO Auto-generated method stub AbsolutePosition pa = new AbsolutePosition (); // call the constructor when instantiating an object }}
The running results of strength are as follows: