MODULE 5.pptx gui programming and applets

LIKITHLIKITH7 24 views 72 slides Aug 01, 2024
Slide 1
Slide 1 of 72
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
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72

About This Presentation

5 notes


Slide Content

Module 5 GUI PROGRAMMING AND APPLETS Java AWT (Abstract Window Toolkit ) 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 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 . It is basically a screen where the where the components are placed at their specific locations. Thus it contains and controls the layout of components.

There are four types of containers in Java AWT: Window Panel Frame Dialog Window:   Window is a top-level container that represents a graphical window or dialog box. The Window class extends the Container class, which means it can contain other components, such as buttons, labels, and text fields.

Panel :  Panel is a container class in Java. It is a lightweight container that can be used for grouping other components together within a window or a frame. Frame : The Frame is the container that contains the title bar and border and can have menu bars. Dialog :   A dialog box is a temporary window an application creates to retrieve user input.

Java AWT Example To create simple AWT example, you need a frame. There are two ways to create a GUI using Frame in AWT. By extending Frame class ( inheritance ) By creating the object of Frame class ( association )

AWT Example by Inheritance Let's see a simple example of AWT where we are inheriting Frame class . Here, we are showing Button component on the Frame . Example: import java.awt .*; public class AWTExample1 extends Frame { AWTExample1() {

Button b = new Button("Click Me!!"); b.setBounds (30,100,80,30); add(b); setSize (300,300); setTitle ("This is our basic AWT example"); setLayout (null); setVisible (true); } public static void main(String args []) { AWTExample1 f = new AWTExample1(); } }

Output

AWT Example by Association Let's see a simple example of AWT where we are creating instance of Frame class. Here , we are creating a TextField , Label and Button component on the Frame. Example import java.awt .*; class AWTExample2 { AWTExample2() {

Frame f = new Frame(); Label l = new Label("Employee id:"); Button b = new Button("Submit"); TextField t = new TextField (); l.setBounds (20, 80, 80, 30); t.setBounds (20, 100, 80, 30); b.setBounds (100, 100, 80, 30); f.add (b) f.add (l); f.add (t ); f.setSize (400,300 ); f.setTitle ("Employee info "); f.setLayout (null ); f.setVisible (true); }

public static void main(String args []) { AWTExample2 awt_obj = new AWTExample2(); } } output

SWING Swing  is a Java Foundation Classes [JFC] library and an extension of the Abstract Window Toolkit [AWT ]. Swing  is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java . Unlike AWT, Java Swing provides platform-independent and lightweight components. The javax.swing package provides classes for java swing API such as JButton , JTextField , JTextArea , JRadioButton , JCheckbox , JMenu , JColorChooser etc.

Difference between AWT and Swing Java AWT Java Swing AWT components are  platform-dependent . Java swing components are  platform-independent . AWT components are  heavyweight . Swing components are  lightweight . AWT  doesn't support pluggable look and feel . Swing  supports pluggable look and feel . AWT provides  less components  than Swing. Swing provides  more powerful components  such as tables, lists, scrollpanes , colorchooser , tabbedpane etc. AWT  doesn't follows MVC (Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view. Swing  follows MVC .

Hierarchy of Java Swing classes

Swing Containers Containers are an integral part of SWING GUI components. A container provides a space where a component can be located. A Container in AWT is a component itself and it provides the capability to add a component to itself . Following are certain noticable points to be considered. Sub classes of Container are called as Container. For example, JPanel , JFrame and JWindow . Container can add only a Component to itself. A default layout is present in each container which can be overridden using  setLayout  method .

Following is the list of commonly used containers while designed GUI using SWING . Panel: JPanel is the simplest container. It provides space in which any other component can be placed, including other panels . Frame: A JFrame is a top-level window with a title and a border . Window: A JWindow object is a top-level window with no borders and no menubar .

Swing Components A component is an independent visual control and Java Swing Framework contains a large set of these components which provide rich functionalities and allow high level of customization. They all are derived from JComponent class . All these components are lightweight components. This class provides some common functionality like pluggable look and feel, support for accessibility, drag and drop, layout, etc.

A container holds a group of components . It provides a space where a component can be managed and displayed . Containers are of two types : Top level Containers Lightweight Containers

Top level Containers It inherits Component and Container of AWT. It cannot be contained within other containers. Heavyweight. Example: JFrame , JDialog , JApplet Lightweight Containers It inherits JComponent class. It is a general purpose container. It can be used to organize related components together. Example: JPanel

JFrame The javax.swing.JFrame class is a type of container which inherits the java.awt.Frame class . JFrame works like the main window where components like labels, buttons, textfields are added to create a GUI. Unlike Frame, JFrame has the option to hide or close the window with the help of setDefaultCloseOperation ( int ) method.

Example import javax.swing.JFrame ; public class JFrameExample { public static void main(String s[]) { JFrame frame = new JFrame (" JFrame Example"); frame.setSize (300, 300); frame.setVisible (true); } }

JDialog The JDialog 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 Dialog class. Unlike JFrame , it doesn't have maximize and minimize buttons. Commonly used Constructors Constructor Description JDialog () It is used to create a modeless dialog without a title and without a specified Frame owner. JDialog(Frame owner) It is used to create a modeless dialog with specified Frame as its owner and an empty title.

import javax.swing .*; import java.awt .*; public class DialogExample { DialogExample () { JFrame f= new JFrame (); JDialog d = new JDialog (f , "RNSIT", true); d.setSize (300,300); d.setVisible (true); } public static void main(String args []) { new DialogExample (); } }

JPanel The JPanel is a simplest container class. It provides space in which an application can attach any other component . It inherits the JComponents class. It doesn't have title bar. JPanel class declaration public   class   JPanel   extends   JComponent   implements  Accessible  

Commonly used Constructors: Constructor Description JPanel () It is used to create a new JPanel with a double buffer and a flow layout. JPanel(boolean isDoubleBuffered) It is used to create a new JPanel with FlowLayout and the specified buffering strategy. JPanel(LayoutManager layout) It is used to create a new JPanel with the specified layout manager.

import java.awt .*; import javax.swing .*; public class PanelExample { PanelExample () { JFrame f= new JFrame ("Panel Example"); JPanel panel=new JPanel (); panel.setBounds (40,80,200,200); panel.setBackground ( Color.gray ); JButton b1=new JButton ("Button 1"); b1.setBounds(50,100,80,30); b1.setBackground( Color.yellow );

JButton b2=new JButton ("Button 2"); b2.setBounds(100,100,80,30); b2.setBackground( Color.green ); panel.add (b1); panel.add (b2); f.add (panel); f.setSize (400,400); f.setLayout (null); f.setVisible (true); } public static void main(String args []) { new PanelExample (); } }

JButton We use  JButton  class to create a push button on the UI. The button can include some display text or images . It yields an event when clicked and double-clicked . We can implement a JButton in the application by calling one of its constructors. Syntax: JButton okBtn = new JButton (“Click ”);

Commonly used Constructors: Constructor Description JButton () It creates a button with no text and icon. JButton (String s) It creates a button with the specified text. JButton(Icon i) It creates a button with the specified icon object.

Example: import javax.swing .*; import java.awt.event .*; import java.awt .*; public class testswing extends JFrame { testswing () { JButton bt1 = new JButton ("click"); setLayout (new FlowLayout ());

setSize (400 , 400) add(bt1); setVisible (true ); } public static void main(String[] args ) { new testswing (); } }

JLabel In Java, Swingtoolkit contains a JLabel Class. It is under package javax.swing.JLabel class. It is used for placing text in a box . Only Single line text is allowed and the text can not be changed directly. The JLabel Contains 4 constructors . 1 . JLabel () 2. JLabel (String s) 3. JLabel (Icon i) 4. JLabel (String s, Icon i, int horizontalAlignment )

class SLabel { public static void main(String args []) { JFrame label_f = new JFrame ("Label Demo"); JLabel label_l1,label_l2; label_l1=new JLabel ("Welcome to studytonight.com"); label_l1.setBounds(50,50, 200,30); label_l2=new JLabel ("How are You?"); label_l2.setBounds(50,100, 200,30); label_f.add (label_l1); label_f.add (label_l2); label_f.setSize (300,300); label_f.setLayout (null); label_f.setVisible (true); } }

JTextField The object of a JTextField class is a text component that allows the editing of a single line text. It inherits JTextComponent class . Commonly used Constructors: Constructor Description JTextField () Creates a new TextField JTextField (String text) Creates a new TextField initialized with the specified text. JTextField(String text, int columns) Creates a new TextField initialized with the specified text and columns. JTextField(int columns) Creates a new empty TextField with the specified number of columns.

import javax.swing .*; class TextFieldExample { public static void main(String args []) { JFrame f= new JFrame (" TextField Example"); JTextField t1,t2; t1=new JTextField (); t1.setBounds(50,100, 200,30); f.add (t1); f.setSize (400,400); f.setLayout (null); f.setVisible (true); } }

JTextArea The object of a JTextArea class is a multi line region that displays text. It allows the editing of multiple line text. It inherits JTextComponent class. Commonly used Constructors : Constructor Description JTextArea () Creates a text area that displays no text initially. JTextArea (String s) Creates a text area that displays specified text initially. JTextArea ( int row, int column) Creates a text area with the specified number of rows and columns that displays no text initially. JTextArea(String s, int row, int column) Creates a text area with the specified number of rows and columns that displays specified text.

import javax.swing .*; public class TextAreaExample { TextAreaExample () { JFrame f= new JFrame (); JTextArea area=new JTextArea (); area.setBounds (10,30, 200,200); f.add (area); f.setSize (300,300); f.setLayout (null); f.setVisible (true); } public static void main(String args []) { new TextAreaExample (); } }

BorderLayout ( LayoutManagers ) The LayoutManagers are used to arrange components in a particular manner . The  Java LayoutManagers  facilitates us to control the positioning and size of the components in GUI forms . LayoutManager is an interface that is implemented by all the classes of layout managers . :

There are the following classes that represent the layout managers java.awt.BorderLayout java.awt.FlowLayout java.awt.GridLayout java.awt.CardLayout java.awt.GridBagLayout javax.swing.BoxLayout javax.swing.GroupLayout javax.swing.ScrollPaneLayout javax.swing.SpringLayout etc.

BorderLayout The BorderLayout is used to arrange the components in five regions: north, south, east, west, and center. Each region (area) may contain one component only. It is the default layout of a frame or window. The BorderLayout provides five constants for each region: public static final int NORTH public static final int SOUTH public static final int EAST public static final int WEST public static final int CENTER

import java.awt .*; import javax.swing .*; public class Border { JFrame f; Border() { f = new JFrame (); JButton b1 = new JButton (" NORTH"); JButton b2 = new JButton ("SOUTH"); JButton b3 = new JButton ("EAST "); JButton b4 = new JButton ("WEST "); JButton b5 = new JButton ("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(); } }

GridLayout The Java GridLayout class is used to arrange the components in a rectangular grid. One component is displayed in each rectangle. Constructors of GridLayout class 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 along with given horizontal and vertical gaps.

import java.awt .*; import javax.swing .*; public class GridLayoutExample { JFrame frameObj ; GridLayoutExample () { frameObj = new JFrame (); JButton btn1 = new JButton ("1"); JButton btn2 = new JButton ("2"); JButton btn3 = new JButton ("3"); JButton btn4 = new JButton ("4");

frameObj.add (btn1); frameObj.add (btn2); frameObj.add (btn3); frameObj.add (btn4); frameObj.setLayout (new GridLayout (2,2)); frameObj.setSize (300, 300); frameObj.setVisible (true ); } public static void main(String argvs []) { new GridLayoutExample (); } }

FlowLayout The Java FlowLayout class is used to arrange the components in a line, one after another (in a flow). It is the default layout of the 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 vertical gap.

import java.awt .*; import javax.swing .*; public class FlowLayoutExample { JFrame frameObj ; FlowLayoutExample () { frameObj = new JFrame (); JButton b1 = new JButton ("1"); JButton b2 = new JButton ("2"); JButton b3 = new JButton ("3"); JButton b4 = new JButton ("4");

frameObj.add (b1); frameObj.add (b2); frameObj.add (b3); frameObj.add (b4); frameObj.setLayout (new FlowLayout ()); frameObj.setSize (300, 300); frameObj.setVisible (true); } public static void main(String argvs []) { new FlowLayoutExample (); } }

Applet Fundamentals Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side. Java Plug-in software is responsible to manage the life cycle of an applet. Lifecycle of Java Applet init Start paint stop destroy

init(): is used to initialized the Applet. It is invoked only once. start(): is invoked after the init() method or browser is maximized. It is used to start the Applet. paint( ) is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called. The paint( ) method has one parameter of type Graphics . stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized. destroy( ) : The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. The stop( ) method is always called before destroy( ) .

example of Applet by html file: //First.java   import java.awt.*; import java.applet .*; public class First extends Applet { public void paint (Graphics g) { g.drawString (“Hello World", 20, 20); } }

//RunHelloWorld.html <html>   <body>   <applet code=" First " width=“200" height=“60">   </applet>   </body>   </html>  How to run: javac First.java appletviewer RunHelloWorld.html

This applet begins with two import statements. The first imports the Abstract Window Toolkit (AWT) classes. Applets interact with the user (either directly or indirectly) through the AWT, not through the console-based I/O classes. The AWT contains support for a window-based, graphical user interface. The second import statement imports the applet package, which contains the class Applet. Every applet that you create must be a subclass of Applet. paint( ) is called each time that the applet must redisplay its output. Inside paint( ) is a call to drawString ( ), which is a member of the Graphics class.

The call to drawString ( ) in the applet causes the message “A Simple Applet ” to be displayed beginning at location 20,20. Here are the key points that you should remember now: Applets do not need a main( ) method. Applets must be run under an applet viewer or a Java-compatible browser. User I/O is not accomplished with Java’s stream I/O classes. Instead, applets use the interface provided by the AWT or Swing.

Difference between Application and Applets

Passing Parameter in Applet We can get any information from the HTML file as a parameter. For this purpose, Applet class provides a method named getParameter (). Syntax : public  String  getParameter (String  parameterName )  Example  

MyApplet.java import java.awt .*; import java.applet .*; public class MyApplet extends Applet { String n , a ; public void init () { n = getParameter ("name "); a = getParameter ("age"); } public void paint(Graphics g) { g.drawString ("Name is: " + n, 20, 20); g.drawString ("Age is: " + a, 20, 20); }}

rnsit.html < html>   <body>   <applet code=" MyApplet " height="300" width="500"> < param name="name" value="Ramesh" /> < param name="age" value="25" /> </ applet>   </body>   </html>