Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Java AWT, Swing_notes

Uploaded by

deyavilash123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java AWT, Swing_notes

Uploaded by

deyavilash123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

JAVA AWT,EVENT HANDLING AND SWING

Java AWT
Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface
(GUI) or windows-based applications in Java.

Java AWT components are platform-dependent i.e. components are displayed


according to the view of operating system. AWT is heavy weight i.e. its
components are using the resources of underlying operating system (OS).

The java.awt package provides classes for AWT API such as TextField, Label,
TextArea, RadioButton, CheckBox, Choice, List etc.

Java AWT Hierarchy


The hierarchy of Java AWT classes are given below.
Components
All the elements like the button, text fields, scroll bars, etc. are called
components. In Java AWT, there are classes for each component as shown in
above diagram. In order to place every component in a particular position on a
screen, we need to add them to a container.

Container
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.

Java AWT Example

AWT Example by Inheritance


​ / importing Java AWT class
​ import java.awt.*;

​ // extending Frame class to our class AWTExample1
​ public class AWTExample1 extends Frame {

​ // initializing using constructor
​ AWTExample1() {

​ // creating a button
​ Button b = new Button("Click Me!!");

​ // setting button position on screen
​ b.setBounds(30,100,80,30);

​ // adding button into frame
​ add(b);

​ // frame size 300 width and 300 height
​ setSize(300,300);

​ // setting the title of Frame
​ setTitle("This is our basic AWT example");

​ // no layout manager
​ setLayout(null);

​ // now frame will be visible, by default it is not visible
​ setVisible(true);
​ }

​ // main method
​ public static void main(String args[]) {

​ // creating instance of Frame class
​ AWTExample1 f = new AWTExample1();

​ }

​ }
Java Event Handling

Changing the state of an object is known as an event. For example, click on


button, dragging mouse etc. The java.awt.event package provides many
event classes and Listener interfaces for event handling.

Event Classes Listener Interfaces

ActionEvent ActionListener

MouseEvent MouseListener and


MouseMotionListener

KeyEvent KeyListener

TextEvent TextListener

Java event handling by implementing ActionListener


​ import java.awt.*;
​ import java.awt.event.*;
​ class AEvent extends Frame implements ActionListener{
​ TextField tf;
​ AEvent(){

​ //create components
​ tf=new TextField();
​ tf.setBounds(60,50,170,20);
​ Button b=new Button("click me");
​ b.setBounds(100,120,80,30);

​ //register listener
​ b.addActionListener(this);//passing current instance

​ //add components and set size, layout and visibility
​ add(b);add(tf);
​ setSize(300,300);
​ setLayout(null);
​ setVisible(true);
​ }
​ public void actionPerformed(ActionEvent e){
​ tf.setText("Welcome");
​ }
​ public static void main(String args[]){
​ new AEvent();
​ }
​ }

Java Swing
Java Swing was introduced as part of the Java Foundation Classes (JFC) in the
late 1990s, aiming to address the limitations of the earlier Abstract Window
Toolkit (AWT). Unlike AWT, that relies on the native platform's components for
rendering, Swing is entirely written in Java, offering a consistent look and feel
across different operating systems.

Swing provides a comprehensive set of components for building GUIs, including


buttons, text fields, panels, and more. These components are highly
customizable, allowing developers to create visually appealing and user-friendly
interfaces.
Key Features of Java Swing
Platform Independence: One of the primary advantages of Swing is its platform
independence. Applications developed using Swing can run on any platform that
supports Java, without requiring modifications.

Rich Set of Components: Swing offers a wide range of components that can be
used to create complex GUIs. Developers can choose from basic components like
buttons and labels to advanced components such as tables, trees, and scroll
panes.

Customizability: Swing components are highly customizable, allowing developers


to control various aspects of their appearance and behavior. Properties such as
size, color, font, and layout can be easily adjusted to meet specific design
requirements.

Event Handling: Swing provides a robust event handling mechanism that allows
developers to respond to user interactions, such as button clicks and mouse
movements. This enables the creation of interactive and responsive applications.

Hierarchy of Java Swing classes


The hierarchy of java swing API is given below.
Java Swing Example
​ import javax.swing.*;
​ public class FirstSwingExample {
​ public static void main(String[] args) {
​ JFrame f=new JFrame();//creating instance of JFrame

​ JButton b=new JButton("click");//creating instance of JButton
​ b.setBounds(130,100,100, 40);//x axis, y axis, width, height

​ f.add(b);//adding button in JFrame

​ f.setSize(400,500);//400 width and 500 height
​ f.setLayout(null);//using no layout managers
​ f.setVisible(true);//making the frame visible
​ }
​ }

Output:
Example of Swing by Inheritance
We can also inherit the JFrame class, so there is no need to create the instance of
JFrame class explicitly.

File Name: DemoSwing.java

​ import javax.swing.*;
​ public class DemoSwing extends JFrame{//inheriting JFrame
​ JFrame f;
​ Simple2(){
​ JButton b=new JButton("click");//create button
​ b.setBounds(130,100,100, 40);
​ add(b);//adding button on frame
​ setSize(400,500);
​ setLayout(null);
​ setVisible(true);
​ }
​ public static void main(String[] args) {
​ new Simple2();
​ }}

Java JButton
The JButton class is used to create a labeled button that has platform
independent implementation. The application result in some action when the
button is pushed. It inherits AbstractButton class.

A key element of graphical user interfaces (GUIs) in Java that is used to create
interactive buttons is the JButton class. Users can click these labelled buttons to
initiate particular operations within the application. Because JButton offers a
platform-independent implementation, it can be used in a variety of settings and
operating systems. It is descended from the AbstractButton class, which offers
shared functionality for all button kinds in the Swing GUI framework and Java's
Abstract Window Toolkit (AWT). Developers can improve the usability and
interactivity of their Java programmes by adding sensible user interface
components to their JButton objects through configuration.

Java JButton Example with ActionListener


Filename: ButtonExample.java

​ import java.awt.event.*;
​ import javax.swing.*;
​ public class ButtonExample {
​ public static void main(String[] args) {
​ JFrame f=new JFrame("Button Example");
​ final JTextField tf=new JTextField();
​ tf.setBounds(50,50, 150,20);
​ JButton b=new JButton("Click Here");
​ b.setBounds(50,100,95,30);
​ b.addActionListener(new ActionListener(){
​ public void actionPerformed(ActionEvent e){
​ tf.setText("Welcome to Java.");
​ }
​ });
​ f.add(b);f.add(tf);
​ f.setSize(400,400);
​ f.setLayout(null);
​ f.setVisible(true);
​ }
​ }

You might also like