Java Learning Notes (44)-AWT components (Abstract Window Toolkit)

Source: Internet
Author: User

Frame Form Component
ImportJava.awt.Color;ImportJava.awt.Frame;ImportJava.awt.event.WindowAdapter;ImportJava.awt.event.WindowEvent;/ * Frame Form component * / Public  class Test01 {     Public Static void Main(string[] args) {Frame frame=NewFrame ("My Forms");//Create a form        /*frame.setsize (300, 300);//Set the form size frame.setlocation (250, 250);//Set the position of the form to appear */Frame.setbounds ( -, -, -, -);//Set position and size at the same timeFrame.setbackground (color.red);//Set the form background colorFrame.setlocationrelativeto (NULL);//Setup form appears in the middleFrame.setvisible (true);//Display form        //Add listener events to close the form when you click Close in the upper right cornerFrame.addwindowlistener (NewWindowadapter () {@Override             Public void windowclosing(WindowEvent e) {System.exit (0);//exit the program when the button is clicked}        }); }}
Panel Panels Component
 import java.awt.Color; import java.awt.Dimension; import java.awt.Frame; import java.awt.Panel;/ * Panel Panels component * / Public  class Test02 {     Public Static voidMain (string[] args) {Frame frame =NewFrame ("My Frame"); Panel pnl=NewPanel ();//Create panelPnl.setsize (NewDimension ( -, -));        Pnl.setbackground (Color.yellow); Frame.add (PNL);//Add a panel to a formFrame.setsize ( $, $);        Frame.setbackground (Color.gray); Frame.setlocationrelativeto (NULL); Frame.setlayout (NULL);//Cancel layout manager with manual layoutFrame.setvisible (true); }}
Label label and TextField single-line text box
ImportJava.awt.FlowLayout;ImportJava.awt.Frame;ImportJava.awt.Label;ImportJava.awt.TextField;/* Label label and TextField single-line text box */ Public  class Test07 extends Frame{Label lblusername=NewLabel ("User name:"); TextField txtusername=NewTextField (Ten); Public Test07(String title) {Settitle (title);//Call the parent class's construction method, set the captionSetLayout (NewFlowLayout ()); Add (Lblusername);//Add a label to the formAdd (txtUserName);//Add a text box to a formSetSize ( $, $); } Public Static void Main(string[] args) {Test07 test=NewTest07 ("My Forms"); Test.setvisible (true); }}
TextArea multi-line text boxes and button buttons
/* * TEXTAREA multi-line text boxes and button buttons */ Public  class Test08 extends Frame{TextArea txtcomment =NewTextArea (5, -); Button btnok=NewButton ("OK"); Button btncancel=NewButton ("Cancel"); Public Test08(String title) {Super(title); Init ();//Initialize} Public void Init() {setlayout (NewFlowLayout ());        Add (txtcomment);        Add (Btnok);        Add (Btncancel); SetSize ( $, $); Setlocationrelativeto (NULL); SetVisible (true); } Public Static void Main(string[] args) {NewTest08 ("My little apple."); }}
Title
/* checkbox check box and CheckboxGroup */ Public  class Test09extends Frame{  Checkbox hobby1=NewCheckbox ("Dinner"); Checkbox hobby2=NewCheckbox ("Sleeping"); Checkbox hobby3=NewCheckbox ("hit the Peas.");//Create check box Group, add check box to group, change to radio buttonCheckboxGroup sexgroup=NewCheckboxGroup (); Checkbox male=NewCheckbox ("Male",true, Sexgroup); Checkbox female=NewCheckbox ("female",false, Sexgroup); Label lblhobby=NewLabel ("Hobby:"); Label lblsex=NewLabel ("Gender:"); Public Test09(String title) {Super(title);    Init (); } Public void Init() {setlayout (NewGridLayout (7,1));        Add (Lblhobby);        Add (hobby1);        Add (hobby2);        Add (HOBBY3);        Add (Lblsex);        Add (male);    Add (female); } Public Static void Main(string[] args) {Test09 test=NewTest09 ("My Forms"); Test.setsize ( $, $); Test.setlocationrelativeto (NULL); Test.setvisible (true); }}
Choice selection box and list box
ImportJava.awt.Choice;ImportJava.awt.Frame;ImportJava.awt.GridLayout;ImportJava.awt.Label;ImportJava.awt.List;/* * Choice Select box and list box */ Public  class Test10 extends Frame{Label lblfruit=NewLabel ("Your favorite fruit:"); Label lblstar=NewLabel ("Your favorite star:"); Choice fruits=NewChoice (); List starts=NewList (2,true); List LST =NewList (4,false); Public Test10(String title) {Super(title); SetLayout (NewGridLayout (2,2)); Fruits.add ("Little Apple"); Fruits.add ("Jackfruit"); Fruits.add ("Banana Fanshi"); Starts.add ("Lin Chi-ling"); Starts.add ("Liu Yifei"); Starts.add ("Wan Liqing"); Lst.add ("Mercury"); Lst.add ("Venus"); Lst.add ("Earth"); Lst.add ("JavaSoft"); Lst.add ("Mars"); Lst.add ("Jupiter"); Lst.add ("Saturn"); Lst.add ("Uranus"); Lst.add ("Neptune"); Lst.add ("Pluto");        Add (Lblfruit);        Add (fruits);        Add (Lblstar); Add (starts);//add (LST);} Public Static void Main(string[] args) {Test10 test=NewTest10 ("hehe"); Test.setsize ( -, -); Test.setlocationrelativeto (NULL); Test.setvisible (true); }}
MenuBar, Menu, MenuItem
 import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.MenuShortcut; import java.awt.event.KeyEvent;/ * * MenuBar, Menu, MenuItem * / Public  class Test11 {     Public Static voidMain (string[] args) {Frame frame=NewFrame ("My Forms");//Create menu barMenuBar mymenu=NewMenuBar ();//Create a menuMenu menu1=NewMenu ("File"); Menu menu2=NewMenu ("Edit");//Create menu itemMenushortcut newms=NewMenushortcut (Keyevent.vk_n,true);//Set shortcut keysMenuItem item1=NewMenuItem ("New", newms); MenuItem item2=NewMenuItem ("Close"); MenuItem item3=NewMenuItem ("Save"); MenuItem item4=NewMenuItem ("Copy"); MenuItem item5=NewMenuItem ("Cut"); MenuItem item6=NewMenuItem ("Paste");//Add menu items to menusMenu1.add (ITEM1);        Menu1.add (ITEM2);        Menu1.add (ITEM3);        Menu2.add (ITEM4);        Menu2.add (ITEM5); Menu2.add (ITEM6);//Add a menu to the menu barMymenu.add (MENU1);        Mymenu.add (MENU2); Frame.setlayout (NewFlowLayout ()); Frame.setsize ( $, $); Frame.setlocationrelativeto (NULL); Frame.setmenubar (MyMenu);//Set menu bar for formFrame.setvisible (true); }}

Java Learning Notes (44)-AWT components (Abstract Window Toolkit)

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.