Unit 5 java-awt (1)

jennifer.746 1,269 views 48 slides Jan 05, 2021
Slide 1
Slide 1 of 48
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48

About This Presentation

AWT Components
By:Deva Kumari,Asst Professor, Kristu Jayanti College,Autonomous


Slide Content

Java AWT

Java AWT (Abstract Window Toolkit) is an API to develop GUI based applications in java . Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e. its components are using the resources of OS . The java.awt  package provides classes for AWT api such as  TextField , Label,  TextArea , RadioButton ,  CheckBox ,  List etc.

The Container is a component in AWT that can contain another components like buttons, textfields , labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel . The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window . The Panel is the container that doesn't contain menu bars. It can have other components like button, textfield etc . The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc.

1 Label: A Label object is a component for placing text in a container. Button:This class creates a labeled button. Check Box: A check box is a graphical component that can be in either an on (true) or off (false) state. Check Box Group: The CheckboxGroup class is used to group the set of checkbox. List: The List component presents the user with a scrolling list of text items. Text Field: A TextField object is a text component that allows for the editing of a single line of text. Text Area : A TextArea object is a text component that allows for the editing of a multiple lines of text. Choice: A Choice control is used to show pop up menu of choices. Selected choice is shown on the top of the menu. Canvas: A Canvas control represents a rectangular area where application can draw something or can receive inputs created by user. Image:An Image control is superclass for all image classes representing graphical images. Scroll Bar: A Scrollbar control represents a scroll bar component in order to enable user to select from range of values.

Method Description public void add(Component c) inserts a component. public void setSize(int width,int height) sets the size (width and height) of the component. public void setLayout(LayoutManager m) defines the layout manager for the component. public void setVisible ( boolean status) changes the visibility of the component, by default false.

we can create a frame in AWT in two ways. By extending Frame class (inheritance) By creating the object of Frame class (association)

AWT Example by Inheritance import java.awt .*; import java.awt.event .*; class FrameEx1 extends Frame{ FrameEx1(){ Button b=new Button("click me"); b.setBounds (30,100,80,30);// setting button position add(b );//adding button into frame setSize (300,300 );//frame size 300 width and 300 height setLayout (null );//no layout manager setVisible (true );//now frame will be visible, by default not visible addWindowListener (new WindowAdapter () { public void windowClosing ( WindowEvent windowEvent ){ System.exit (0); } }); } public static void main(String args []){ FrameEx1 f=new FrameEx1(); } }

AWT Example by Association import java.awt .*; class FrameEx2{ FrameEx2(){ Frame f=new Frame(); Button b=new Button("click Here"); b.setBounds (30,50,80,30 ); f.add (b ); f.setSize (300,300 ); f.setLayout (null ); f.setVisible (true ); } public static void main(String args []){ FrameEx2 f=new FrameEx2 (); } }

Event Classes Listener Interfaces ActionEvent ActionListener ItemEvent ItemListener TextEvent TextListener AdjustmentEvent AdjustmentListener WindowEvent WindowListener ComponentEvent ComponentListener ContainerEvent ContainerListener FocusEvent FocusListener

For registering the component with the Listener, many classes provide the registration methods. For example: Button public void addActionListener ( ActionListener a){} MenuItem public void addActionListener ( ActionListener a){} TextField public void addActionListener ( ActionListener a){} public void addTextListener ( TextListener a){} TextArea public void addTextListener ( TextListener a){} Checkbox public void addItemListener ( ItemListener a){} Choice public void addItemListener ( ItemListener a){} List public void addActionListener ( ActionListener a){} public void addItemListener ( ItemListener a){}

The button class is used to create a labeled button that has platform independent implementation. The application result in some action when the button is pushed . Syntax: public   class  Button  extends  Component  implements  Accessible  Constructors: Button () : Constructs a button with an empty string for its label . Button(String text) : Constructs a new button with specified label . Methods: void addActionListener ( ActionListener l) : Adds the specified action listener to receive action events from this button. void setLabel (String label) : Sets the button's label to be the specified string . String getLabel () : Gets the label of this button.

Label Class : The  object of Label class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly. Syntax:public   class  Label  extends  Component  implements  Accessible  Methods; int getAlignment (): Gets the current alignment of this label . String getText (): Gets the text of this label . void setAlignment ( int alignment) : Sets the alignment for this label to the specified alignment .[CENTER,LEFT,RIGHT] void setText (String text): Sets the text for this label to the specified text. Constructor 1 Label() Constructs an empty label. 2 Label(String text) Constructs a new label with the specified string of text, left justified. 3 Label(String text, int alignment) Constructs a new label that presents the specified string of text with the specified alignment.

The object of a TextField class is a text component that allows the editing of a single line text. It inherits TextComponent class. Syntax: public class   TextField   extends   TextComponent   Constructor: TextField () : Constructs a new text field . TextField ( int columns) : Constructs a new empty text field with the specified number of columns . TextField (String text) : Constructs a new text field initialized with the specified text . TextField (String text, int columns) : Constructs a new text field initialized with the specified text to be displayed, and wide enough to hold the specified number of columns . Method   : void addActionListener ( ActionListener l) : Adds the specified action listener to receive action events from this text field . int getColumns () : Gets the number of columns in this text field . void setColumns ( int columns) : Sets the number of columns in this text field . void setText (String t) : Sets the text that is presented by this text component to be the specified text.

import java.awt .*; import java.awt.event .*; public class ButtonExample { ButtonExample (){ Frame f=new Frame("Button Example"); Label l1; l1=new Label("First Label."); l1.setBounds(30,100, 100,30); final TextField tf =new TextField (); tf.setBounds (100,100, 100,20); Button b=new Button("Click Here"); b.setBounds (70,150,60,30); b.addActionListener (new ActionListener (){ public void actionPerformed ( ActionEvent e){ tf.setText ("Welcome to Java."); } }); f.addWindowListener (new WindowAdapter () { public void windowClosing ( WindowEvent windowEvent ){ System.exit (0); } }); f.add (b); f.add ( tf ); f.add (l1); f.setSize (400,400); f.setLayout (null); f.setVisible (true); }

The TextArea control in AWT provide us multiline editor area. When the text in the text area become larger than the viewable area the scroll bar is automatically appears which help us to scroll the text up & down and right & left . Constructor: TextArea () : Constructs a new text area with the empty string as text . TextArea ( int rows, int columns): Constructs a new text area with the specified number of rows and columns and the empty string as text. TextArea (String text): Constructs a new text area with the specified text . TextArea (String text, int rows, int columns) : Constructs a new text area with the specified text, and with the specified number of rows and columns . TextArea (String text, int rows, int columns, int scrollbars) Constructs a new text area with the specified text, and with the rows, columns, and scroll bar visibility as specified. static int SCROLLBARS_BOTH  -- Create and display both vertical and horizontal scrollbars. static int SCROLLBARS_HORIZONTAL_ONLY  -- Create and display horizontal scrollbar only. static int SCROLLBARS_NONE  -- Do not create or display any scrollbars for the text area. static int SCROLLBARS_VERTICAL_ONLY  -- Create and display vertical scrollbar only.

Method  void append(String str ) : Appends the given text to the text area's current text . int getColumns () : Returns the number of columns in this text area . void insert(String str , int pos ) : Inserts the specified text at the specified position in this text area . void replaceRange (String str , int start, int end) : Replaces text between the indicated start and end positions with the specified replacement text . void setColumns ( int columns) : Sets the number of columns for this text area. void setRows ( int rows) : Sets the number of rows for this text area . int getRows () : Returns the number of rows in the text area.

import java.awt .*; public class TextAreaExample { TextAreaExample (){ Frame f= new Frame(); TextArea area=new TextArea ("Welcome to KJC"); area.setBounds (10,30, 300,300); f.add (area); f.setSize (400,400); f.setLayout (null); f.setVisible (true); area.insert (“III B.Sc Java”,150,150); } public static void main(String args []) { new TextAreaExample (); } }

Checkbox control is used to turn an option on(true) or off(false). There is label for each checkbox representing what the checkbox does.The state of a checkbox can be changed by clicking on it . Class constructors: Checkbox() : Creates a check box with an empty string for its label . Checkbox(String label) : Creates a check box with the specified label . Checkbox(String label, boolean state) : Creates a check box with the specified label and sets the specified state . Checkbox(String label, boolean state, CheckboxGroup group) : Constructs a Checkbox with the specified label, set to the specified state, and in the specified check box group . Checkbox(String label, CheckboxGroup group, boolean state) : Creates a check box with the specified label, in the specified check box group, and set to the specified state.

Class methods: void addItemListener ( ItemListener l) : Adds the specified item listener to receive item events from this check box . String getLabel () : Gets the label of this check box . boolean getState () : Determines whether this check box is in the  on  or  off  state . void setState ( boolean state) ; Sets the state of this check box to the specified state .

import java.awt .*; public class CheckboxExample { CheckboxExample (){ Frame f= new Frame("Checkbox Example"); Checkbox checkbox1 = new Checkbox("C++"); checkbox1.setBounds(100,100, 50,50); Checkbox checkbox2 = new Checkbox("Java", true); checkbox2.setBounds(100,150, 50,50); f.add (checkbox1); f.add (checkbox2); f.setSize (400,400); f.setLayout (null); f.setVisible (true); } public static void main(String args []) { new CheckboxExample (); } }

The CheckboxGroup class is used to group the set of checkbox . Constructor: CheckboxGroup (): Creates a new instance of CheckboxGroup . Methods: Checkbox getSelectedCheckbox () : Gets the current choice from this check box group . void setSelectedCheckbox (Checkbox box) : Sets the currently selected check box in this group to be the specified check box.

Choice control is used to show pop up menu of choices. Selected choice is shown on the top of the menu . Constructor: Choice (): Creates a new choice menu . Methods: void add(String item) : Adds an item to this Choice menu . void addItemListener ( ItemListener l) : Adds the specified item listener to receive item events from this Choice menu . void insert(String item, int index) : Inserts the item into this choice at the specified position . void remove( int position) : Removes an item from the choice menu at the specified position . void remove(String item) : Removes the first occurrence of item from the Choice menu.

The List represents a list of text items. The list can be configured that user can choose either one item or multiple items . Constructors: List (): Creates a new scrolling list . List( int rows): Creates a new scrolling list initialized with the specified number of visible lines . List( int rows, boolean multipleMode ): Creates a new scrolling list initialized to display the specified number of rows . Methods: void add(String item) : Adds the specified item to the end of scrolling list . void add(String item, int index) : Adds the specified item to the the scrolling list at the position indicated by the index. void addItemListener ( ItemListener l) : Adds the specified item listener to receive item events from this list . String getItem ( int index): Gets the item associated with the specified index . int getItemCount (): Gets the number of items in the list.

public class ListExample { ListExample (){ Frame f= new Frame(); List l1=new List(5); l1.setBounds(100,100, 75,75); l1.add("Item 1"); l1.add("Item 2"); l1.add("Item 3"); l1.add("Item 4"); l1.add("Item 5"); f.add (l1); f.setSize (400,400); f.setLayout (null); f.setVisible (true); } public static void main(String args []) { new ListExample (); } }

The object of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a GUI component allows us to see invisible number of rows and columns. Constructor: Scrollbar (): Constructs a new vertical scroll bar . Scrollbar( int orientation): Constructs a new scroll bar with the specified orientation . Scrollbar( int orientation, int value, int visible, int minimum, int maximum):Constructs a new scroll bar with the specified orientation, initial value, visible amount, and minimum and maximum values . static int HORIZONTAL --A constant that indicates a horizontal scroll bar . static int VERTICAL --A constant that indicates a vertical scroll bar.

void addAdjustmentListener ( AdjustmentListener l) Adds the specified adjustment listener to receive instances of AdjustmentEvent from this scroll bar . int getMaximum (): Gets the maximum value of this scroll bar . int getMinimum (): Gets the minimum value of this scroll bar. int getOrientation (): Returns the orientation of this scroll bar . int getValue () : Gets the current value of this scroll bar . void setMaximum ( int newMaximum ): Sets the maximum value of this scroll bar . void setMinimum ( int newMinimum ): Sets the minimum value of this scroll bar . void setOrientation ( int orientation): Sets the orientation for this scroll bar.

Layout Manager Layout is the arrangement of components within the container. i.e >placing the components at a particular position within the container . The task of layouting the controls is done automatically by the Layout Manager .  If we do not use layout manager then also the components are positioned by the default layout manager .   The layout managers adapt to the dimensions of appletviewer or the application window. The layout manager is associated with every Container object.

The class BorderLayout arranges the components to fit in the five regions: east, west, north, south and center . Each region can contain only one component and each component in each region is identified by the corresponding constant NORTH, SOUTH, EAST, WEST, and CENTER Constructor: BorderLayout ():Constructs a new border layout with no gaps between components . BorderLayout ( int hgap , int vgap ): Constructs a border layout with the specified gaps between components . static String CENTER  -- The center layout constraint (middle of container). static String EAST  -- The east layout constraint (right side of container ). static String NORTH  -- The north layout constraint (top of container ). static String SOUTH  -- The south layout constraint (bottom of container). static String WEST  -- The west layout constraint (left side of container).

void addLayoutComponent (String name, Component comp): If the layout manager uses a per-component string, adds the component comp to the layout, associating it with the string specified by name. int getHgap (): Returns the horizontal gap between components. int getVgap () :Returns the vertical gap between components. void setHgap ( int hgap ): Sets the horizontal gap between components . void setVgap ( int vgap ): Sets the vertical gap between components.

import java.awt .*; public class Border { Frame f; Border(){ f=new Frame (); Button b1=new Button ("NORTH");; Button b2=new Button ("SOUTH");; Button b3=new Button ("EAST");; Button b4=new Button ("WEST");; Button b5=new Button ("CENTER");; f.add (b1,BorderLayout.NORTH); f.add (b2,BorderLayout.SOUTH); f.add (b3,BorderLayout.EAST); f.add (b4,BorderLayout.WEST); f.add (b5,BorderLayout.CENTER); f.setSize (300,300); f.setVisible (true); } public static void main(String[] args ) { new Border(); } }

The GridLayout is used to arrange the components in rectangular grid. One component is displayed in each rectangle . Constructors: GridLayout (): creates a grid layout with one column per component in a row. GridLayout ( int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components. GridLayout ( int rows, int columns, int hgap , int vgap ): creates a grid layout with the given rows and columns alongwith given horizontal and vertical gaps.

import java.awt .*; import javax.swing .*; public class GridLayoutEx { Frame f; GridLayoutEx (){ f=new JFrame (); Button b1=new Button ("1"); Button b2=new Button ("2"); Button b3=new Button ("3"); Button b4=new Button ("4"); Button b5=new Button ("5"); Button b6=new Button ("6"); Button b7=new Button ("7"); Button b8=new Button ("8"); Button b9=new Button ("9"); f.add (b1); f.add (b2); f.add (b3); f.add (b4); f.add (b5); f.add (b6); f.add (b7); f.add (b8); f.add (b9); f.setLayout (new GridLayout (3,3)); //setting grid layout of 3 rows and 3 columns f.setSize (300,300); f.setVisible (true); } public static void main(String[] args ) { new GridLayoutEx (); } }

The FlowLayout is used to arrange the components in a line, one after another (in a flow). It is the default layout of applet or panel. Fields of FlowLayout class public static final int LEFT public static final int RIGHT public static final int CENTER public static final int LEADING public static final int TRAILING Constructors of FlowLayout class: FlowLayout (): creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap. FlowLayout ( int align): creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap. FlowLayout ( int align, int hgap , int vgap ): creates a flow layout with the given alignment and the given horizontal and

import java.awt .*; public class FlowLayoutEx { Frame f; FlowLayoutEx (){ f=new Frame (); Button b1=new Button ("1"); Button b2=new Button ("2"); Button b3=new Button ("3"); Button b4=new Button ("4"); Button b5=new Button ("5"); f.add (b1); f.add (b2); f.add (b3); f.add (b4); f.add (b5); f.setLayout (new FlowLayout ( FlowLayout.RIGHT )); //setting flow layout of right alignment f.setSize (300,300); f.setVisible (true); } public static void main(String[] args ) { new FlowLayoutEx (); } }

The CardLayout class manages the components in such a manner that only one component is visible at a time. It treats each component as a card that is why it is known as CardLayout . Constructors of CardLayout class CardLayout (): creates a card layout with zero horizontal and vertical gap. CardLayout ( int hgap , int vgap ): creates a card layout with the given horizontal and vertical gap. methods of CardLayout class public void next(Container parent): is used to flip to the next card of the given container. public void previous(Container parent): is used to flip to the previous card of the given container. public void first(Container parent): is used to flip to the first card of the given container. public void last(Container parent): is used to flip to the last card of the given container. public void show(Container parent, String name): is used to flip to the specified card with the given name.

import java.awt .*; import java.awt.event .*; Import java.swing .*; public class CardLayoutExample extends Jrame implements ActionListener { CardLayout card; Button b1,b2,b3; Container c; CardLayoutExample (){ c= getContentPane (); card=new CardLayout (40,30); //create CardLayout object with 40 hor space and 30 ver space c.setLayout (card); b1=new Button ("Apple"); b2=new Button ("Boy"); b3=new Button ("Cat"); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); c.add ("a",b1); c.add ("b",b2); c.add ("c",b3); } public void actionPerformed ( ActionEvent e) { card.next (c); } public static void main(String[] args ) { CardLayoutExample cl=new CardLayoutExample (); cl.setSize (400,400); cl.setVisible (true); cl.setDefaultCloseOperation (EXIT_ON_CLOSE); } }

The Java GridBagLayout class is used to align components vertically, horizontally or along their baseline . The components may not be of same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells. The GridBagLayout manages each component's minimum and preferred sizes in order to determine component's size . Constructor: GridBagLayout (): Creates a grid bag layout manager . Method : void addLayoutComponent (Component comp, Object constraints):Adds the specified component to the layout, using the specified constraints object . void addLayoutComponent (String name, Component comp): Adds the specified component with the specified name to the layout . void removeLayoutComponent (Component comp):Removes the specified component from this layout.

Menu Bars and Menus The object of MenuItem class adds a simple labeled menu item on menu. The items used in a menu must belong to the MenuItem or any of its subclass . Constructor  : MenuBar () : Creates a new menu bar . Method  : Menu add(Menu m) : Adds the specified menu to the menu bar . Menu getHelpMenu ():Gets the help menu on the menu b Menu getMenu ( int i ):Gets the specified menu . int getMenuCount (): Gets the number of menus on the menu bar .

import java.awt .*; class MenuExample { MenuExample (){ Frame f= new Frame("Menu and MenuItem Example"); MenuBar mb =new MenuBar (); Menu menu=new Menu("Menu"); Menu submenu=new Menu("Sub Menu"); MenuItem i1=new MenuItem ("Item 1"); MenuItem i2=new MenuItem ("Item 2"); MenuItem i3=new MenuItem ("Item 3"); MenuItem i4=new MenuItem ("Item 4"); menu.add (i1); menu.add (i2); submenu.add (i3); submenu.add (i4); menu.add (submenu); mb.add (menu); f.setMenuBar ( mb ); f.setSize (400,400); f.setLayout (null); f.setVisible (true); } public static void main(String args []) { new MenuExample (); } }

The Dialog control represents a top level window with a border and a title used to take some form of input from the user. It inherits the Window class. Modal dialog or modeless Unlike Frame, it doesn't have maximize and minimize buttons. Dialog(Dialog owner): Constructs an initially invisible, modeless Dialog with the specified owner Dialog and an empty title. Dialog(Dialog owner, String title):Constructs an initially invisible, modeless Dialog with the specified owner Dialog and title. Dialog(Frame owner):Constructs an initially invisible, modeless Dialog with the specified owner Frame and an empty title . Dialog(Frame owner, boolean modal):Constructs an initially invisible Dialog with the specified owner Frame and modality and an empty title . Dialog(Frame owner, String title):Constructs an initially invisible, modeless Dialog with the specified owner Frame and title.

import java.awt .*; import java.awt.event .*; public class DialogExample { private static Dialog d; DialogExample () { Frame f= new Frame(); d = new Dialog(f , "Dialog Example", true); d.setLayout ( new FlowLayout () ); Button b = new Button ("OK"); b.addActionListener ( new ActionListener () { public void actionPerformed ( ActionEvent e ) { DialogExample.d.setVisible (false); } }); d.add ( new Label ("Click button to continue.")); d.add (b); d.setSize (300,300); d.setVisible (true); } public static void main(String args []) { new DialogExample (); } }

FileDialog control represents a dialog window from which the user can select a file. static int LOAD -- This constant value indicates that the purpose of the file dialog window is to locate a file from which to read. static int SAVE -- This constant value indicates that the purpose of the file dialog window is to locate a file to which to write. Constructor: FileDialog (Dialog parent): Creates a file dialog for loading a file. FileDialog (Dialog parent, String title):Creates a file dialog window with the specified title for loading a file. FileDialog (Dialog parent, String title, int mode): Creates a file dialog window with the specified title for loading or saving a file. FileDialog (Frame parent): Creates a file dialog for loading a file . FileDialog (Frame parent, String title): Creates a file dialog window with the specified title for loading a file . FileDialog (Frame parent, String title, int mode): Creates a file dialog window with the specified title for loading or saving a file.

Method String getDirectory (): Gets the directory of this file dialog . String getFile (): Gets the selected file of this file dialog . int getMode () : Indicates whether this file dialog box is for loading from a file or for saving to a file . void setMode ( int mode) : Sets the mode of the file dialog.

import java.awt .*; import java.awt.event .*; public class FileControlDemo { Frame f = new Frame("Java AWT Examples "); Label l1=new Label(),l2=new Label(); Panel controlPanel =new Panel(); public FileControlDemo (){ prepareGUI (); } public static void main(String[] args ){ FileControlDemo awtControlDemo = new FileControlDemo (); awtControlDemo.showFileDialogDemo (); } private void prepareGUI (){ f f.setSize (400,400 ); f.setLayout (new GridLayout (3, 1)); f.addWindowListener (new WindowAdapter () { public void windowClosing ( WindowEvent windowEvent ){ System.exit (0); } }); l1.setAlignment( Label.CENTER ); l2.setAlignment( Label.CENTER ); l2.setSize(350,100 ); controlPanel.setLayout (new FlowLayout ()); f.add (l1); f.add ( controlPanel ); f.add (l2); f.setVisible (true); } private void showFileDialogDemo (){ l1.setText("Control in action: FileDialog "); final FileDialog fileDialog = new FileDialog ( f,"Select file"); Button showFileDialogButton = new Button("Open File"); showFileDialogButton.addActionListener (new ActionListener () { public void actionPerformed ( ActionEvent e) { fileDialog.setVisible (true); l2.setText("File Selected :" + fileDialog.getDirectory () + fileDialog.getFile ()); } }); controlPanel.add ( showFileDialogButton ); f.setVisible (true); } }

Adapter Class Adapter classes provide an implementation of listener interfaces. When you inherit the adapter class implementation for all methods is not mandatory java.awt.event Adapter Class Listener Interface WindowAdapter WindowListener KeyAdapter KeyListener MouseAdapter MouseListener MouseMotionAdapter MouseMotionListener FocusAdapter FocusListener ComponentAdapter  ComponentListener ContainerAdapter ContainerListener

Mouse Adapter import java.awt .*; import java.awt.event .*; public class MouseAdapterExample extends MouseAdapter { Frame f; MouseAdapterExample (){ f=new Frame("Mouse Adapter"); f.addMouseListener (this); f.setSize (300,300); f.setLayout (null); f.setVisible (true); } public void mouseClicked ( MouseEvent e) { Graphics g= f.getGraphics (); g.setColor ( Color.BLUE ); g.fillOval ( e.getX (), e.getY (),30,30); } public static void main(String[] args ) { new MouseAdapterExample (); } }

MouseMotionAdapter import java.awt .*; import java.awt.event .*; public class MouseMotionAdapterExample extends MouseMotionAdapter { Frame f; MouseMotionAdapterExample (){ f=new Frame("Mouse Motion Adapter"); f.addMouseMotionListener (this); f.setSize (300,300); f.setLayout (null); f.setVisible (true); } public void mouseDragged ( MouseEvent e) { Graphics g= f.getGraphics (); g.setColor ( Color.ORANGE ); g.fillOval ( e.getX (), e.getY (),20,20); } public static void main(String[] args ) { new MouseMotionAdapterExample (); } }

KeyAdapter import java.awt .*; import java.awt.event .*; public class KeyAdapterExample extends KeyAdapter { Label l; TextArea area; Frame f; KeyAdapterExample (){ f=new Frame("Key Adapter"); l=new Label(); l.setBounds (20,50,200,20); area=new TextArea (); area.setBounds (20,80,300, 300); area.addKeyListener (this); f.add (l); f.add (area); f.setSize (400,400); f.setLayout (null); f.setVisible (true); } public void keyReleased ( KeyEvent e) { String text= area.getText (); String words[]= text.split ("\s"); l.setText ("Words: "+ words.length +" Characters:"+ text.length ()); } public static void main(String[] args ) { new KeyAdapterExample (); }
Tags