JAVA learning Swing is definitely a local simple learning, and wing is a local

Source: Internet
Author: User

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:

 

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.