GUI programming of J2SE

Source: Internet
Author: User

AWT

Componet & Container

Frame

/*Example Name: Frame Application Example * source file name: Testframe.java * Important: Frame component creation and display settings*/Importjava.awt.*; Public classTestframe { Public Static voidMain (String args[]) {Frame F=NewFrame ("My First Test"); F.setlocation (300, 300); F.setsize (170,100);        F.setbackground (Color.Blue); F.setresizable (false); F.setvisible (true); }}
Testframe

Importjava.awt.*; Public classTestmultiframe { Public Static voidMain (String args[]) {MyFrame F1=NewMyFrame (100,100,200,200, Color.Blue); MyFrame F2=NewMyFrame (300,100,200,200, Color.yellow); MyFrame f3=NewMyFrame (100,300,200,200, Color.green); MyFrame f4=NewMyFrame (300,300,200,200, Color.magenta); }}classMyFrameextendsframe{Static intID = 0; MyFrame (intXintYintWintH,color Color) {        Super("MyFrame" + (+ +)id));        SetBackground (color); SetLayout (NULL);        SetBounds (X,Y,W,H); SetVisible (true); }}
Testmultiframe

Pannel

Importjava.awt.*; Public classTestpanel { Public Static voidMain (String args[]) {Frame F=NewFRAME ("Java Frame with Panel"); Panel P=NewPanel (NULL); F.setlayout (NULL); F.setbounds (300,300,500,500); F.setbackground (NewColor (0,0,102)); P.setbounds (50,50,400,400); P.setbackground (NewColor (204,204,255));         F.add (P); F.setvisible (true); }}
Testpanel

Importjava.awt.*; Public classTestmultipanel { Public Static voidMain (String args[]) {NewMyFrame2 ("Myframewithpanel", 300,300,400,300); }}classMyFrame2extendsframe{PrivatePanel P1,P2,P3,P4; MyFrame2 (String s,intXintYintWinth) {        Super(s); SetLayout (NULL); P1=NewPanel (NULL); P2 =NewPanel (NULL); P3=NewPanel (NULL); P4 =NewPanel (NULL); P1.setbounds (0,0,w/2,h/2); P2.setbounds (0,h/2,w/2,h/2); P3.setbounds (W/2,0,w/2,h/2); P4.setbounds (W/2,h/2,w/2,h/2);        P1.setbackground (Color.Blue);        P2.setbackground (Color.green);        P3.setbackground (Color.yellow);        P4.setbackground (Color.magenta);        Add (p1); add (p2); add (p3); Add (p4);        SetBounds (X,Y,W,H); SetVisible (true); }}
Testmultipanel

Practice

Importjava.awt.*; Public classCenterpanel { Public Static voidMain (String args[]) {NewMyFrame3 (300,300,400,300, Color.Blue); }}classMyFrame3extendsframe{PrivatePanel p; MyFrame3 (intXintYintWintH,color c) {    Super("Framewithpanel"); SetLayout (NULL);    SetBounds (X,Y,W,H);    SetBackground (c); P=NewPanel (NULL); P.setbounds (W/4,h/4,w/2,h/2);    P.setbackground (Color.yellow);    Add (P); SetVisible (true); }}
Centerpanel

Layout Manager

FlowLayout layout Manager

/*Example Name: FlowLayout Usage Example * source file name: Testflowlayout.java * Key: * 1. Layout manager concept and role * 2. The nature and usage of FlowLayout*/Importjava.awt.*; Public classTestflowlayout { Public Static voidMain (String args[]) {Frame F=NewFrame ("Flow Layout"); Button button1=Newbutton ("OK"); Button Button2=NewButton ("Open"); Button Button3=NewButton ("Close"); F.setlayout (NewFlowLayout (flowlayout.left));        F.add (button1);        F.add (button2);        F.add (Button3); F.setsize (100,100); F.setvisible (true); }}
Testflowlayout

Importjava.awt.*; Public classTestFlowLayout2 { Public Static voidMain (String args[]) {Frame F=NewFRAME ("Java frame"); FlowLayout l=NewFlowLayout (Flowlayout.center, 20, 40);         F.setlayout (l); F.setlocation (300,400); F.setsize (300,200); F.setbackground (NewColor (204,204,255));  for(inti = 1; i<=7; i++) {F.add (NewButton ("button")); } f.setvisible (true); }}
TestFlowLayout2

BorderLayout layout Manager

/*Example Name: BorderLayout Application Example * source file name: Testborderlayout.java * Important: FlowLayout the nature and usage of layout manager*/Importjava.awt.*; Public classTestborderlayout { Public Static voidMain (String args[]) {Frame F; F=NewFrame ("Border Layout"); Button Bn=NewButton ("BN"); Button BS=NewButton ("BS"); Button BW=NewButton ("BW"); Button be=NewButton ("Be"); Button BC=NewButton ("BC"); F.add (BN,"North"); F.add (BS,"South"); F.add (BW,"West"); F.add (BE,"East"); F.add (BC,"Center"); //You can also use the following statement        /*F.add (BN, Borderlayout.north);        F.add (BS, Borderlayout.south);        F.add (BW, borderlayout.west);        F.add (Be, borderlayout.east);        F.add (BC, Borderlayout.center); */F.setsize (200,200); F.setvisible (true); }}
Testborderlayout

GridLayout layout Manager

/*Example Name: GridLayout Application Example * source file name: Testgridlayout * Important: GridLayout the nature and usage of layout manager*/Importjava.awt.*; Public classTestgridlayout { Public Static voidMain (String args[]) {Frame F=NewFrame ("GridLayout Example"); Button B1=NewButton ("B1"); Button B2=NewButton ("B2"); Button B3=NewButton ("B3"); Button B4=Newbutton ("B4"); Button b5=NewButton ("B5"); Button b6=Newbutton ("B6"); F.setlayout (NewGridLayout (3,2));                F.add (B1);        F.add (B2);                F.add (B3);        F.add (B4);                F.add (B5);        F.add (B6);                  F.pack (); F.setvisible (true); }}
Testgridlayout

Importjava.awt.*; Public classtenbuttons { Public Static voidMain (String args[]) {Frame F=NewFRAME ("Java frame"); F.setlayout (NewGridLayout (2,1)); F.setlocation (300,400); F.setsize (300,200); F.setbackground (NewColor (204,204,255)); Panel P1=NewPanel (NewBorderLayout ()); Panel P2=NewPanel (NewBorderLayout ()); Panel P11=NewPanel (NewGridLayout (2,1)); Panel P21=NewPanel (NewGridLayout (2,2)); P1.add (NewButton ("button"), borderlayout.west); P1.add (NewButton ("button"), borderlayout.east); P11.add (NewButton ("button")); P11.add (NewButton ("button"));    P1.add (P11,borderlayout.center); P2.add (NewButton ("button"), borderlayout.west); P2.add (NewButton ("button"), borderlayout.east);  for(intI =1;i<=4;i++) {P21.add (NewButton ("button"));}    P2.add (P21,borderlayout.center);    F.add (p1); F.add (p2); F.setvisible (true); }}
tenbuttons

GUI programming of J2SE

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.