Programmer Coding

GUI Programming with java

Contents

The AWT Class ranking

 

Java   AWT (Conceptual Window Tool kit)  is an   API   to    develop           GUI                      or              window-based applications in java.

 

Java AWT factors are platform-dependent i.e. factors are out put according to the view of operating system. AWT is heavyweight i.e. its factors are using the resources of OS.

 

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

Java AWT Ranking

 

The ranking of Java AWT classes are given below.

 

Store(contain)

 

The Store(contain) is a factor in AWT that can store(contain) another factors like buttons, textfields, labels etc. The classes that expand Store(contain)er class are known as store(contain)er such as Frame, Dialog and Panel.

Window

 

The window is the store(contain)er that have no borders and menu bars. You must use frame, dialog or another window for creating a window.

Panel

 

The Panel is the store(contain) that doesn’t store(contain) title bar and menu bars. It can have other                                   factors like button, textfield etc.

Frame

 

The Frame is the store(contain)er that store(contain) title bar and can have menu bars. It can have other                                                        factors like button, textfield etc.

Useful Way(method) s of Factor class

 

Way(method)                                                    Description

public void add(Factor c) inserts a factor on this factor.
public void setSize(int width,int height) sets the size (width and height) of the factor.
public void setSetup(SetupManager m) bounds the setup manager for the factor.
 

public void setClear(boolean status)

 

changes the visibility of the factor, by default false.

Java AWT Example

 

To produce simple awt example, you need a frame. There are two way(method) s to produce a 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 factor on the Frame.

 

import java.awt.*;

class First expand Frame{ First(){

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 setSetup(null);//no setup manager

setClear(true);//now frame will be clear, by default not clear

}

public static void main(String args[]){ First f=new First();

}}

The setBounds(int xaxis, int yaxis, int width, int height) way(method)  is used in the above example that sets the position of the awt button.

 

 

 

 

 

 

 

 

 

 

 

Java Swing

 

Java Swing tutorial is a part of Java Foundation Classes (JFC) that is used to produce window- based applications. It is built on the top of AWT (Conceptual Windowing Toolkit) API and entirely written in java.

 

Unlike AWT, Java Swing gives platform-self-supported and lightweight factors.

 

The javax.swing package gives classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.

 

Difference between AWT and Swing.

 

No.

 

Java AWT

   

Java Swing

 

1)

 

AWT         factors

dependent.

 

are platform-

 

Java swing factors are platform- self-supported.

2) AWT factors are heavyweight. Swing factors are lightweight.
3) AWT doesn’t pillar pluggable look and feel. Swing pillars pluggable look and feel.
4) AWT     gives less

Swing.

factors than  

Swing gives more powerful factorssuch as tables, lists, scrollpanes, colorchooser, tabbedpane etc.

5) AWT doesn’t follows MVC(Model View Controller) where model represents data, view represents presentation and controller acts as an borderline between model and view. Swing follows MVC.

Commonly used Way(method) s of Factor class

 

Way(method)                                                    Description

public void add(Factor c) add a factor on another factor.
public void setSize(int width,int height) sets size of the factor.
public void setSetup(SetupManager m) sets the setup manager for the factor.
public void setClear(boolean b) sets the visibility of the factor. It is by default                  false.

 

Ranking of Java Swing classes

 

The ranking of java swing API is given below.

Java Swing Examples

 

There are two way(method) s to produce a frame:

 

  • By creating the object of Frame class
  • By extending Frame class (inheritance)

 

We can write the code of swing inside the main(), creater or any other way(method) .

 

Simple Java Swing Example

 

Let’s see a simple swing example where we are creating one button and adding it on the JFrame object inside the main() way(method) .

 

File: FirstSwingExample.java

 

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.setSetup(null);//using no setup managers f.setClear(true);//making the frame clear

} }

Store(contain)er

Java JFrame

 

The javax.swing.JFrame class is a type of store(contain)er which inherits the java.awt.Frame class. JFrame works like the main window where factors like labels, buttons, text fields are added to produce a GUI.

 

Unlike Frame, JFrame has the option to hide or close the window with the help of setDefaultCloseOperation(int) way(method) .

JFrame Example

 

import java.awt.FlowSetup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.Jpanel; public class JFrameExample {

public static void main(String s[]) {

JFrame frame = new JFrame(“JFrame Example”); JPanel panel = new JPanel(); panel.setSetup(new FlowSetup());

JLabel label = new JLabel(“JFrame By Example”); JButton button = new JButton(); button.setText(“Button”);

panel.add(label);

 

panel.add(button); frame.add(panel); frame.setSize(200, 300); frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setClear(true);

} }

J’a small computer application(Applet)’

As we prefer Swing to AWT. Now we can use J’a small computer application(Applet)’ that can have all the controls of swing. The J’a small computer application(Applet)’ class expand the ‘a small computer application(Applet)’ class.

Example of IncidentHandling in J’a small computer application(Applet)’:

 

import java.’a small computer application(Applet)’.*; import javax.swing.*; import java.awt.incident.*;

public class IncidentJ’a small computer application(Applet)’ expand J’a small computer application(Applet)’ applies ActionListener{ JButton b;

JTextField tf;

public void init(){ tf=new JTextField();

tf.setBounds(30,40,150,20); b=new JButton(“Click”); b.setBounds(80,150,70,40); add(b);add(tf); b.addActionListener(this); setSetup(null);

}

public void actionPull off(ActionIncident e){ tf.setText(“Welcome”);

} }

In the above example, we have produced all the controls in init() way(method)  because it is produce only once.

my’a small computer application(Applet)’.html

  1. <html>
  2. <body>
  3. <‘a small computer application(Applet)’ code=”IncidentJ’a small computer application(Applet)’.class” width=”300″ height=”300″>

 

</’a small computer application(Applet)’>

</body>

</html>

 

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.

 

JDialog class announcement

 

Let’s see the announcement for javax.swing.JDialog class.

 

  1. public class JDialog expand Dialog applies WindowConstants, Provided, RootPaneConta iner

Commonly used Creaters:

 

Creater                                                      Description

JDialog()  

It is used to produce a modeless dialog without a title and without a specified Frame owner.

JDialog(Frame owner) It is used to produce a modeless dialog with specified Frame as its owner and an empty title.
 

JDialog(Frame owner, String title, boolean modal)

 

It is used to produce a dialog with the specified title, owner Frame and modality.

 

Java JDialog Example

import javax.swing.*;

import java.awt.*;

import java.awt.incident.*;

public class DialogExample {

private static JDialog d;

DialogExample() {

JFrame f= new JFrame();

d = new JDialog(f , “Dialog Example”, true);

d.setSetup( new FlowSetup() );

JButton b = new JButton (“OK”);

b.addActionListener ( new ActionListener()

{

public void actionPull off( ActionIncident e )

{

DialogExample.d.setClear(false);

}

 

});

 

d.add( new JLabel (“Click button to continue.”));

d.add(b);

d.setSize(300,300);

d.setClear(true);

}

public static void main(String args[])

{

new DialogExample();

} }

 

JPanel

 

The JPanel is a simplest store(contain)er class. It gives space in which an application can reattach any other factor. It inherits the JFactors class.

 

It doesn’t have title bar.

 

JPanel class announcement

 

  1. public class JPanel expand JFactor applies Provided

 

Java JPanel Example

 

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.setSetup(null);

f.setClear(true);

}

public static void main(String args[])

{

new PanelExample();

} }

Overview of some Swing Factors Java JButton

The JButton class is used to produce a labeled button that has platform self-supported fulfilment. The application result in some action when the button is pushed. It inherits ConceptualButton class.

 

JButton class announcement

 

Let’s see the announcement for javax.swing.JButton class.

 

  1. public class JButton expand ConceptualButton applies Provided

 

Java JButton Example

 

import javax.swing.*;

public class ButtonExample {

public static void main(String[] args) {

JFrame f=new JFrame(“Button Example”);

JButton b=new JButton(“Click Here”);

b.setBounds(50,100,95,30);

f.add(b); f.setSize(400,400);

f.setSetup(null);

f.setClear(true);

}

}

Java JLabel

The object of JLabel class is a factor for placing text in a store(contain)er. 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. It inherits JFactor class.

 

JLabel class announcement

Let’s see the announcement for javax.swing.JLabel class.

 

  1. public class JLabel expand JFactor applies SwingConstants, Provided

Commonly used Creaters:

 

Creater

       

Description

JLabel() Produces a JLabel instance with no image and with an empty string for the title.
JLabel(String s) Produces a JLabel instance with the specified text.
JLabel(Icon i) Produces a JLabel instance with the specified image.
JLabel(String     s, horizontalAlignment) Icon i, int Produces a JLabel instance with the specified text, image, and horizontal alignment.

 

Commonly used Way(method) s:

 

Way(method) s                                                  Description

 

String getText()

 

t returns the text string that a label displays.

void setText(String text) It bounds the single line of text this factor will display.
void             setHorizontalAlignment(int alignment) It sets the alignment of the label’s contents along the X axis.
Icon getIcon() It returns the graphic image that the label displays.
 

int getHorizontalAlignment()

 

It returns the alignment of the label’s contents along the X axis.

 

Java JLabel Example

 

import javax.swing.*;

class LabelExample

{

public static void main(String args[])

{

JFrame f= new JFrame(“Label Example”);

JLabel l1,l2;

l1=new JLabel(“First Label.”);

l1.setBounds(50,50, 100,30);

l2=new JLabel(“Second Label.”);

l2.setBounds(50,100, 100,30);

f.add(l1); f.add(l2);

f.setSize(300,300);

f.setSetup(null);

f.setClear(true);

}

}

 

JTextField

 

The object of a JTextField class is a text factor that allows the editing of a single line text. It inherits JTextFactor class.

JTextField class announcement

 

Let’s see the announcement for javax.swing.JTextField class.

 

  1. public class JTextField expand JTextFactor applies SwingConstants

 

Java JTextField Example

 

import javax.swing.*;

class TextFieldExample

{

public static void main(String args[])

{

JFrame f= new JFrame(“TextField Example”);

JTextField t1,t2;

t1=new JTextField(“Welcome to Javatpoint.”);

t1.setBounds(50,100, 200,30);

t2=new JTextField(“AWT Tutorial”);

t2.setBounds(50,150, 200,30);

f.add(t1); f.add(t2);

f.setSize(400,400);

f.s etSetup(null);

f.setClear(true);

}

}

Java 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 JTextFactor class

JTextArea class announcement

Let’s see the announcement for javax.swing.JTextArea class.

 

  1. public class JTextArea expand JTextFactor

 

Java JTextArea Example

 

import javax.swing.*;

public class TextAreaExample

{

TextAreaExample(){

JFrame f= new JFrame();

JTextArea area=new JTextArea(“Welcome to programercoding.com”);

area.setBounds(10,30, 200,200);

f.add(area);

f.setSize(300,300);

f.setSetup(null);

f.setClear(true);

}

public static void main(String args[])

{

new TextAreaExample();

}}

 

 

 

Simple Java Applications

 

import javax.swing.JFrame;

import javax.swing.SwingUtilities;

public class Example expand JFrame { public Example() {

setTitle(“Simple example”);

setSize(300, 200);

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

 

public static void main(String[] args) {

Example ex = new Example();

ex.setClear(true);

}}

Setup Management

 

Java SetupManagers

The SetupManagers are used to arrange factors in a particular manner. SetupManager is an borderline that is applied by all the classes of setup managers.

 

BorderSetup

The BorderSetup gives five constants for each region:

 

1.        public static final int NORTH
  1. public static final int SOUTH
3.        public static final int EAST
  1. public static final int WEST
  2. public static final int CENTER

 

Creaters of BorderSetup class:

  • BorderSetup(): produces a border setup but with no gaps between the
  • JBorderSetup(int hgap, int vgap): produces a border setup with the given horizontal and vertical gaps between the

 

Example of BorderSetup class:

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,BorderSetup.NORTH);

f.add(b2,BorderSetup.SOUTH);

f.add(b3,BorderSetup.EAST);

f.add(b4,BorderSetup.WEST);

f.add(b5,BorderSetup.CENTER);

f.setSize(300,300);

f.setClear(true);

}

public static void main(String[] args)

{

new Border();

} }

 

Java GridSetup

 

The GridSetup is used to arrange the factors in rectangular grid. One factor is out put in each rectangle.

 

Creaters of GridSetup class

  1. GridSetup(): produces a grid setup with one column per factor in a
  2. GridSetup(int rows, int columns): produces a grid setup with the given rows and columns but no gaps between the
  3. GridSetup(int rows, int columns, int hgap, int vgap): produces a grid setup with the given rows and columns alongwith given horizontal and vertical

 

Example of GridSetup class

  1. import awt.*;
  2. import swing.*;
  3. public class MyGridSetup{
  4. JFrame f;

MyGridSetup(){ f=new JFrame();

JButton b1=new JButton(“1”);

JButton b2=new JButton(“2”);

JButton b3=new JButton(“3”);

JButton b4=new JButton(“4”);

JButton b5=new JButton(“5”);

JButton b6=new JButton(“6”);

JButton b7=new JButton(“7”);

JButton b8=new JButton(“8”);

JButton b9=new JButton(“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.setSetup(new GridSetup(3,3));

//setting grid setup of 3 rows and 3 columns f.setSize(300,300);

f.s etClear(true);

}

public static void main(String[] args) {

new MyGridSetup();

}

}

Java FlowSetup

 

The FlowSetup is used to arrange the factors in a line, one after another (in a flow). It is the default setup of ‘a small computer application(Applet)’ or panel.

 

Fields of FlowSetup class

1.        public static final int LEFT
  1. public static final int RIGHT
3.        public static final int CENTER
  1. public static final int LEADING
  2. public static final int TRAILING

 

Creaters of FlowSetup class

  1. FlowSetup(): produces a flow setup with centered alignment and a default 5 unit horizontal and vertical
  2. FlowSetup(int align): produces a flow setup with the given alignment and a default 5 unit horizontal and vertical
  3. FlowSetup(int align, int hgap, int vgap): produces a flow setup with the given alignment and the given horizontal and vertical

 

Example of FlowSetup class

import java.awt.*;

import javax.swing.*;

public class MyFlowSetup{

JFrame f;

MyFlowSetup(){

f=new JFrame();

JButton b1=new JButton(“1”);

JButton b2=new JButton(“2”);

JButton b3=new JButton(“3”);

JButton b4=new JButton(“4”);

JButton b5=new JButton(“5”);

f.add(b1);

f.add(b2);

f.add(b3);

f.add(b4);

f.add(b5);

f.setSetup(new FlowSetup(FlowSetup.RIGHT));

//setting flow setup of right alignment f.setSize(300,300);

f.s etClear(true);

}

public static void main(String[] args) {

new MyFlowSetup();

} }

Incident Handling

Incident and Listener (Java Incident Handling)

Changing the state of an object is known as an incident. For example, click on button, dragging mouse etc. The java.awt.incident package gives a lot of incident classes and Listener borderlines for incident handling.

 

Types of Incident

 

The incidents can be broadly classified into two categories:

 

  • Foreground Incidents – Those incidents which need the direct interaction of user.They are produced as consequences of a person interacting with the graphical factors in Graphical User Borderline. For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item from list, scrolling the page
  • Background Incidents – Those incidents that need the interaction of end user are known as

 

background incidents. Operating system interrupts, hardware or software failure, timer expires, an operation completion are the example of background incidents.

Incident Handling

 

Incident Handling is the tool that controls the incident and decides what should happen if an incident happens. This tool have the code which is known as incident handler that is applied when an incident happens. Java Uses the Delegation Incident Model to handle the incidents. This model bounds the typical tool to produce and handle the incidents.Let’s have a brief introduction to this model.

 

The Delegation Incident Model has the following key participants namely:

 

  • Source – The source is an object on which incident Source is responsible for providing information of the happenred incident to it’s handler. Java provide as with classes for source object.

 

  • Listener – It is also known as incident Listener is responsible for generating response to an incident. From java fulfilment point of view the listener is also an object. Listener waits until it receives an incident. Once the incident is received , the listener process the incident an then returns.

 

Incident classes and Listener borderlines:

Incident Classes                          Listener Borderlines
ActionIncident ActionListener
MouseIncident MouseListener and MouseMotionListener
MouseWheelIncident MouseWheelListener
KeyIncident KeyListener
ItemIncident ItemListener
TextIncident TextListener
AdjustmentIncident AdjustmentListener
WindowIncident WindowListener
FactorIncident FactorListener
Store(contain)erIncident Store(contain)erListener
FocusIncident FocusListener

 

Steps to apply Incident Handling

 

Following steps are need to apply incident handling:

 

  1. Implement the Listener borderline and overrides its way(method) s
  2. Register the factor with the Listener

 

For registering the factor with the Listener, a lot of classes provide the registration way(method) s. For example:

 

o    Button
  • public void addActionListener(ActionListener a){}
o    MenuItem
  • public void addActionListener(ActionListener a){}
o    TextField
  • public void addActionListener(ActionListener a){}
  • public void addTextListener(TextListener a){}
o    TextArea
  • public void addTextListener(TextListener a){}
o    Checkbox
  • public void addItemListener(ItemListener a){}
o    Choice
  • public void addItemListener(ItemListener a){}
o    List
  • public void addActionListener(ActionListener a){}
  • public void addItemListener(ItemListener a){}

 

Incident Handling Codes:

We can put the incident handling code into one of the following places:

  1. Same class
  2. Other class
  3. Annonymous class

 

Example of incident handling enclosed by class:

import java.awt.*;

import java.awt.incident.*;

class AIncident expand Frame applies ActionListener{ TextField tf;

 

AIncident(){

tf=new TextField();

tf.setBounds(60,50,170,20);

Button b=new Button(“click me”);

b.setBounds(100,120,80,30);

b.addActionListener(this);

add(b);

add(tf);

setSize(300,300);

setSetup(null);

setClear(true);

}

public void actionPull off(ActionIncident e){ tf.setText(“Welcome”);

}

public static void main(String args[]){

new AIncident();

} }

public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above example that sets the position of the factor it may be button, textfield etc.

 

Java incident handling by implementing ActionListener

 

import java.awt.*;

import java.awt.incident.*;

class AIncident expand Frame applies ActionListener{ TextField tf;

AIncident(){

//produce factors 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 factors and set size, setup and visibility add(b);

add(tf);

setSize(300,300);

setSetup(null);

setClear(true);

}

public void actionPull off(ActionIncident e){ tf.setText(“Welcome”);

}

public static void main(String args[]){

new AIncident();

Java KeyListener Borderline

 

The Java KeyListener is notified whenever you change the state of key. It is notified against KeyIncident. The KeyListener borderline is found in java.awt.incident package. It has three way(method) s.

 

Way(method) s of KeyListener borderline

 

The signature of 3 way(method) s found in KeyListener borderline are given below:

 

  1. public conceptual void keyPressed(KeyIncident e);
  2. public conceptual void keyReleased(KeyIncident e);
  3. public conceptual void keyTyped(KeyIncident e);

 

Java KeyListener Example

import java.awt.*;

import java.awt.incident.*;

public class KeyListenerExample expand Frame applies KeyListener{

Label l;

TextArea area;

KeyListenerExample(){

l=new Label();

l.setBounds(20,50,100,20);

area=new TextArea();

area.setBounds(20,80,300, 300);

area.addKeyListener(this);

add(l);

add(area);

setSize(400,400);

setSetup(null);

setClear(true);

}

public void keyPressed(KeyIncident e) {

l.setText(“Key Pressed”);

}

public void keyReleased(KeyIncident e) {

l.setText(“Key Released”);

}

public void keyTyped(KeyIncident e) {

l.setText(“Key Typed”);

}

public static void main(String[] args) {

new KeyListenerExample();

}

}

Java Adapter Classes

 

Java adapter classes provide the default fulfilment of listener borderlines. If you inherit the adapter class, you will not be forced to provide the fulfilment of all the way(method) s of listener borderlines. So it saves code.

 

java.awt.incident Adapter classes

 

 

Adapter class                                                              Listener borderline

WindowAdapter WindowListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
FactorAdapter FactorListener
Store(contain)erAdapter Store(contain)erListener
RankingBoundsAdapter RankingBoundsListener

 

Java WindowAdapter Example

 

  1. import java.awt.*;

import java.awt.incident.*;

public class AdapterExample{
Frame f;

AdapterExample(){

f=new Frame(“Window Adapter”);

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowIncident e) { f.dispose();

}

});

f.s etSize(400,400);

f.setSetup(null);

f.setClear(true);

}

public static void main(String[] args) {

new AdapterExample();

} }

 

‘a small computer application(Applet)’s

 

‘a small computer application(Applet)’ is a specific type of program that is embedded in the webpage to produce the dynamic content. It runs inside the browser and works at client side.

Advantage of ‘a small computer application(Applet)’

 

There are a lot of advantages of ‘a small computer application(Applet)’. They are as follows:

 

  • It works at client side so less response
  • Secured
  • It can be applied by browsers running under a lot of plateforms, including Linux, Windows, Mac Os

 

Drawback of ‘a small computer application(Applet)’

  • Plugin is needd at client browser to apply ‘a small computer application(Applet)’.

 

Lifecycle of Java ‘a small computer application(Applet)’ Ranking of ‘a small computer application(Applet)’

  1. ’a small computer application(Applet)’ is
  2. ‘a small computer application(Applet)’ is
  3. ‘a small computer application(Applet)’ is
  4. ‘a small computer application(Applet)’ is
  5. ‘a small computer application(Applet)’ is

 

Lifecycle way(method) s for ‘a small computer application(Applet)’:

 

The java.’a small computer application(Applet)’.’a small computer application(Applet)’ class 4 life cycle way(method) s and java.awt.Factor class gives 1 life cycle way(method) s for an ‘a small computer application(Applet)’.

java.’a small computer application(Applet)’.’a small computer application(Applet)’ class

 

For creating any ‘a small computer application(Applet)’ java.’a small computer application(Applet)’.’a small computer application(Applet)’ class must be inherited. It gives 4 life cycle way(method) s of ‘a small computer application(Applet)’.

 

  1. public void init(): is used to established the ‘a small computer application(Applet)’. It is produce only
  2. public void start(): is produce after the init() way(method) or browser is maximized. It is used to start the ‘a small computer application(Applet)’.
  3. public void stop(): is used to stop the ‘a small computer application(Applet)’. It is produce when ‘a small computer application(Applet)’ is stop or browser is
  4. public void destroy(): is used to destroy the ‘a small computer application(Applet)’. It is produce only

 

java.awt.Factor class

 

The Factor class gives 1 life cycle way(method)  of ‘a small computer application(Applet)’.

 

  1. public void paint(Diagram g): is used to paint the ‘a small computer application(Applet)’. It gives Diagram class object that can be used for drawing oval, rectangle, arc etc.

Simple example of ‘a small computer application(Applet)’ by html file:

 

To apply the ‘a small computer application(Applet)’ by html file, produce an ‘a small computer application(Applet)’ and compile it. After that produce an html file and place the ‘a small computer application(Applet)’ code in html file. Now click the html file.

 

  1. //First.java

import java.’a small computer application(Applet)’.

’a small computer application(Applet)’;

import java.awt.Diagram;

public class First expand ‘a small computer application(Applet)’{

public void paint(Diagram g){ g.drawString(“welcome”,150,150);

}

}

 

Simple example of ‘a small computer application(Applet)’ by ‘a small computer application(Applet)’viewer tool:

 

To apply the ‘a small computer application(Applet)’ by ‘a small computer application(Applet)’viewer tool, produce an ‘a small computer application(Applet)’ that insure  ‘a small computer application(Applet)’ tag in comment and compile it. After that run it by: ‘a small computer application(Applet)’viewer First.java. Now Html file is not need but it is for testing purpose only.

 

  1. //First.java

import java.’a small computer application(Applet)’.

’a small computer application(Applet)’;

import java.awt.Diagram;

public class First expand ‘a small computer application(Applet)’{

public void paint(Diagram g){ g.drawString(“welcome to ‘a small computer application(Applet)’”,150,150);

}

}

/*

<‘a small computer application(Applet)’ code=”First.class” width=”300″ height=”300″>

</’a small computer application(Applet)’>

*/

 

To apply the ‘a small computer application(Applet)’ by ‘a small computer application(Applet)’viewer tool, write in command prompt:

c:\>javac First.java

c:\>appletviewer First.java

 

Difference between ‘a small computer application(Applet)’ and Application programming

 

Parameter in ‘a small computer application(Applet)’

 

We can get any information from the HTML file as a parameter. For this purpose, ‘a small computer application(Applet)’ class gives a way(method)  named getParameter(). Syntax:

 

  1. public String getParameter(String parameterName)

 

Example of using parameter in ‘a small computer application(Applet)’:

 

  1. import ’a small computer application(Applet)’.
  2. ’a small computer application(Applet)’;
  3. import awt.Diagram;
  4. public class Use Param expand ‘a small computer application(Applet)’ {
  5. public void paint(Diagram g) 6. {
  6. String str=getParameter(“msg”);
  7. drawString(str,50, 50);
  8. } }
my’a small computer application(Applet)’.html
  1. <html>
  2. <body>
  3. <‘a small computer application(Applet)’ code=”UseParam.class” width=”300″ height=”300″>
  4. <param name=”msg” value=”Welcome to ‘a small computer application(Applet)’”>
  5. </’a small computer application(Applet)’>
  6. </body>
  7. </html>

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top