Showing posts with label Applets. Show all posts
Showing posts with label Applets. Show all posts

Sunday, November 4, 2012

Simple calculator using Java Applet and Event Handling

You can see all event methods in java.awt.Event class
and java.awt.event.ActionListener is an Interface that includes method named actionPerformed and takes ActionEvent as an argument.
Type javap java.awt.event.ActionListener in Command Prompt.


Step 1:Create a java source file with name calc.java that is name of java class and it is mandatory that class-name and file-name must be same.

Applet class must be public.

In java source file commented applet code will not be compile by java compiler but will executed by AppletViewer.

In command addActionListener(this) "this" keyword refers as object of current class. we can also write "new calc()" at place of "this" keyword.For making any TextField ReadOnly we have to use method TextField().setEnabled(false)
 import java.awt.*;  
 import java.applet.*;  
 import java.awt.event.*;  
 //<applet code="calc.class" width=150 height=300></applet>  
 public class calc extends Applet implements ActionListener  
 {  
 Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,ba,bs,br,bc;  
 TextField t1,t2,t3;  
 Label l1,l2,l3;  
 String m="";  
 int i,a;  
      public void init()  
      {  
      l1=new Label("Enter No.");  
      add(l1);  
      t1=new TextField(15);  
      add(t1);  
      b1=new Button("1");  
      b2=new Button("2");  
      b3=new Button("3");  
      b4=new Button("4");  
      b5=new Button("5");  
      b6=new Button("6");  
      b7=new Button("7");  
      b8=new Button("8");  
      b9=new Button("9");  
      b0=new Button("0");  
      ba=new Button("Add");  
      bs=new Button("Sub");  
      br=new Button("Result");  
      bc=new Button("Clear");  
      add(b1);  
      add(b2);  
      add(b3);  
      add(b4);  
      add(b5);  
      add(b6);  
      add(b7);  
      add(b8);  
      add(b9);  
      add(b0);  
      add(ba);  
      add(bs);  
      add(br);  
      add(bc);  
      b1.addActionListener(this);  
      b2.addActionListener(this);  
      b3.addActionListener(this);  
      b4.addActionListener(this);  
      b5.addActionListener(this);  
      b6.addActionListener(this);  
      b7.addActionListener(this);  
      b8.addActionListener(this);  
      b9.addActionListener(this);  
      b0.addActionListener(this);  
      ba.addActionListener(this);  
      bs.addActionListener(this);  
      br.addActionListener(this);  
      bc.addActionListener(this);  
      l2=new Label("Result");  
      add(l2);  
      t2=new TextField(15);  
      add(t2);  
      t2.setEnabled(false);  
      l3=new Label("Exception");  
      add(l3);  
      t3=new TextField(40);  
      add(t3);  
      t3.setEnabled(false);  
      }  
      public void actionPerformed(ActionEvent e)  
      {  
      try{  
      if(e.getActionCommand().equals("1"))  
           {     m+="1";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("2"))  
           {     m+="2";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("3"))  
           {     m+="3";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("4"))  
           {     m+="4";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("5"))  
           {     m+="5";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("6"))  
           {     m+="6";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("7"))  
           {     m+="7";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("8"))  
           {     m+="8";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("9"))  
           {     m+="9";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("0"))  
           {     m+="0";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("Add"))  
           {i=Integer.parseInt(t1.getText());       
                t1.setText("");  
                m="";  
                a=1;}  
      if(e.getActionCommand().equals("Sub"))  
           {i=Integer.parseInt(t1.getText());       
                t1.setText("");  
                m="";  
                a=2;}  
      if(e.getActionCommand().equals("Result"))  
           {     if(a==1)  
                {  
                int j=Integer.parseInt(t1.getText());  
                int k=i+j;  
                Integer z=new Integer(k);  
                String s=z.toString();  
                t2.setText(s);       
                t1.setText("");  
                m="";  
                }  
                else if(a==2)  
                {                 
                int j=Integer.parseInt(t1.getText());  
                int k=i-j;  
                Integer z=new Integer(k);  
                String s=z.toString();  
                t2.setText(s);       
                t1.setText("");  
                m="";  
                }  
                else  
                {}  
           }  
      if(e.getActionCommand().equals("Clear"))  
           {       
                t1.setText("");  
                t2.setText("");  
                t3.setText("");}  
      }  
      catch(Exception e1){  
      String s=e1.toString();  
      t3.setText(s);}  
      }  
 }                  

Step 2: Run cmd(command prompt) and compile java source file, calc.class file'll be build.

then give appletviewer command and applet will pop out.


output:


Download Code Link 1 (3 kb)
Download Code Link 2 (3 kb)













Also Read Simple calculator using Windows form Application with C# in VS2008

Wednesday, October 24, 2012

Login using applet class and ActionListener interface


You can see all event methods in java.awt.Event class
and java.awt.event.ActionListener is an Interface that includes method named actionPerformed and takes ActionEvent as an argument.



Step 1:Create a java source file with name Login.java that is name of java class and it is mandatory that class-name and file-name must be same.
Applet class must be public.

In java source file commented applet code will not be compile by java compiler but will executed by AppletViewer.
In command addActionListener(this) "this" keyword refers as object of current class. we can also write "new Login()" at place of "this" keyword.
setEchoChar() is a method that makes password hidden.



Step 2: Run cmd(command prompt) and compile java source file, Login.class file'll be build.

then give appletviewer command and applet will pop out.

output: when you click on login then related output will display on console.

 

Sunday, October 21, 2012

Event handling using applet class and FocusListener interface


You can see all event methods in java.awt.Event class and java.awt.event.FocusListener is an Interface that includes methods named focusGained() and focusLost() and both takes FocusEvent as an argument.
javap java.awt.event.FocusListener(type over command prompt)

echo %path%(type over command prompt to check path)


Step 1:Create a java source file with name SecondEvent.java that is name of java class and it is mandatory that class name and file name must be same.

Applet class must be public.

In java source file commented applet code will not be compile by java compiler but will executed by AppletViewer.
In command addFocusListener(this) "this" keyword refers as object of current class. we can also write "new SecondEvent()" at place of "this" keyword.

Step 2: Run cmd(command prompt) and compile java source file, SecondEvent.class file'll be build.

then give appletviewer command and applet will pop out.


Download Code Link 1
Download Code Link 2

output:
Focus Gained
Focus Lost

Event handling using applet class and ActionListener interface


You can see all event methods in java.awt.Event class and java.awt.event.ActionListener is an Interface that includes method named actionPerformed and takes ActionEvent as an argument.
javap java.awt.event.ActionListener(type over command prompt)



Step 1:Create a java source file with name FirstEvent.java that is name of java class and it is mandatory that class-name and file name must be same.

Applet class must be public.

In java source file commented applet code will not be compile by java compiler but will executed by AppletViewer.
In command addActionListener(this) "this" keyword refers as object of current class. we can also write "new FirstEvent()" at place of "this" keyword.


Step 2: Run cmd(command prompt) and compile java source file, FirstEvent.class file'll be build.

then give appletviewer command and applet will pop out.
Download Code Link 1
Download Code Link 2
output: click first and second button

Thursday, October 11, 2012

component class methods mouseEnter,mouseExit and action used with applet


Step 1:Create a java source file with name MyButton.java that is name of java class and it is mandatory that classname and filename must be same.
Applet class must be public.

Step 2:run cmd(command prompt) and compile java source file, MyButton.class file'll be build.

then give appletviewer command and applet will pop out.
Download Code Link 1
Download Code Link 2
output:

Explore Applet by creating textbox, checkbox, list, radio button


Step 1:Create a java source file with name MyAction.java that is name of java class and it is mandatory that classname and filename must be same.
Applet class must be public.


Step 2: Run cmd(command prompt) and compile java source file, MyAction.class file'll be build.

then give appletviewer command and applet will pop out.



output:

moving banner using Applet class and Runnable Interface


Step 1:Create a java source file with name Movingb.java that is name of java class and it is mandatory that classname and filename must be same.
Applet class must be public.
In java source file commented applet code will not be compile by java compiler but will executed by AppletViewer.

Step 2: Run cmd(command prompt) and compile java source file, Movingb.class file'll be build.

then give appletviewer command and applet will pop out.
paint() is a method of java.awt.Component class which is a child of java.lang.Object class and parent of java.awt.Container class
Download Code Link 1
Download Code Link 2
output:

Tuesday, October 9, 2012

Handling image using Applets

Step 1: Create a java source file with name ImageExample.java that is name of java class and it is mandatory that classname and filename must be same.
Applet class must be public.

Step 2: Run cmd(command prompt) and compile java source file, ImageExample.class file'll be build.

then give appletviewer command and applet will pop out.
paint() is a method of java.awt.Component class which is a child of java.lang.Object class and parent of java.awt.Container class

Download Code Link 1
Download Code Link 2

output:

Applet lifecycle

Step 1:
Class a=Class.forName("AppletDemo");

Step 2:Instantiate the class

Applet a1=(Applet)a.newInstance();

Step 3:Call init() Method


Step 4:Call start() Method


Step 5:Call paint() Method


Step 6:Call stop() Method


Step 7:Call destroy() Method

Applet using html

Step 1: Create a java source file with name AppletDemo.java that is name of java class and it is mandatory that classname and filename must be same.
Applet class must be public.

Step 2: Run cmd(command prompt) and compile java source file, AppletDemo.class file'll be build.



Step 3: Double click on html file.


Download Code Link 1
Download Code Link 2
output:

Basic Applet Program

Step 1: Create a java source file with name AppletDemo.java that is name of java class and it is mandatory that classname and filename must be same.
Applet class must be public.

Step 2: Run cmd(command prompt) and compile java source file, AppletDemo.class file'll be build.
then give appletviewer command and applet will pop out.

output:

Popular Posts