Вы находитесь на странице: 1из 52

AWT

Abstract Window Toolkit (AWT) library of java package Contains 25 packages with hundreds of classes Used for GUI Import java.awt.*;

Object

Component

Text Component

Button

Container

Check Box

Menu Component

Text Field

Text Area

Panel

Window

Menu Bar

Menu

Menu Item

Applet

Frame

Dialog

COMPONENT
Component is an object that can be displayed on screen Has visual representation Component has spl properties like backcolor and font

FRAME
Frame class provides window for applets and applications

import java.awt.*; class frame { public static void main(String args[]) { Frame f=new Frame("This is my frame"); f.setBackground(Color.red); f.setSize(300,100); f.setVisible(true); f.setCursor(Frame.HAND_CURSOR); System.out.println("Return to DOS prompt & press ctrl+c for Quit"); } }

Button Class
A Button is a component that has a label and can respond when pressed Syntax:
Button() Construct a button with no label Button(String label) Construct a button with specified label.

Methods:
setLabel(String label) sets the label to a specified string getLabel() gets label from button.

import java.awt.*; class button { public static void main(String args[]) { Button b; Frame f=new Frame("This is my Button"); f.setBackground(Color.blue); f.setSize(50,50); b=new Button("press me"); f.add(b); f.setLocation(200,200); f.setVisible(true); f.setCursor(Frame.HAND_CURSOR); System.out.println("Return to DOS prompt & press ctrl+c for Quit"); } }

Button fills entire Frame. Frames default manager is always border layout and its default constraint is BorderLayout.CENTER.

LAYOUT MANAGERS
Arrangement of several components within a container is called Layout Java borrowed their component from window 5 layout classes
FlowLayout GridLayout BorderLayout CardLayout GridBagLayout

Flow Layout
Arrange the component from upperleft corner, left to right and top-bottom Small space between every component Constructors for flow layout
FlowLayout(); ->default constructor FlowLayout(int align);
FlowLayout.LEFT FlowLayout.CENTER FlowLayout.RIGHT

FlowLayout(int align, int horizontal, int vertical)


Vertical and horizontal space

import java.awt.*; class flow { public static void main(String args[]) { Frame f=new Frame(); f.setSize(300,140); f.setLayout(new FlowLayout(FlowLayout.CENTER)); for(int i=1;i<=9;i++) { f.add(new Button("Button No"+i)); } f.setVisible(true); } }

Grid Layout
Organizes the display into a rectangular grid
GridLayout(int rows,int cols);

All the components are of equal size

import java.awt.*; class gd extends Frame { gd(String s) { super(s); setSize(300,140); setLayout(new GridLayout(3,4)); for(int i=1;i<=10;i++) add(new Button("Button No"+i)); setVisible(true); } } class grid { public static void main(String args[]) { gd g=new gd("Grid Layout"); } }

import java.awt.*; import java.applet.*; public class grid extends Applet { public void init() { setLayout(new GridLayout(3,3)); setFont(new Font("SansSerif",Font.PLAIN,24)); for(int i=0;i<9;i++) { int no=i*3; add(new Button(""+no)); } } }

Border Layout
Provides you to position components in directions Constructors
BorderLayout(); BorderLayout(int horizontal,int vertical);

5 constraints
BorderLayout.EAST BorderLayout.WEST BorderLayout.NORTH BorderLayout.SOUTH BorderLayout.CENTER

import java.awt.*; import java.applet.*; class border { public static void main(String args[]) { Frame f=new Frame("BORDER"); f.add(new Button("North"), BorderLayout.NORTH); f.add(new Button("East"), BorderLayout.EAST); f.add(new Button("west"), BorderLayout.WEST); f.add(new Button("South"), BorderLayout.SOUTH); f.add(new Button("Center"), BorderLayout.CENTER); f.setVisible(true); } }

Insets
To leave small space between container hold value and the window that contains it To do this, use getInsets()
Insets(int top, int left, int bottom,int right);

import java.applet.*; import java.awt.*; public class inset extends Applet { Button b1,b2,b3; public void init() { setBackground(Color.cyan); setLayout(new BorderLayout()); add(new Button("RMI"),BorderLayout.NORTH); add(new Button("Servlet"), BorderLayout.EAST); add(new Button("Servlet"), BorderLayout.WEST); add(new Button("Servlet"), BorderLayout.SOUTH); add(new Button("Servlet"), BorderLayout.CENTER); } public Insets getInsets() { return new Insets(10,20,10,20); } }

canvas
-component that has no default appearance For custom drawing regions and work areas setSize() to make preferred size of canvas
Canvas();

import java.applet.*; import java.awt.*; public class can extends Applet {


public void init() { setBackground(Color.cyan); setLayout(new FlowLayout(FlowLayout.RIGHT)); Canvas c=new Canvas(); c.setBackground(Color.blue); c.setSize(100,100); add(c); } }

Label
Label do not respond to user input Do not send any other events Syntax:
Label(); Label(String caption); Label(String caption,alignment); Label.CENTER Label.RIGHT Label.LEFT

Alignment

2 methods
getText(); setText();

Text Field
To accept line of textual data from user Subclass of text component Constructor
TextField() TextField(String text); TextField(String text,int n);

Methods
getText() setText() setEchoChar(char c);

import java.applet.*; import java.awt.*; public class text extends Applet { public void init() { setLayout(new GridLayout(2,2)); Label l1=new Label("Enter the user Name",Label.CENTER); TextField t1=new TextField(); Label l2=new Label("Enter your password",Label.CENTER); TextField t2=new TextField(10); t2.setEchoChar('*'); add(l1); add(t1); add(l2); add(t2); } }

Text Area
Handles multilines of text Constructor
TextArea(); TextText(String text); TextArea(int row, int colunm); TextArea(String text,int row,int cols,int scrollbasconstant);

import java.applet.*; import java.awt.*; public class text1 extends Applet { public void init() { TextArea ta=new TextArea(5,8); Label l3=new Label("Text Area",Label.CENTER); ta.setText("this is\n is the \n text area \n with \n non editorial \n model");
add(l3); add(ta); ta.setEditable(false); } }

Methods
TextArea.SCROLLBARS_BOTH TextArea.SCROLLBARS_NONE TextArea.SCROLLBARS_HORIZONTAL_ONLY TextArea.SCROLLBARS_VERTICAL_ONLY

append(String s); Insert(String s,int position); setEditable(boolean b);

Checkbox
Used to create labeled checkbox Tow parts-caption and state Constructors
Checbox(String s); Checkbox(String s,initial state);

Methods
getState(); setState(Boolen state);

import java.applet.*; import java.awt.*; public class text1 extends Applet { public void init() { setLayout(new GridLayout(3,1)); TextArea ta=new TextArea(5,8); Label l3=new Label("Text Area",Label.CENTER); ta.setText("this is\n is the \n text area \n with \n non editorial \n model"); Checkbox c1=new Checkbox("Tamil",true); Checkbox c2=new Checkbox("English"); Checkbox c3=new Checkbox("Maths"); add(l3); add(ta); add(c1); add(c2); add(c3); ta.setEditable(false); } }

Check box group


Also known as option button or radio button Constructor
Checkbox cb=new Checkbox(String caption, group state);

Group refers tocheckboxGroup object


CheckboxGroup cbg=new CheckboxGroup();

Choice
Pull down list Called combo-box Constructor
Choice c=new Choice(); c.add(value);

Methods
int getItemCount()-returns no of items Void select(int position) String getItem(int Index);

import java.applet.*; import java.awt.*; public class text1 extends Applet { public void init() { setLayout(new GridLayout(3,1)); CheckboxGroup cbg=new CheckboxGroup(); TextArea ta=new TextArea(5,8); Label l3=new Label("Text Area",Label.CENTER); ta.setText("this is\n is the \n text area \n with \n non editorial \n model"); Checkbox c1=new Checkbox("Tamil",cbg,true); Checkbox c2=new Checkbox("English",cbg,false); Checkbox c3=new Checkbox("Maths",cbg,false); Choice color=new Choice(); color.add("red"); color.add("blue"); color.add("yellow"); add(color); add(l3); add(ta); add(c1); add(c2); add(c3); ta.setEditable(false);

List
Allows you to create a scrollinglist of values List(int number of rows,boolean multiple);

import java.applet.*; import java.awt.*; public class text1 extends Applet { public void init() { setLayout(new GridLayout(3,1)); List icecream=new List(3,true); icecream.add("chocolate"); icecream.add("orange"); icecream.add("mint chip"); icecream.add("venilla"); add(icecream); } }

Menu
2 types : pull down and pop up Steps for creating pull down menu
Create an object for menubar class
MenuBar main=new MenuBar();

Attach menubar frame


setMenuBar(main);

Create menu class for each menu you want on menu bar
Menu file=new Menu(File);

Add menu to menubar


main.add(file);

Create an objects for menu item


MenuItem new=new MenuItem(New); MenuItem open=new MenuItem(Open);

Add the menu item to menu class


file.add(new); File.add(open);

import java.applet.*; import java.awt.*; public class text1 extends Applet { public static void main(String args[]) { Frame f=new Frame(); MenuBar main=new MenuBar(); f.setMenuBar(main); Menu file=new Menu("file"); Menu edit=new Menu("edit"); main.add(file); main.add(edit); MenuItem new1=new MenuItem("New"); MenuItem open=new MenuItem("open"); MenuItem line=new MenuItem("-"); CheckboxMenuItem print=new CheckboxMenuItem("Print");

MenuItem exit=new MenuItem("Exit"); file.add(new1); file.add(open); file.add(line); file.add(print); file.add(exit); MenuItem cut=new MenuItem("cut"); MenuItem undo=new MenuItem("undo"); Menu more=new Menu("More"); edit.add(cut); edit.add(undo); undo.setEnabled(false); edit.add(more); more.add("commands"); f.setSize(200,200); f.setVisible(true); } }

Event Handling
When user clicks a button, performs some action Syntax:
button1.add ActionListener(this);
this keyword refers to current object Catch the events and pass it to action performed() method

Public void actionPerformed(ActionEvent ae) { }

import java.applet.*; import java.awt.*; import java.awt.event.*; public class text1 extends Applet implements ActionListener { TextField t1,t2,t3; Button add1; public void init() { setLayout(new GridLayout(3,1)); t1=new TextField(); t2=new TextField(); t3=new TextField(); add(t1); add(t2); add(t3); add1=new Button("+"); add(add1); add1.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource()==add1) { int sum=Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText()); t3.setText(String.valueOf(sum)); } } }

Вам также может понравиться