Sie sind auf Seite 1von 18

Java AWT programming

Button Class
• A Button is a component that contains a label
and that generates an ActionEvent when it is
pressed.
• Button defines these two constructors:
– Button( ) : Creates an empty button
– Button(String str) : Creates a button that contains str
as a label.
• These methods are as follows:
– void setLabel(String str ) : , str becomes the new label
for the button
– String getLabel(): GET the label name for button
Action Event
• This event is generated by a component (such as a Button)
when the component-specific action occurs (such as click).
• The event is passed to an ActionListener object which is
registered to receive the event notification using the
component’s addActionListener() method.
• The event handler method is actionPerformed(), will be
executed as event handler.
TextField & TextArea
• The TextField class implements a single-line text entry.
– TextField( ) -used to create default text editor
– TextField(int numChars) -used to create a text filed with
some number of characters length.
– String getText( ) -used to get the text from the text field
– void setText(String str) -used to set the text to the text field
– String getSelectedText( ) -used to get the selected text
• The TextArea class implements multiple line text.
– 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.
KeyEvent
• KeyEvent is an event which indicates that a keystroke
occurred in a component.
• It is generated by component object (such as a text
field, Applet, Frame) when a key is pressed, released,
or typed.
• The event is passed to a KeyListener object which is
registered to receive the event notification using the
component’s addKeyListener ()method.
ScrollBar
• Scrollbars are used to select continuous values through
a range of integer values (the range set between
maximum and minimum).
• These scrollbars can either be set horizontally or
vertically.
• The scrollbar’s maximum and minimum values can be
set along with line increments and page increments.
– Scrollbar()
– Scrollbar(int direction)
– Scrollbar(int direction, int initValue, int pageSize, int min,
int max)
AdjustmentEvent
• The adjustment events are generated by
Adjustable objects like scroll bar.
• Methods of AdjustmentEvent :
– Adjustable getAdjustable()
– returns the Adjustable object where this event
originated.
– int getAdjustmentType()
– returns the type of adjustment which caused the
value changed event.
– int getValue()
– returns the current value in the adjustment event.
ItemEvent
• It is an event which shows whether an item was selected or de-
selected.
• This event is generated by an ItemSelectable object (such as a List),
where the event is generated when an item of the list is either
selected or de-selected.
• The event generated is passed to every ItemListener object which is
registered to receive such events.
• The method addItemListener() is used for this registration process.
CheckBox & CheckboxGroup
• Checkboxes are used as on-off or yes-no switches
• if you click on an unchecked checkbox, it will get checked and
vice versa.
• Constructors of Checkbox
– Checkbox()
– Checkbox(String str) // constructor with label of chechbox
– Checkbox(String str, boolean on) // constructor with label and initial
state of checkbox
– Checkbox(String str, CheckBoxGroup cbg, boolean on) // creating
radio button
• Methods
– boolean getState() // returns state of the checkbox true / false
– String getLabel() // returns label of chechbox
– CheckboxGroup getCheckboxGroup() // returns checkbox group
obj. of radiobutton
Choice Control
• provides a pop-up menu of text string choices
• current choice is displayed on top.
– Choice c = new Choice();
• the add method enables you to add new entries.
– c.add(“Red”);
– c.add(“Green”);
• The currently selected item can be changed by using
select() method.
• The selection can be made based on name or index. For
e.g.
– c.select(“Red”);
– c.select(0);
– getSelectedIndex() - return the position of the selected item
– getSelectedItem() - returns the name of the selected item
List Control
• The List class provides a compact, multiple-choice, scrolling
selection list.
• Unlike the Choice object, which shows only the single selected
item in the menu, a List object can be constructed to show any
number of choices in the visible window.
• Constructors:
– List(int numRows) - the value of numRows specifies the number of
entries in the list that will always be visible.
– List(int numRows, boolean multipleSelect) -if multipleSelect is true,
then the user may select two or more items at a time.
• Methods
– void add(String name) -Used to add an item to the list at the end.
– void add(String name, int index) – used to add an item to the list at
the specified index.
– String getSelectedItem( ) -used to get the item that is selected
– int getSelectedIndex( ) -used to get the index of the item selected.
Adapter classes
• Java provides a special feature, called an adapter class,
that can simplify the creation of event handlers.
• An adapter class provides an empty implementation of
all methods in an event listener interface.
• Adapter classes are useful when you want to receive
and process only some of the events that are handled
by a particular event listener interface.
• You can define a new class to act as an event listener
by extending one of the adapter classes and
implementing only those events in which you are
interested.
Container classes
• A Container is an object used to group Components.
• The container class is a subclass of Component:
• Any Container object can be treated as a Component.
• Since a Container is a Component, we can have Containers inside
Containers.
• Every Container has its own LayoutManager which determines how
Components will be arranged.
• Window
– Window is a top-level display surface. An object of Window class is not
Attached to nor embedded within another container. An instance of the
Window does not have border, title bar or menu.
• Frame
– Frame is a top-level window with a border and title. An instance of the
Frame class may have a menu bar, title bar and borders. It is otherwise like
an object of the Window class.
• Dialog
– Dialog is top-level display surface (a window) with a border and title. An
object of the Dialog class cannot exist without an associated object of the
Frame class.
• Panel
– Panel is generic container for holding components. An instance of the
Panel class provides a container to which components can be added. It
does not add any new method; it simply implements the Container.
• Applet
– Applet is a special type of Container that is embedded in the webpage to
generate the dynamic content. It runs inside the browser and works at
client side. The browser can control the applet with calling life cycle
methods of the Applet class.
Methods of Containers classes
• The Container class defined all the data and methods
necessary for managing groups of Components
– void add(Component c) -- adding control to the container
– Component getComponent() – returns the object of
control
– Dimension getMaximumSize()
– Dimension getMinimumSize()
– Dimension getPreferredSize()
– void remove(Component c) – remove the control
– void removeAll() – remove all controls
MouseEvent
• It is an event which indicates that a mouse action occurred in a
component.
• A mouse action occurs in a particular component if and only if the
mouse cursor is over the defined part of the component’s bounds when
the action happens.
• There are eight types of mouse events defined in the MouseEvent class.
• Methods in the class MouseEvent:
– int getButton()
– Returns which, if any, of the mouse buttons has changed state.
– int getClickCount()
– Returns the number of mouse clicks associated with this event.
– int getX()
– Returns the horizontal x position of the event relative to the source
component.
– int getY()
– Returns the vertical y position of the event relative to the source
component.
WindowEvent-WindowListener
• WindowListener
– void windowActivated(WindowEvent we)
– void windowClosed(WindowEvent we)
– void windowClosing(WindowEvent we)
– void windowDeactivated(WindowEvent we)
– void windowDeiconified(WindowEvent we)
– void windowIconified(WindowEvent we)
– void windowOpened(WindowEvent we)

Das könnte Ihnen auch gefallen