Study Note 22: GUI (iii)

Source: Internet
Author: User

The decisive factor in forming a genius should be diligence. --Guo Moruo


What's in this lecture: Common components


First, Panel components JPanel (non-top-level components)

An interface can have only one jframe form component, but it can have multiple JPanel panel components, and JPanel can also use various layout managers such as Flowlayout,borderlayout,gridlayout. This can be combined to achieve a more complex layout effect.

JPanel is a subclass of JComponent, belongs to the container class component, can add other components, the default is the flow layout.

The public class Text extends jframe{//defines the component JPanel JP1,JP2; JButton jb1,jb2,jb3,jb4,jb5;public static void Main (string[] args) {text t=new text ();} Public Text () {//Create component Jp1=new JPanel ();//jpanel layout default is Flowlayoutjp2=new JPanel (); Jb1=new JButton ("Snow Plum"); Jb2=new JButton ( "Chi Hao"); Jb3=new JButton ("bodybuilding"); Jb4=new JButton ("Chi Hua"); jb5=new JButton ("Xiao Jing");//Add components to the  JPanel to complete the first, Add the JFrame component Jp1.add (JB1); Jp1.add (JB2); Jp2.add (JB3); Jp2.add (JB4); This.add (Jp1,borderlayout.south); This.add (Jp2,borderlayout.north); This.add (JB5);// Set the form Properties This.settitle ("Layout layout-"); This.setlocation (+); this.setsize (400,300); this.setresizable (false); This.setvisible (True); This.setdefaultcloseoperation (Jframe.exit_on_close);}}



Two, text box (JTextField), Password box (JPasswordField), label (jlable), button (JButton)

Import Java.awt.*;import javax.swing.*;p ublic class Text extends jframe{//define components JPanel jp1,jp2,jp3; JButton jb1,jb2; JLabel jlb1,jlb2; JTextField WBK1; JPasswordField pwd1;public static void Main (string[] args) {text t=new text ();} Public Text () {//Create component Jp1=new JPanel (); Jlb1=new JLabel ("User name"); wbk1=new JTextField (   jp1.add); jlb1 Jp1.add (WBK1); jp2=new JPanel () jlb2=new JLabel ("Secret     Code");p wd1=new JPasswordField (jp2.add);   Jp2.add (PWD1); jp3=new JPanel () jb1=new JButton ("login"); jb2=new JButton ("Cancel"); Jp3.add (JB1)   ; Jp3.add (JB2); This.setlayout (New GridLayout (3,1));//Add Component  This.add (JP1); This.add (JP2); This.add (JP3);//Set form Properties This.settitle ( "Layout layout-"); This.setlocation (+); this.setsize (400,300); this.setresizable (false); this.setvisible (true); This.setdefaultcloseoperation (Jframe.exit_on_close);}}



Three, check box component (Jcheckbox), Radio box component (Jradiobutton)

Note: The same group of radio buttons must first create the Buttongroup and then place the radio box component into the Buttongroup .

The public class Text extends jframe{//defines the component JPanel jp1,jp2,jp3; JButton jb1,jb2; JLabel jlb1,jlb2; Jcheckbox jcb1,jcb2,jcb3; Jradiobutton Jrb1,jrb2; Buttongroup bg1;//put the radio button in a group public static void Main (string[] args) {text t=new text ();} Public Text () {//Create component Jp1=new JPanel (); Jlb1=new JLabel ("speciality"); Jcb1=new Jcheckbox ("Music"); Jcb2=new jcheckbox ("Sport"); jcb3= New Jcheckbox ("literary"); Jp1.add (JLB1); Jp1.add (JCB1); Jp1.add (JCB2); Jp1.add (JCB3); jp2=new JPanel () jlb2=new JLabel ("gender"); Jrb1=new Jradiobutton ("male"); Jrb2=new Jradiobutton ("female"); bg1=new Buttongroup (); Bg1.add (JRB1); Bg1.add (JRB2);//Add into group so that it can only select one Jp2.add (JLB2); Jp2.add (JRB1); Jp2.add (JRB2);//Do not add the group to Jp3=new JPanel (); jb1=new JButton ("registration"); Jb2=new JButton ("Cancel"); Jp3.add (JB1); Jp3.add (JB2); This.setlayout (new GridLayout (3,1));//Add Component This.add (JP1); This.add (JP2); This.add (JP3);// Set the form Properties This.settitle ("User registration-small strength"); This.setlocation (+); this.setsize (400,300); this.setresizable (false); This.setvisible (True); This.setdefaultcloseoperation (Jframe.exit_on_close);}}



Four, drop-down box component (JComboBox), list box component (JList), scroll pane component (JScrollPane)

In general, the list box component + Scroll pane component is used in conjunction with the

The public class Text extends jframe{//defines the component JPanel MB1,MB2; JLabel bq1,bq2; JComboBox xlk; JList lb; JScrollPane gd;public static void Main (string[] args) {text t=new text ();} Public Text () {//Create component Mb1=new JPanel (); mb2=new JPanel (); Bq1=new JLabel ("Hometown") bq2=new JLabel ("Education"); String[] jg={"Yangjiang", "Tianjin", "Shanghai", "Chongqing"};xlk=new JComboBox (JG); String[] xl={"High School", "College", "undergraduate", "Master", "PhD",};lb=new JList (XL); gd=new JScrollPane (lb);//ScrollBar Lb.setvisiblerowcount (3);// The Settings show 3 this.setlayout (new GridLayout (2,1));//Add Component  Mb1.add (BQ1);  Mb1.add (XLK); Mb2.add (BQ2);  Mb2.add (GD);//Unlike radio buttons, add a rolling component This.add (MB1);  This.add (MB2);//Set the form Properties This.settitle ("layout-small Strength"); This.setlocation (+); this.setsize (400,300); This.setresizable (false); this.setvisible (true); This.setdefaultcloseoperation (Jframe.exit_on_close);}}



This is where we go, take your time and enjoy it

Study Note 22: GUI (iii)

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.