Java---Swing interface Development summary

Source: Internet
Author: User

One, the Java graphical interface

1.AWT java.awt

jdk1.4 before the introduction of the graphical interface, written in C + +, cross-platform is not good

2.swing javax.swing

jdk1.4 when the graphical interface, the cross-platform of good

Second, the commonly used components

1. Container components

JFrame the top-level container of the form class (the default layout is a border layout and cannot be added to each other)

JPanel Panel class Most flexible containers (can be added to each other)

2. Element components

JLabel Label class

JTextField Text Input box class (you can set the character in the box, the length of the input box)

JPasswordField Password Input box class (the Characters in the box will be replaced by specific characters)

Jcheckbox check box class

JComboBox drop-down box class

JButton Button Class

Containers and components are classes under the swing package. Javax.swing.JFrame

Third, the common layout

1. Layout classes can only be used in containers. SetLayout () required to set layout

BorderLayout Border layout (divides the form into five regions in the upper and lower left and right, respectively, when called.) North,. South,. WEST,. EAST,. Center, when no layout is indicated, default to border layout when not added to center)

FlowLayout Flow layout (line-by-line arrangement as text document, arrange full wrap, default direction is left-to-right, you can set alignment direction)

GridLayout Grid layout (can be set several rows, spacing)

These three types are all classes that are located under the AWT package. Java.awt.***layout

Iv. implementation of the simple login interface

1. Create a new XXX class (Login) and define the main function in the class.

2. Define a method for initializing the interface in a class (Initui)

3. In the Initialize interface method, instantiate an object of a top-level container (JFrame) class.

4. Set the property value of the JFrame object (title, size, display position, close exit program, prohibit resizing, visible, step completion of creation of a form)

5. Instantiate the element Component object and add the object to the form

6. Instantiate the object of the login class in the main function and invoke the method of initializing the interface with the object name.

Note: setdefaultcloseoperation () optional number in brackets, 3 is closed when exiting the program. 0 is not closed. 1 is hidden, 2 is closed when the program continues to work.

SetVisible (true) must be placed at the end to display all components. True is visible.

SetSize () size is the size of the pixel point. The default starting point is the upper-left corner.

Five, the Code

1. Simple Login Interface

You can use a streaming layout or a border layout.

Java code

Package cn.wz.study0718; Define a login interface public class Showframeqq {//Portal main function public static void main (string[] args          {//Instantiate the object of the Showframeqq class showframeqq showqq = new Showframeqq ();           Call the QQ interface method Showqq.initgui (); }//Define a QQ interface method public void Initgui () {//Instantiate an object of the JFrame class Javax.swing.JFrame JF = new JAVAX.SWI Ng.          JFrame ();          Set the Title property of the form Jf.settitle ("QQ login Interface");          Set the Size property of the form jf.setsize (300,200);          Set the Position property of the form jf.setlocation (450,200);          Setting the form off when exiting the program jf.setdefaultcloseoperation (3);                        Sets the size jf.setresizable (false) of the prohibit resizing form;           Instantiate an object of a layout class java.awt.FlowLayout fl = new Java.awt.FlowLayout ();          Set the layout of the form to flow layout jf.setlayout (fl);                                          Instantiate an object of a JLabel tag class Javax.swing.JLabel jlaname = new Javax.swing.JLabel ("        ");  Add the Jlaname object to the container JFrame object Jf.add (jlaname);          Instantiate a JTextField object Javax.swing.JTextField jtename = new Javax.swing.JTextField ("account", 20);          Add the Jtename object to the container JFrame object Jf.add (jtename);          Instantiate a JLabel object Javax.swing.JLabel jlaName2 = new Javax.swing.JLabel ("registered account");          Add the JlaName2 object to the container JFrame object Jf.add (jlaName2);          Instantiate a JPasswordField object Javax.swing.JPasswordField jpaname = new Javax.swing.JPasswordField ("Please enter password", 20);          Add the Jpaname object to the container JFrame object Jf.add (jpaname);          Instantiate a JLabel object Javax.swing.JLabel jlaName3 = new Javax.swing.JLabel ("Retrieve password");          Add the JlaName3 object to the container JFrame object Jf.add (JlaName3);          Instantiate a Jcheckbox object Javax.swing.JCheckBox jchname = new Javax.swing.JCheckBox ("Remember Password");          Add the JchName3 object to the container JFrame object Jf.add (jchname); Instantiate a Jcheckbox object Javax.swing.JCheckBox jchName2 = new Javax.swing.JCheckBox ("Auto Login");          Add the JchName3 object to the container JFrame object Jf.add (jchName2);           Instantiate a Jcheckbox object Javax.swing.JButton jbuname = new Javax.swing.JButton ("login");                        Add the JButton object to the container JFrame object Jf.add (jbuname);      Jf.setvisible (TRUE); }} Package cn.wz.study0718;//defines a login interface public class Showframeqq {//Portal main function public static void main (string[] args)   {//Instantiate the object of the Showframeqq class showframeqq showqq = new showframeqq ();//Call the QQ interface method Showqq.initgui (); }//defines a method of QQ interface public void Initgui () {//Instantiate an object of a JFrame class javax.swing.JFrame JF = new Javax.swing.JFrame ();// Set the Caption property of the form Jf.settitle ("QQ login Interface");//Set the Size property of the form jf.setsize (300,200);//Set the Position property of the form jf.setlocation (450,200);// Set the form off when the program exits jf.setdefaultcloseoperation (3);//sets the size of the form to prevent jf.setresizable (false);//Instantiate an object of a layout class Java.awt.FlowLayout FL = new Java.awt.FlowLayout ();//sets the layout of the form to flow layout jf.setlayout (fl);//Instantiate an object of a JLabel tag class Javax.swing.JLabel jlaname = new           Javax.swing.JLabel ("                               ");//Add the Jlaname object to the container JFrame object Jf.add (jlaname);//Instantiate a JTextField object Javax.swing.JTextField Jten ame = new Javax.swing.JTextField ("account", 20);//Add Jtename object to the container JFrame object Jf.add (jtename);// Instantiate a JLabel object Javax.swing.JLabel jlaName2 = new Javax.swing.JLabel ("register Account");//Add JlaName2 object to the container JFrame object Jf.add ( JLANAME2);//Instantiate a JPasswordField object Javax.swing.JPasswordField jpaname = new Javax.swing.JPasswordField ("Please enter password", 20); /Add the Jpaname object to the container JFrame object Jf.add (jpaname);//Instantiate a JLabel object Javax.swing.JLabel jlaName3 = new Javax.swing.JLabel ("Retrieve password ");//Add the JlaName3 object to the container JFrame object Jf.add (JLANAME3);//Instantiate a Jcheckbox object Javax.swing.JCheckBox jchname = new Javax.swing.JCheckBox ("Remember Password");//Add JchName3 object to Container JFrame object Jf.add (jchname);// Instantiate a Jcheckbox object Javax.swing.JCheckBox jchName2 = new Javax.swing.JCheckBox ("Auto Login");// Add the JchName3 object to the container JFrame object Jf.add (jchName2);//Instantiate a Jcheckbox object Javax.swing.JButton jbuname = new Javax.swing.JButton ( "Login");//Add the JButton object to the container JFrame object Jf.add (jbuname); Jf.setvisIble (TRUE);} }

  

2. Simple Calculator interface

Analysis: Use a grid layout.

Java code

Package cn.wz.study0718; Define a calculator interface public class Showcalculaterframe {//Portal main function public static void main (string[] args) {/          /Instantiate an object of the Showcalculaterframe class showcalculaterframe showcal = new Showcalculaterframe ();           Call the Calculator interface method Showcal.initgui (); }//define a calculator interface method public void Initgui () {//Instantiate an object of the JFrame class Javax.swing.JFrame JF = new JAVAX.SW Ing.          JFrame ();          Set the Caption property of the form Jf.settitle ("calculator");          Set the Size property of the form jf.setsize (250,350);          Set the Position property of the form jf.setlocation (400,200);          Setting the form off when exiting the program jf.setdefaultcloseoperation (3);                        Sets the size jf.setresizable (false) of the prohibit resizing form;          Instantiate an object that JTextField a text box class//javax.swing.jtextfield jtename = new Javax.swing.JTextField (20);          Add the Jtename to the container jframe//jf.add (jtename);          Instantiate a layout class object Java.awt.GridLayout gr = new Java.awt.GridLayout (5,4); Setup windowThe layout of the body is grid layout jf.setlayout (GR);          Instantiate 19 JButton objects, a JTextField object Javax.swing.JButton jb1 = new Javax.swing.JButton ("1");          Javax.swing.JButton jb2 = new Javax.swing.JButton ("2");          Javax.swing.JButton jb3 = new Javax.swing.JButton ("3");          Javax.swing.JButton jb4 = new Javax.swing.JButton ("4");          Javax.swing.JButton jb5 = new Javax.swing.JButton ("5");          Javax.swing.JButton jb6 = new Javax.swing.JButton ("6");          Javax.swing.JButton jb7 = new Javax.swing.JButton ("7");          Javax.swing.JButton jb8 = new Javax.swing.JButton ("8");          Javax.swing.JButton jb9 = new Javax.swing.JButton ("9");          Javax.swing.JButton jb0 = new Javax.swing.JButton ("0");          Javax.swing.JButton JBA = new Javax.swing.JButton ("+");          Javax.swing.JButton jbb = new Javax.swing.JButton ("-");          Javax.swing.JButton JBC = new Javax.swing.JButton ("*");         Javax.swing.JButton jbd = new Javax.swing.JButton ("/"); Javax.swing.JButton Jbe = new Javax.swing.JButton ("=");          Javax.swing.JButton JBF = new Javax.swing.JButton ("AC");          Javax.swing.JButton JBG = new Javax.swing.JButton ("sqrt");          Javax.swing.JButton JBH = new Javax.swing.JButton ("1/x");          Javax.swing.JButton Jbi = new Javax.swing.JButton (".");          Javax.swing.JTextField jte = new Javax.swing.JTextField ();          Add the JButton object, JTextField object to the container JFrame jf.add (jte);          Jf.add (JBF);          Jf.add (JBG);          Jf.add (JBH);          Jf.add (JB1);          Jf.add (JB2);          Jf.add (JB3);          Jf.add (JBA);          Jf.add (JB4);          Jf.add (JB5);          Jf.add (JB6);          Jf.add (JBB);          Jf.add (JB7);          Jf.add (JB8);          Jf.add (JB9);          Jf.add (JBC);          Jf.add (JB0);          Jf.add (JBI);          Jf.add (JBE);                        Jf.add (JBD);                                                Sets whether the form is visible jf.setvisible (true); }       }

Transferred from: http://www.bdqn.cn/news/201307/10424.shtml

Java---Swing interface Development summary

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.