Java GUI layout and setting background

Source: Internet
Author: User

first, the implementation of the interface steps

1, divided into 2 ways:

1) inherit a top-level container class;

2) JFrame as a property of the class.

2, in the construction method inside, the design interface

3. Create Interface class

The design interface inherits the JFrame class and can directly use its methods and properties public class Firstjframe extends Jframe{public firstjframe () {setvisible (true);//SET interface visible , default falsesetsize (300,300);//Set line height, general to set Settitle ("Fist JFrame");//Set Caption Getcontentpane ();//<span style= "color:# FF0000; " > Get the Contentpane</span> that comes with it;} public    static void Main (string[] args)    {    new Firstjframe ();}    }
JFrame as the property of the class public class Secondframe {JFrame jf;public secondframe () {jf=new JFrame (); jf.setvisible (true);//The Settings interface is visible, Default Falsejf.setsize (300,300);//Set the line height, general to set Jf.settitle ("Fist JFrame");//Set Caption Jf.getcontentpane ();//Get Your own ContentPane} public static void Main (string[] args) {new Secondframe ();}}
second, the component layout class

1, FlowLayout the control according to the horizontal direction or vertical direction. Usage Reference Code:

public class Flowlayoutdemo extends Jframe{jpanel JP; JButton btn1,btn2,btn3;public Flowlayoutdemo () {setvisible (true); SetSize (400,400); Settitle ("text field"); jp=new JPanel (); Btn1=new JButton ("new"), Btn2=new JButton ("Modify"), Btn3=new JButton ("delete"),//Setup Panel layout FlowLayout layout=new flowlayout ();// You can also use the alignment as a parameter layout.setalignment (Flowlayout.left) at new,//To set the alignment of controls in the JPanel, flowlayout.left for left alignment jp.setlayout (                Layout),//Setup Panel layouts Jp.add (BTN1);                Jp.add (BTN2); Jp.add (BTN3); jp.setcomponentorientation (componentorientation.right_to_left);//<span style= "color: #FF0000;" > Set the display order of controls in JPanel, Right_to_left to right-to-left </span>add (JP);} public static void Main (string[] args) {new Flowlayoutdemo ();}}
post-run interface:


2, BorderLayout is divided into North South EAST WEST Center Five regions, is the JFrame default layout. Usage Reference Code:

public class Borderlayoutdemo extends Jframe{jpanel JP; JButton btn1,btn2,btn3,btn4,btn5;public Borderlayoutdemo () {setvisible (true); SetSize (400,400); Settitle ("text field"); jp= New JPanel (); btn1=new JButton ("System logo") Btn2=new JButton ("Contact Us"); Btn3=new JButton ("Main Page"); btn4=new JButton ("menu link"); Btn5=new JButton ("ad");//Set layout to borderlayout,jframe default flow layout jp.setlayout (new BorderLayout ()); Jp.add (BTN1, Borderlayout.north)///Jp.add (Btn2,borderlayout.south) in the upper area, or//in the area below Jp.add (btn3,borderlayout.center);// Put in the middle of the area jp.add (btn4,borderlayout.west),//On the left side of the area Jp.add (btn5,borderlayout.east),//On the right side of the area add (JP);} public static void Main (string[] args) {new Borderlayoutdemo ();}}
post-run interface:


3, GridLayout in accordance with the ranks of the display control, can not be arbitrarily placed control , is the JPanel default layout. Also known as the flow layout, the default is left to right in the order in which they are added, beyond the width of the line, wrapping automatically.

Usage Reference Code:

public class Gridlayoutdemo extends Jframe{jpanel JP; JButton btn1,btn2,btn3,btn4,btn5;public Gridlayoutdemo () {setvisible (true); SetSize (400,400); Settitle ("text field"); jp= New JPanel (); btn1=new JButton ("System logo") Btn2=new JButton ("Contact Us"); Btn3=new JButton ("Main Page"); btn4=new JButton ("menu link"); Btn5=new JButton ("advertising");//Set layout to Gridlayout,jpanel default flow layout GridLayout layout=new GridLayout (2,3);//<span style= "COLOR: #FF0000; " > When instantiated, you can specify the row and column, so that the line is arranged first, the row is full, and then the next line </span>jp.setlayout (Layout); Jp.add (BTN1); Jp.add (BTN2); Jp.add (BTN3); Jp.add (BTN4); Jp.add (BTN5); Add (JP);} public static void Main (string[] args) {new Gridlayoutdemo ();}}
post-run interface:

4, GridBagLayout According to the ranks of the display control, you can set the row of the control. Property: Gridwidth Sets the number of columns that the control occupies gridheight the number of rows that the control takes up.

Usage Reference Code:

public class Gridbaglayoutdemo extends Jframe{jpanel JP; JButton btn1,btn2,btn3,btn4,btn5;public Gridbaglayoutdemo () {setvisible (true); SetSize (400,400); Settitle ("text field"); JP =new JPanel () btn1=new JButton ("btn1"); Btn2=new JButton ("btn2"); Btn3=new JButton ("Btn3"); Btn4=new JButton ("Btn4"); Btn5=new JButton ("btn5");//Set layout to Gridbaglayoutgridbaglayout layout=new gridbaglayout (); jp.setlayout (layout); Gridbagconstraints c=new gridbagconstraints ();//Specify Baglayout constraints (coordinates) c.gridx=5;//x represent columns c.gridy=5;//y represent rows c.fill= gridbagconstraints.horizontal;//sets how the control is populated, horizontal to fill the remaining columns with full rows c.gridwidth=gridbagconstraints.remainder;// remainder specifies that the current control is the last row or column jp.add (BTN1,C);//Add the first row Sixth column c.gridx=1;c.gridy=1;c.gridwidth=2;//specify the current control as 2 columns c.gridheight=2;/ /Specifies that the current control occupies 2 rows Jp.add (BTN2,C);//Add second row second column c.gridx=2;c.gridy=2;c.gridwidth=1;jp.add (BTN3,C);//Add third row third column c.gridx=3; C.gridy=2;c.gridwidth=1;jp.add (btn4,c);//Add third row Fourth column c.gridx=0;c.gridy=0;c.gridwidth=1;jp.add (BTN5,C);// Add first Row first column add (JP);} public static void Main (string[] args) {new Gridbaglayoutdemo ();}}
post-run interface:



5, BoxLayout vertical or horizontal positioning components, will not be changed due to the size of the interface to change the position of the control. Usage Reference Code:

public class Boxlayoutdemo extends Jframe{jbutton btn1,btn2,btn3,btn4,btn5;public Boxlayoutdemo () {setvisible (true); SetSize (400,400); Settitle ("BoxLayout"); Setdefaultcloseoperation (exit_on_close);//Specify interface default off option  Exit_on_ Close to exit the program Btn1=new JButton ("Btn1") when closed, Btn2=new JButton ("btn2"), Btn3=new JButton ("Btn3"), Btn4=new JButton ("Btn4"); Btn5=new JButton ("Btn5"); Add (btn1); add (btn2); add (btn3); add (btn4); add (BTN5); BoxLayout layout=new BoxLayout (Getcontentpane (), Boxlayout.y_axis);//getcontentpane ();//jframe get a panel similar to JPanel jp/ /boxlayout.y_axis set the vertical placement control setlayout (layout);} public static void Main (string[] args) {new Boxlayoutdemo ();}}
post-run interface:


6, Grouplayout Group layout (group: A collection of some components): first horizontal grouping and then vertical grouping, divided into:

1) Continuous group: Vertical or horizontal, one after another.

2) Parallel group: Vertical or horizontal, one component at the top of another component.

Grouplayout Create steps:

First step: Write the horizontal direction layout: Continuous group SG1 contains: b1,b2, parallel group PG1 (B3,B4) F, Method: Grouplayout.sethorizontalgroup ()

Step Two: Write vertical layout: Continuous group SG2 contains: pg2 (B1,B2,B3), B4, method: Grouplayout.setverticalgroup

Usage Reference Code:

public class Grouplayoutdemo extends Jframe{jbutton btn1,btn2,btn3,btn4;public Grouplayoutdemo () {setvisible (true); SetSize (400,400); Settitle ("BoxLayout"); Setdefaultcloseoperation (exit_on_close);//Specify interface default off option Exit_on_ Close to exit the program Grouplayout layout=new grouplayout (Getcontentpane ()), setlayout (layout), Btn1=new JButton ("btn1"); btn2 =new JButton ("btn2"); Btn3=new JButton ("Btn3"); Btn4=new JButton ("Btn4"); Add (btn1); add (btn2); add (btn3); add (btn4);// Write Horizontal layout: Continuous group SG1 contains: b1,b2, parallel group P1 (B3,B4) Grouplayout.sequentialgroup sg1=layout.createsequentialgroup ();//Create continuous Group object, As a horizontal layout sg1.addcomponent (BTN1);//Add Component Sg1.addcomponent (BTN2) to a continuous group, or//parallel group consisting of vertical B3 and B4, PG1 as the 3rd element of SG1 Grouplayout.parallelgroup Pg1=layout.createparallelgroup (); Sg1.addgroup (PG1);p g1.addcomponent ( BTN3);p g1.addcomponent (BTN4); Layout.sethorizontalgroup (SG1);//Specifies the horizontal direction of the layout sg1//write vertical layout: Continuous group SG2 contains: pg2 (B1,B2,B3), B4grouplayout.sequentialgroup Sg2=layout.createsequentialgroup ();//create continuous group objects as horizontal layout//btn1,btn2, Btn3 as a parallel grouping pg2 as the first element of SG2 Grouplayout.parallelgrOUP Pg2=layout.createparallelgroup ();p g2.addcomponent (BTN1);p g2.addcomponent (btn2);p g2.addcomponent (BTN3); Sg2.addgroup (PG2); sg2.addcomponent (BTN4); Layout.setverticalgroup (SG2);//Specifies a vertical layout sg2}public static void Main ( String[] (args) {new Grouplayoutdemo ();}}
post-run interface:

7. Custom layout: Use SetBounds (x, y, width, height);

1) Empty the layout of the parent container: setlayout (NULL)

2) Set the coordinates, width, and length for each control.

The code can refer to examples in the GUI foundation.

Third, add the background

1, set the general component background, using the SetIcon method, use the reference code:

public class Backgrounddemo extends Jframe{jpanel JP; JButton btn1;public Backgrounddemo () {setvisible (true); SetSize (400,400); Settitle ("background"); jp=new JPanel (); btn1=new JButton ("System logo"); Btn1.seticon (New ImageIcon (". Src/images/4.bmp"));        Jp.add (BTN1);        Add (JP);} public static void Main (string[] args) {new Backgrounddemo ();}}
2, set JPanel container background, usage reference code:

public class Jpanelbackgrouddemo extends Jframe{mypanel panel; JButton btn; Container c;public Jpanelbackgrouddemo () {setvisible (true); SetSize (400,400); Settitle ("background");p anel=new Mypanel (new ImageIcon (". Src/images/vvjjj.jpg"). GetImage ()) btn=new JButton ("registration"); setlayout (null);//Do not use any layout c=getcontentpane ( );p anel.setbounds (0, 0, +), C.add (panel);p Anel.add (BTN); public static void Main (string[] args) {new Jpanelbackgrouddemo ();}} Overriding the container class, such as the Paintcoponent () method of the JPanel class, draws the graphics class Mypanel extends Jpanel{image img;public mypanel (Image img) {this.img=img ; This.setopaque (true);//Set Transparency}//draw container public void paintcoponent (Graphics g) {     super.paintcomponent (g);// The method to get the original drawing component of the parent class     //The first parameter is the picture to be the background, the 2nd 3 represents the start coordinate, and the 4th/5 represents the width and height of the picture     G.drawimage (Img,0,0,this.getwidth (), This.getheight (), this);}}


Java GUI layout and setting background

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.