Java Learning 9:AWT Programming of essays

Source: Internet
Author: User
Tags save file

namespace import java.awt.*;

1. Create a frame window

Frame f = new frame ("Test window"); F.setbounds (100,100, 300,300); f.setvisible (true);

2. Create a panel and add elements:

panel p = new Panel ();p. Add (new TextField);p. Add (New button ("click Me");        F.add (P);

3.ScrollPane scroll bar Pane

ScrollPane sp = new ScrollPane (scrollpane.scrollbars_always); Sp.add (new TextField); Sp.add (New button ("Click Me") ;        F.add (SP);

Layout Manager

    1. FlowLayout--arranges components from left to right, encounters a line automatically
F.setlayout (New FlowLayout (Flowlayout.left, 20,10));        F.pack ();     Automatically adapts to screen size

2. BorderLayout (Frame, Dialog, ScrollPane Use this layout by default)

Divide the container into five regions in the cardinal and the old, add the element to specify which area to add to, if not specified, the default is added to the region, and the added element overwrites the previously added element

               F.setlayout (New BorderLayout (30,5)), F.add (New button ("South"), Borderlayout.south), F.add (The New button ("North"), Borderlayout.north); panel p = new Panel ();p. Add (new TextField);p. Add (New button ("click Me"); F.add (P); F.add (New button ("EAST"), Borderlayout.east);        

3. GridLayout Grid Layout, default from left to right, top to bottom, each component size is determined by the component area

panel p1 = new Panel ();p 1.add (new TextField); F.add (P1, Borderlayout.north); panel P2 = new Panel ();p 2.setLayout (New GridLayout (3,5, bis));    3 Rows 5 Columns string[] name = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", "."}; for (int i=0; i<name.length; i++) {p2.add (new Button (Name[i]));} F.add (p2);

4. GridBagLayout A component can span one or more meshes and set the size of each mesh to be different.

5. All components are considered a stack of cards, each time only the topmost one is visible

6. BoxLayout can put components in vertical or horizontal direction

Basic components: (Reference namespace import java.awt.*; )

Frame f = new frame ("Test window"); Button OK = New button ("Confirm");//define a checkbox groupcheckboxgroup CBG = new CheckboxGroup ();//Define a radio box (inside the CBG Group), and the initial check C Heckbox male = new Checkbox ("Man", CBG, True); checkbox female = new checkbox ("Female", CBG, false);  CheckBox married = new CheckBox ("is married?", false);//define a drop-down box Choice colorchooser = new Choice ();//define a list colorlist = New List (6, true);//defines a 5-row 20-column multi-text TextArea ta = new TextArea (5, 20);//defines a 50 column of single-text TextField name= new TextField (c); Olorchooser.add ("Red"); Colorchooser.add ("Yellow"); Colorchooser.add ("Blue"); Colorlist.add ("Red"); Colorlist.add ( "Yellow"); Colorlist.add ("Blue"); Panel bottom = new Panel (); Bottom.add (name); Bottom.add (OK); F.add (bottom, Borderlayout.south); Panel checkpanel = new Panel (); Checkpanel.add (Colorchooser); Checkpanel.add (male); Checkpanel.add (female); Checkpanel.add (married);//Create a vertical column component box, Dovenben (box requires a reference namespace: import javax.swing.*;) box TopLeft = Box.createverticalbox (); Topleft.add (TA); Topleft.add (Checkpanel);/Create a horizontal box box top = Box.createhorizontalbox (); Top.add (TopLeft); Top.add (colorlist); F.add (top); F.pack (); F.setvisible (TRUE);

Dialog

Dialog

Dialog D1=new Dialog (F, "title", true); F is the parent class, "title" is dialog title, True is pattern, true is pattern dialog, false is non-modal dialog

FileDialog is a sub-class of Dialog:

FileDialog d1=new FileDialog (F, "Choose the File", filedialog.load);                      FileDialog d2 = new FileDialog (f, "Save the File", Filedialog.save); Button B1 = New button ("Open file"); Button B2 = New button ("Save file"); B1.addactionlistener (e-> {d1.setvisible (true); System.out.println (d1.getdirectory () + d1.getfile ());}); B2.addactionlistener (E->{d2.setvisible (TRUE); System.out.println (d2.getdirectory () + d2.getfile ());}); F.add (B1); F.add (B2, Borderlayout.south);

Event Handling (Reference namespaces:

Import java.awt.event.WindowEvent;

Import Java.awt.event.WindowAdapter;

Import Java.awt.event.WindowListener; )

ActionListener actionperformed button text box menu item to start when clicked

Adjustmentlistener adjustmentvaluechanged when the slider position changes

KeyListener keypressed Trigger When a key is pressed

TextListener textvaluechanged

ItemListener itemstatechanged

WindowListener windowactivated, Windowclosing (click on the "X" button to trigger the upper right corner)

    Class MyListener extends Windowadapter    (adapter) {public void windowclosing (WindowEvent e) {ta.append ("User closed the window! \ n "); System.exit (0);}} F.addwindowlistener (New MyListener ());//anonymous Inner class implementation F.addwindowlistener (new Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);}});

 awt menu MenuBar, Menu, MenuItem, menushortcut

MenuBar MB = new MenuBar (); Menu file = new Menu ("file"); Menu edit = new Menu ("edit"); MenuItem newitem = new MenuItem ("Create"); MenuItem SaveItem = new MenuItem ("Save"); MenuItem Exititem = new MenuItem ("Exit", New Menushortcut (keyevent.vk_x)); File.add (NewItem); File.add (SaveItem); File.add (Exititem); Checkboxmenuitem autowrap = new Checkboxmenuitem ("Auto Wrap"); MenuItem CopyItem = new MenuItem ("Copy"), Edit.add (AutoWrap); Edit.addseparator (); Edit.add (CopyItem);                                        ActionListener Menulistener = e->      //Add listener event {String cmd = E.getactioncommand (); if (Cmd.equals ("Exit")) { System.exit (0);}; Exititem.addactionlistener (Menulistener); File.add (edit);                     Mb.add (file);//mb.add (edit); F.setmenubar (MB);

Drawing using the Graphics class:

DrawLine () Drawing lines

DrawString () draw a string

DrawRect () Draw a rectangle

Drawroundrect () Draw rounded rectangles

DrawOval (); Draw Ellipse

DrawPolygon () Drawing polygons

DrawArc () Draw an arc

DrawPolyline () draw a polyline

FillRect () Fill Rectangle

Fillroundrect () fills a rounded rectangle

Filloval (); Fill ellipse

FillPolygon () fills a polygon

Fillarc () Fill arc

DrawImage () Drawing bitmaps

Class MyCanvas extends Canvas {public void paint (Graphics g) {Random Rand = new Random (), if (Shape.equals (Rect_shape)) {G. SetColor (New Color (220, 100,80)); Font font = new Font ("Arial", Font.Bold,//g.setcolor (color.red); SetBackground (new Color (0,255,0)); G.setfont (font ); G.drawrect (Rand.nextint), Rand.nextint (+), 40,60);          G.drawline (Rand.nextint), Rand.nextint (+), 40,60), g.DrawString ("Graphics method", 20,40); G.drawroundrect (50, G.drawarc (Rand.nextint), Rand.nextint (120), 120, 100, 200, 150, etc.); }if (Shape.equals (Oval_shape)) {G.setcolor (new Color (80,100,120)); G.filloval (Rand.nextint), Rand.nextint (120), 80,60);} } }

Java Learning 9:AWT Programming of essays

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.