Any program that utilizes GUI (graphic user interface) like windows-written Java application is event oriented. Event defines any object's change in status. For example: press a button, enter a character in textbox, click or drag a mouse, etc.
Event handling has three main components,
A source produces an event and sends it with the source to one or more listeners. Once the listener receives the event, the event will be processed and returned. Many Java packages, such as java.util, java.awt and java.awt.event, support events.
Event Classes | Description | Listener Interface |
---|---|---|
ActionEvent | generated when button is pressed, menu-item is selected, list-item is double clicked | ActionListener |
MouseEvent | generated when mouse is dragged, moved,clicked,pressed or released and also when it enters or exits a component | MouseListener |
KeyEvent | generated when input is received from keyboard | KeyListener |
ItemEvent | generated when check-box or list item is clicked | ItemListener |
TextEvent | generated when value of textarea or textfield is changed | TextListener |
MouseWheelEvent | generated when mouse wheel is moved | MouseWheelListener |
WindowEvent | generated when window is activated, deactivated, deiconified, iconified, opened or closed | WindowListener |
ComponentEvent | generated when component is hidden, moved, resized or set visible | ComponentEventListener |
ContainerEvent | generated when component is added or removed from container | ContainerListener |
AdjustmentEvent | generated when scroll bar is manipulated | AdjustmentListener |
FocusEvent | generated when component gains or loses keyboard focus | FocusListener |
import java.awt.*; import java.awt.event.*; import java.applet.*; import java.applet.*; import java.awt.event.*; import java.awt.*; public class Test extends Applet implements KeyListener { String msg=""; public void init() { addKeyListener(this); } public void keyPressed(KeyEvent k) { showStatus("KeyPressed"); } public void keyReleased(KeyEvent k) { showStatus("KeyRealesed"); } public void keyTyped(KeyEvent k) { msg = msg+k.getKeyChar(); repaint(); } public void paint(Graphics g) { g.drawString(msg, 20, 40); } }
<applet code="Test" width=300, height=100> </applet>
AWT includes a big amount of classes and techniques to build and handle apps such as windows, buttons, scroll bars, etc. The AWT was intended to provide a prevalent collection of GUI design instruments capable of working on a multitude of platforms.The tools supplied by the AWT are introduced using the indigenous GUI toolkit of each platform, thus maintaining each platform's look and feel. This is an benefit of using AWT.But the disadvantage of such an strategy is that when displayed on another platform, GUI built on one platform may look distinct.
AWT is the basis for Swing, i.e. Swing is a set of GUI interfaces that extend the AWT. But nowadays AWT is merely used as most GUI Java programs are being implemented using Swing, due to its rich implementation of GUI controls and lightweight nature.
At the top of the AWT hierarchy is the component category. Component is an abstract class encapsulating all the visual component characteristics. Recalling the present foreground and background colors and the presently chosen text font is accountable for a component item.
Container is an AWT element containing a different element such as button, text field, tables, etc. Container is a component class subclass. Container class keeps track of parts added to a different element.
Panel class is a container specific subclass. There is no title bar, menu bar or boundary in the panel. It is a container used to hold parts.
Window class produces a window of the highest standard. Window has no boundaries and menu bar.
Frame is a Window subclass and can be resized. It is a container containing various parts such as button, title bar, textfield, label, etc. Most AWT apps are developed in Java using the Frame window. Frame class is made up of two distinct builders.
Frame() throws HeadlessException Frame(String title) throws HeadlessException
There are two ways to create a Frame. They are,
Creating Frame Window by Instantiating Frame class
import java.awt.*; public class Testawt { Testawt() { Frame fm=new Frame(); //Creating a frame Label lb = new Label("welcome to java graphics"); //Creating a label fm.add(lb);//adding label to the frame fm.setSize(300, 300); //setting frame size. fm.setVisible(true); //set frame visibilty true } public static void main(String args[]) { Testawt ta = new Testawt(); } }
Creating Frame window by extending Frame class
package testawt; import java.awt.*; import java.awt.event.*; public class Testawt extends Frame { public Testawt() { Button btn=new Button("Hello World"); add(btn); //adding a new Button. setSize(400, 500); //setting size. setTitle("StudyTonight"); //setting title. setLayout(new FlowLayout());//set default layout for frame. setVisible(true); //set frame visibilty true. } public static void main (String[] args) { Testawt ta = new Testawt(); //creating a frame. } }
Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.
Thank you for your valuable information.
Thank you for this wonderful article!
This article was really helpful to me, thank you!
super article!
Leave a Reply
Your email address will not be published. Required fields are marked *