Java --- Swing Interface Development Summary, java --- swing Interface

Source: Internet
Author: User

Java --- Swing Interface Development Summary, java --- swing Interface

I. graphical interface of java

1. awt java. awt

The graphic interface launched before jdk1.4 is written in c/c ++, which is not cross-platform.

2. swing javax. swing

Jdk1.4 graphic interface, good cross-platform

Ii. common components

1. Container Components

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

The most flexible container of the JPanel class (you can add them to each other)

2. element components

JLabel label class

JTextField text input box class (you can set the characters in the box and the length of the input box)

JPasswordField password input box (the characters in the box will be replaced by specific characters)

JCheckBox check box class

JComboBox drop-down box class

JButton

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

3. Common Layout

1. The layout class can only be used in containers. SetLayout () is required for layout setting ()

BorderLayout border layout (the form is divided into the top, bottom, left, and top five areas, respectively. NORTH ,. SOUTH ,. WEST ,. EAST ,. CENTER. If it is not specified, it indicates CENTER. If no layout is added, the border layout is added to the CENTER by default)

FlowLayout streaming layout (like a text document, it is arranged in one row, with full line breaks. The default direction is from left to right, and the alignment direction can be set)

GridLayout grid layout (you can set several rows in several columns and set the spacing)

These three classes are located under the awt package. Java. awt. *** Layout

IV. Implementation of the simple logon Interface

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

2. Define an initialization interface method (initUI) in the class)

3. instantiate an object of the top-level container (JFrame) Class in the initialization interface method.

4. Set the attribute value of the JFrame object (title, size, display position, close the exit program, and disable resizing. This step completes the creation of a form)

5. instantiate the element component object and add the object to the form.

6. instantiate the Login class object in the main function and call the interface initialization method with the object name.

Note: The number of setdefaclocloseoperation () is optional. The value 3 indicates exit when the program is disabled. 0 is not disabled. 1 is hidden, 2 is to close the program to continue working.

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

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

V. Code

1. Simple logon Interface

You can use streaming layout or border layout.

Java code

Package cn. wz. study0718; // define a logon interface public class showFrameqq {// The main function of the Entry public static void main (String [] args) {// instantiate the showFrameqq Class Object showFrameqq showqq = new showFrameqq (); // call the qq interface method showqq. initGUI () ;}// define a qq interface method public void initGUI () {// instantiate a JFrame Class Object javax. swing. JFrame jf = new javax. swing. JFrame (); // set the title attribute jf of the form. setTitle ("QQ logon interface"); // you can specify the size attribute jf of the form. setSize (300,200); // set the location attribute jf of the form. setLocation (450,200); // set the exit program jf when the form is closed. setdefaclocloseoperation (3); // you cannot adjust the size of the Form. jf. setResizable (false); // instantiate a layout Class Object java. awt. flowLayout fl = new java. awt. flowLayout (); // set the layout mode of the form to the stream layout jf. setLayout (fl); // instantiate a JLabel label Class Object javax. swing. JLabel jlaName = new javax. swing. JLabel (""); // Add the jlaName object to the JFrame object of the container. add (jlaName); // instantiate a JTextField object javax. swing. JTextField jteName = new javax. swing. JTextField ("Account", 20); // Add the jteName object to the JFrame object of the container. add (jteName); // instantiate a JLabel object javax. swing. JLabel jlaName2 = new javax. swing. JLabel ("registered account"); // Add the jlaName2 object to the JFrame object of the container. add (jlaName2); // instantiate a JPasswordField object javax. swing. JPasswordField jpaName = new javax. swing. JPasswordField ("Enter Password", 20); // Add the jpaName object to the JFrame object of the container. add (jpaName); // instantiate a JLabel object javax. swing. JLabel jlaName3 = new javax. swing. JLabel ("retrieve password"); // Add the jlaName3 object to the JFrame object of the container. add (jlaName3); // instantiate a JCheckBox object javax. swing. JCheckBox jchName = new javax. swing. JCheckBox ("Remember password"); // Add the jchName3 object to the JFrame object of the container. add (jchName); // instantiate a JCheckBox object javax. swing. JCheckBox jchName2 = new javax. swing. JCheckBox ("Automatic Logon"); // Add the jchName3 object to the JFrame object of the container. add (jchName2); // instantiate a JCheckBox object javax. swing. JButton jbuName = new javax. swing. JButton ("login"); // Add the jButton object to the JFrame object of the container. add (jbuName); jf. setVisible (true) ;}} package cn. wz. study0718; // define a logon interface public class showFrameqq {// The main function of the Entry public static void main (String [] args) {// instantiate the showFrameqq Class Object showFrameqq showqq = new showFrameqq (); // call the qq interface method showqq. initGUI () ;}// define a qq interface method public void initGUI () {// instantiate a JFrame Class Object javax. swing. JFrame jf = new javax. swing. JFrame (); // set the title attribute jf of the form. setTitle ("QQ logon interface"); // you can specify the size attribute jf of the form. setSize (300,200); // set the location attribute jf of the form. setLocation (450,200); // set the exit program jf when the form is closed. setdefaclocloseoperation (3); // you cannot adjust the size of the Form. jf. setResizable (false); // instantiate a layout Class Object java. awt. flowLayout fl = new java. awt. flowLayout (); // set the layout mode of the form to the stream layout jf. setLayout (fl); // instantiate a JLabel label Class Object javax. swing. JLabel jlaName = new javax. swing. JLabel (""); // Add the jlaName object to the JFrame object of the container. add (jlaName); // instantiate a JTextField object javax. swing. JTextField jteName = new javax. swing. JTextField ("Account", 20); // Add the jteName object to the JFrame object of the container. add (jteName); // instantiate a JLabel object javax. swing. JLabel jlaName2 = new javax. swing. JLabel ("registered account"); // Add the jlaName2 object to the JFrame object of the container. add (jlaName2); // instantiate a JPasswordField object javax. swing. JPasswordField jpaName = new javax. swing. JPasswordField ("Enter Password", 20); // Add the jpaName object to the JFrame object of the container. add (jpaName); // instantiate a JLabel object javax. swing. JLabel jlaName3 = new javax. swing. JLabel ("retrieve password"); // Add the jlaName3 object to the JFrame object of the container. add (jlaName3); // instantiate a JCheckBox object javax. swing. JCheckBox jchName = new javax. swing. JCheckBox ("Remember password"); // Add the jchName3 object to the JFrame object of the container. add (jchName); // instantiate a JCheckBox object javax. swing. JCheckBox jchName2 = new javax. swing. JCheckBox ("Automatic Logon"); // Add the jchName3 object to the JFrame object of the container. add (jchName2); // instantiate a JCheckBox object javax. swing. JButton jbuName = new javax. swing. JButton ("login"); // Add the jButton object to the JFrame object of the container. add (jbuName); jf. setVisible (true );}}

  

2. Simple calculator Interface

Analysis: Use grid layout.

Java code

Package cn. wz. study0718; // defines a calculator interface public class showCalculaterFrame {// entry main function public static void main (String [] args) {// instantiate the showCalculaterFrame Class Object showCalculaterFrame showCal = new showCalculaterFrame (); // call the method showCal on the calculator interface. initGUI () ;}// defines the method of a calculator interface public void initGUI () {// instantiate a JFrame Class Object javax. swing. JFrame jf = new javax. swing. JFrame (); // set the title attribute jf of the form. setTitle ("Calculator"); // sets the size attribute of the form jf. setSize (250,350); // set the location attribute jf of the form. setLocation (400,200); // set the exit program jf when the form is closed. setdefaclocloseoperation (3); // you cannot adjust the size of the Form. jf. setResizable (false); // instantiate a JTextField text box Class Object // javax. swing. JTextField jteName = new javax. swing. JTextField (20); // Add jteName to the container JFrame. // jf. add (jteName); // instantiate a layout Class Object java. awt. gridLayout gr = new java. awt. gridLayout (5, 4); // set the layout mode of the form to grid layout jf. setLayout (gr); // instantiate 19 JButton objects and 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 and JTextField object to the JFrame container. 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); // set whether the form is visible. setVisible (true );}}

  

Related Article

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.