Java Applets and Awt Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 48

1.

AIM:
Write a java program on applet life cycle
PROGRAM:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
/*
<applet code="AppletLifeCycle.class" width="350" height="150">
</applet>
*/
public class AppletLifeCycle extends Applet
{
public void init()
{
setBackground(Color.CYAN);
System.out.println("init() called");
}
public void start()
{
System.out.println("Start() called");
}
public void paint(Graphics g)
{
System.out.println("Paint(() called");
}
public void stop()
{
System.out.println("Stop() Called");
}
public void destroy()
{
System.out.println("Destroy)() Called");
}
}
OUTPUT:
2. AIM:
Write a java program to take user input using applets
PROGRAM:
import java.awt.*;
import java.applet.*;
/*
<TITLE>Getting Input from the User</TITLE>
<APPLET Code="GettingInputfromtheUser.class" Width=400 Height=300>
</APPLET>
*/
public class GettingInputfromtheUser extends Applet
{
TextField t1;
public void init()
{
t1 = new TextField(10);
add(t1);
t1.setText("0");
}
public void paint(Graphics g)
{
int a=0,c=0;
String str1,str;
g.drawString("Entered number in box",10,50);
try
{
str1=t1.getText();
a=Integer.parseInt(str1);
}
catch(Exception e)
{
}
c=a;
str=String.valueOf(c);
g.drawString("enter num is",10,15);
g.drawString(str,100,75);
}
}
OUTPUT:

3. AIM:
Write a java program on Graphics class
PROGRAM:
import java.awt.*;
import java.applet.*;
/*<applet code="drawShapes.class" height=400 width=400>
</applet>*/
public class drawShapes extends Applet
{
public void paint(Graphics g)
{
g.drawString("Some of the drawing objects ",40,20);
g.setColor(Color.red);
g.drawLine(40,30,200,30);
g.setColor(Color.blue);
g.drawRect(40,60,70,40);
g.setColor(Color.green);
g.fillRect(140,60,70,40);
g.setColor(Color.blue);
g.drawRoundRect(240,60,70,40,10,20);
g.setColor(Color.blue);
g.fillRoundRect(40,120,70,40,10,20);
g.setColor(Color.pink);
g.draw3DRect(140,120,70,40,true);
g.setColor(Color.orange);
g.drawOval(240,120,70,40);
g.setColor(Color.pink);
g.fillOval(40,180,70,40);
g.setColor(Color.black);
g.drawArc(140,180,70,40,0,180);
g.setColor(Color.green);
g.fillArc(40,180,70,40,0,-180);
}
}
OUTPUT:

4. AIM:
Write a Java program on Frames using Applets
PROGRAM:
/*<HTML>
<HEAD>
<TITLE>My First Java Applet Program</TITLE>
</HEAD>
<BODY>
<APPLET Code="FirstJavaApplet.class" Width=300 Height=200>
</APPLET>
</BODY>
</HTML>*/
import java.awt.*;
import java.applet.*;
public class FirstJavaApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("My First Java Applet Program",100,100);
}
}

OUTPUT:
5. AIM:
Write a Java program on buttons using applets
PROGRAM:
import java.awt.*;
import java.applet.*;

/*
<APPLET Code="ButtonTest" Width=500 Height=200>
</APPLET>
*/

public class ButtonTest extends Applet


{
public void init( )
{
Label l1;
Button btnRewind,btnPlay,btnFforward,btnStop;
Font f1=new Font("New Times Roman",Font.BOLD,16);
Font f2=new Font("New Times Roman",Font.BOLD|
Font.ITALIC,12);

Color c1=new Color(100,100,250);

l1 = new Label("Creating Buttons");

btnRewind = new Button("Rewind");


btnPlay = new Button("Play");
btnFforward = new Button("Fast Forward");
btnStop = new Button("Stop");

l1.setForeground(c1);
btnRewind.setForeground(c1);
btnPlay.setForeground(c1);
btnFforward.setForeground(c1);
btnStop.setForeground(c1);

l1.setFont(f1);

btnRewind.setFont(f2);
btnPlay.setFont(f2);
btnFforward.setFont(f2);
btnStop.setFont(f2);

add(l1);
add(btnRewind);
add(btnPlay);
add(btnFforward);
add(btnStop);
}
}
OUTPUT:
6. AIM:
Write a Java program on Choices using applets
PROGRAM:
import java.awt.*;
import java.applet.*;
/*
<APPLET Code="ChoiceTest" Width=500 Height=200>
</APPLET>
*/

public class ChoiceTest extends Applet


{
public void init( )
{
Choice ch = new Choice();
ch.add("apples");
ch.add("oranges");
ch.add("strawberies");
ch.add("bananas");
add(ch );
}
}
OUTPUT:

7. AIM:
Write a Java program on Checkboxes using applets
PROGRAM:

import java.awt.*;
import java.applet.*;
/*
<APPLET Code="CheckboxTest" Width=500 Height=200>
</APPLET>
*/
public class CheckboxTest extends Applet
{
public void init( )
{
/*The statement creates an object and
adds the checkbox on the applet.*/

Checkbox chkShoes,chkSocks,chkShirt;
chkShoes = new Checkbox("Shoes");
chkSocks = new Checkbox("Socks", true); //This checkbox is checked
chkShirt = new Checkbox("Shirt");

add(chkShoes);
add(chkSocks);
add(chkShirt);
}
}
OUTPUT:
8. AIM:
Write a Java program on checkbox group using applets
PROGRAM:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="CBGroup" width=250 height=200></applet>*/
public class CBGroup extends Applet implements ItemListener
{
String msg="";
Checkbox Red,Green,Yellow;
CheckboxGroup cbg;
public void init()
{
cbg=new CheckboxGroup();
Red=new Checkbox("RED",cbg,false);
Green=new Checkbox("GREEN",cbg,false);
Yellow=new Checkbox("YELLOW",cbg,false);
add(Red);
add(Yellow);
add(Green);
Red.addItemListener(this);
Green.addItemListener(this);
Yellow.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
if(cbg.getSelectedCheckbox().getLabel()=="RED")
{
g.setColor(Color.red);
g.fillOval(30,30,50,50);
} if(cbg.getSelectedCheckbox().getLabel()=="YELLOW")
{
g.setColor(Color.yellow);
g.fillOval(30,30,50,50);
}
if(cbg.getSelectedCheckbox().getLabel()=="GREEN")
{
g.setColor(Color.green);
g.fillOval(30,30,50,50);
}}}
OUTPUT:
9. AIM:
Write a Java program on Scrollbar using applets
PROGRAM:
import java.awt.*;
import java.applet.*;
/*
<APPLET Code="Scroll" Width=500 Height=200>
</APPLET>
*/

public class Scroll extends Applet


{
Scrollbar bar = new Scrollbar(Scrollbar.VERTICAL, 10, 0, 1, 100);

public void init( )


{
add(bar);
}
}
OUTPUT:
10. AIM:
Write a Java program Labels and Textfield using applets
PROGRAM:
import java.awt.*;
import java.applet.*;

/*
<APPLET Code="TextFieldTest" Width=500 Height=200>
</APPLET>
*/

public class TextFieldTest extends Applet


{
public void init( )
{
Label lblName = new Label("enter name");
Label lblPhone = new Label("enter phone number");
Label lblPasswd = new Label("enter password");

TextField txtName = new TextField("your name here", 20);


TextField txtPhone = new TextField(12);

add(lblName);
add(txtName);
add(lblPhone);
add(txtPhone);
}
}
OUTPUT:

11. AIM:
Write a Java program on frames using 2 method
PROGRAM:
import java.awt.*;
import java.awt.event.*;
class framedemo extends Frame
{
public static void main(String[] args)
{
framedemo f=new framedemo();
f.setSize(400,300);
f.setTitle("My Frame ");
f.setVisible(true);
//to close the frame the code is
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
OUTPUT:

12. AIM:
Write a Java program to display message in a frame with font, color.
PROGRAM:
import java.awt.*;
import java.awt.event.*;
class dispmsginframe extends Frame
{
//Frame f = new Frame();
public void paint(Graphics g)
{
//display a background color
this.setBackground(new Color(100,20,20));
g.setColor(Color.green);
Font f=new Font("sansserif",Font.BOLD+Font.ITALIC,36);
g.setFont(f);
g.drawString("Hello Students !",50,100);
}

public static void main(String[] args)


{
dispmsginframe mf=new dispmsginframe();
mf.setTitle("My Message");
mf.setSize(600,450);
mf.setVisible(true);
mf.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
OUTPUT:

13. AIM:
Write a Java program for List Box
PROGRAM:
import java.awt.*;
import java.awt.event.*;
class Listdemo extends Frame implements ItemListener
{
String msg[];
List ch;
//Choice ch;

Listdemo()
{
setLayout(new FlowLayout());
// ch=new Choice();
ch=new List(3,true);

//add items to the choice

ch.add(" IDLY ");


ch.add(" DOSHA ");
ch.add(" POORI ");
ch.add(" VADA ");
ch.add(" VEG BIRYANI ");
ch.add(" TEA ");
ch.add(" COFFEE ");
add(ch);
ch.addItemListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString(" U HAVE SELECTED = ",20,100);
//msg=ch.getSelectedItem();
//g.drawString(msg,20,120);
msg=ch.getSelectedItems();
for(int i=0;i<msg.length;i++)
g.drawString(msg[i],20,120+i*20);
}

public static void main(String[] args)


{
Listdemo cd=new Listdemo();
cd.setTitle("MY CHOICEBOX ");
cd.setSize(600,500);
cd.setVisible(true);

}
}

OUTPUT:
14. AIM:
Write a Java program for choice
PROGRAM:
import java.awt.*;
import java.awt.event.*;
class Choicedemo extends Frame implements ItemListener
{
String msg;
Choice ch;

Choicedemo()
{
setLayout(new FlowLayout());
ch=new Choice();

//add items to the choice

ch.add(" IDLY ");


ch.add(" DOSHA ");
ch.add(" POORI ");
ch.add(" VADA ");
ch.add(" VEG BIRYANI ");
ch.add(" TEA ");
ch.add(" COFFEE ");
add(ch);
ch.addItemListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
g.drawString(" U HAVE SELECTED = ",20,100);
msg=ch.getSelectedItem();
g.drawString(msg,20,120);
}
public static void main(String[] args)
{
Choicedemo cd=new Choicedemo();
cd.setTitle("MY CHOICEBOX ");
cd.setSize(600,500);
cd.setVisible(true);

OUTPUT:
15. AIM:
Write a Java program on Buttons
PROGRAM:
import java.awt.*;
import java.awt.event.*;
class Buttonsdemo extends Frame implements ActionListener
{
Button b1,b2,b3;
Buttonsdemo()
{
setLayout(null);

b1=new Button("yellow");
b2=new Button("Blue");
b3=new Button("Red");
// specify locations
b1.setBounds(50,50,75,40);
b2.setBounds(50,100,75,40);
b3.setBounds(50,150,75,40);
//add buttons in frame
add(b1);
add(b2);
add(b3);
//add action listeneres to the buttons
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

// code to close the frame


addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("yellow"))
setBackground(Color.yellow);
if(str.equals("Blue"))
setBackground(Color.blue);
if(str.equals("Red"))
setBackground(Color.red);
}

public static void main(String[] args)


{
Buttonsdemo b=new Buttonsdemo();
b.setTitle("My Buttons");
b.setSize(600,500);
b.setVisible(true);
}

}
OUTPUT:
16. AIM:
Write a Java program for creating checkboxes
PROGRAM:
import java.awt.*;
import java.awt.event.*;
class Checkboxdemo extends Frame implements ItemListener
{
Checkbox c1,c2,c3;
String msg;
Checkboxdemo()
{
setLayout(new FlowLayout());

c1=new Checkbox("Bold",null,true);
c2=new Checkbox("Italic");
c3=new Checkbox("Underline");

//add checkbox to frame

add(c1);
add(c2);
add(c3);

// add action listener to the checkbox

c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);

// code to close the frame


addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}

public void paint(Graphics g)


{
g.drawString("Checkbox State=",20,100);
msg="Bold : "+c1.getState();
g.drawString(msg,20,120);
msg="Italic : "+c2.getState();
g.drawString(msg,20,140);
msg="Underline : "+c3.getState();
g.drawString(msg,20,160);
}

public static void main(String[] args)


{
Checkboxdemo cb=new Checkboxdemo();
cb.setTitle("My Checkboxes");
cb.setSize(500,400);
cb.setVisible(true);

}
}

OUTPUT:
17. AIM:
Write a Java program on checkbox group using listeners
PROGRAM:
import java.awt.*;
import java.awt.event.*;
class RadioButtonItemEvent extends Frame implements ItemListener
{
Checkbox r1,r2;
CheckboxGroup cg;
public RadioButtonItemEvent()
{
ShowGUI();
}
private void ShowGUI()
{
setTitle("Checkbox with ItemListener demo");
setLayout(new FlowLayout());
r1=new Checkbox("Male");
r2=new Checkbox("Female");
r1.addItemListener(this);
r2.addItemListener(this);
cg=new CheckboxGroup();
r1.setCheckboxGroup(cg);
r2.setCheckboxGroup(cg);
add(r1);
add(r2);
setSize(400,400);
setVisible(true);
}
public void itemStateChanged(ItemEvent ie)
{
setTitle(ie.getItem()+" is selected.");
}
public static void main(String args[])
{
new RadioButtonItemEvent();
}
}
OUTPUT:

18. AIM:
Write a Java program on labels and textfield
PROGRAM:
import java.awt.*;
import java.awt.event.*;
class Textdemo extends Frame implements ActionListener
{
Label n,p;
TextField name,pass;
Textdemo()
{
setLayout(new FlowLayout());

// create 2 lables

n=new Label("Enter Name=",Label.RIGHT);


p=new Label("Enter passwor=",Label.RIGHT);

// create 2 text fields

name=new TextField(20);
pass=new TextField(20);

name.setBackground(Color.yellow);
name.setForeground(Color.red);
Font f=new Font("Arial",Font.PLAIN,30);
name.setFont(f);

//hide the password

pass.setEchoChar('*');

//add these to the frame

add(n);
add(name);
add(p);
add(pass);

//add actionlistener to the name,pass

name.addActionListener(this);
pass.addActionListener(this);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
repaint();
}

public void paint(Graphics g)


{
g.drawString("NAME ="+name.getText(),50,200);
g.drawString("PASSWORD ="+pass.getText(),50,220);
}

public static void main(String[] args)


{
Textdemo td=new Textdemo();
td.setSize(500,400);
td.setTitle("MY TEXT FIELDS ");
td.setVisible(true);

}
}
1 OUTPUT:
19. AIM:
Write a Java program keyboard events
PROGRAM:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Key" width=300 height=400>
</applet>
*/
public class Key extends Applet
implements KeyListener
{
int X=20,Y=30;
String msg="KeyEvents--->";
public void init()
{
addKeyListener(this);
requestFocus();
setBackground(Color.green);
setForeground(Color.blue);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyDown");
int key=k.getKeyCode();
switch(key)
{
case KeyEvent.VK_UP:
showStatus("Move to Up");
break;
case KeyEvent.VK_DOWN:
showStatus("Move to Down");
break;
case KeyEvent.VK_LEFT:
showStatus("Move to Left");
break;
case KeyEvent.VK_RIGHT:
showStatus("Move to Right");
break;
}
repaint();
}
public void keyReleased(KeyEvent k)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent k)
{
msg+=k.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}
OUTPUT:
20. AIM:
Write a Java program on Mouse Events
PROGRAM:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class mouse4 extends JFrame implements
MouseListener,MouseMotionListener
{
JLabel l;
public static void main(String args[])
{
mouse4 obj=new mouse4();
}
public mouse4()
{
Container c=getContentPane();
l=new JLabel();
c.add(l);
addMouseListener(this);
addMouseMotionListener(this);
setSize(500,300);
show();
}
public void mouseClicked(MouseEvent e)
{
l.setText("you have clicked at ("+e.getX()+","+e.getY()+")");
}
public void mousePressed(MouseEvent e)
{
l.setText("you have pressed at ("+e.getX()+","+e.getY()+")");
}
public void mouseReleased(MouseEvent e)
{
l.setText("you have released at ("+e.getX()+","+e.getY()+")");
}

public void mouseEntered(MouseEvent e)


{
l.setText("mouse is in window");
}
public void mouseExited(MouseEvent e)
{
l.setText("mouse is out of window");
}
public void mouseDragged(MouseEvent e)
{
l.setText("you have dragged at ("+e.getX()+","+e.getY()+")");
}
public void mouseMoved(MouseEvent e)
{
l.setText("you have moved at ("+e.getX()+","+e.getY()+")");
}
}

OUTPUT:
21. AIM:
Write a Java program on canvas
PROGRAM:
import java.awt.*;
public class CanEx
{
public CanEx()
{
Frame f= new Frame("can sunshine ex");
f.add(new MyCanvas());
f.setLayout(null);
f.setSize(400,400);
f.show();
}
public static void main(String s[])
{
new CanEx();
}
}
class MyCanvas extends Canvas
{
public MyCanvas()
{
setBackground(Color.GRAY);
setSize(300,200);
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(75,75,150,75);
}
}
OUTPUT:

22. AIM:
Write a Java program on Scrollbars
PROGRAM:
import java.awt.*;
class ScrollBarExample extends Frame
{
ScrollBarExample()
{
setLayout(new FlowLayout());
//Horizontal Scrollbar with min value 0,max value 100,initial
value 50 and visible amount 10
Label lblHor =new Label("Horizontal Scrollbar");
Scrollbar sl = new
Scrollbar(Scrollbar.HORIZONTAL,50,10,0,100);
//Vertical Scrollbar with min value 0,max value 255,initial
value 10 and visible amount 5
Label lblver =new Label("Vertical Scrollbar");
Scrollbar s2 = new Scrollbar(Scrollbar.VERTICAL,10,5,0,100);
add(lblHor); add(sl);
add(lblver); add(s2);
}
}
class ScrollBarJavaExample
{
public static void main(String args[])
{
ScrollBarExample frame = new ScrollBarExample();
frame.setTitle("Scrollbar in Java Example");
frame.setSize(520,200);
frame.setVisible(true);
}
}
OUTPUT:

23. AIM:
Write a Java program on panels
PROGRAM:
import java.awt.*;
public class PanelExample {
PanelExample()
{
Frame f= new Frame("Panel Example");
Panel panel=new Panel();
panel.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
Button b1=new Button("Button 1");
b1.setBounds(50,100,80,30);
b1.setBackground(Color.yellow);
Button b2=new Button("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();
}
}
OUTPUT:

24. AIM:
Write a Java program on Menu Creation
PROGRAM:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MenuCreation extends JFrame implements ActionListener
{
JMenuBar mb;
JMenu file,edit,font;
JMenuItem op,cl,cp,pt;
JCheckBoxMenuItem pr;
MenuCreation()
{
Container c=getContentPane();
c.setLayout(new BorderLayout());
mb=new JMenuBar();
c.add("North",mb);
file=new JMenu("File");
edit=new JMenu("Edit");
mb.add(file);
mb.add(edit);
op=new JMenuItem("Open");
cl=new JMenuItem("Close");
cp=new JMenuItem("Copy");
pt=new JMenuItem("Paste");
file.add(op);
file.add(cl);
edit.add(cp);
edit.add(pt);
cl.setEnabled(true);

pr=new JCheckBoxMenuItem("Print");
file.add(pr);
file.addSeparator();
font=new JMenu("Font");
file.add(font);
font.add("Arial");
font.add("Times New Roman");
op.addActionListener(this);
cl.addActionListener(this);
cp.addActionListener(this);
pt.addActionListener(this);
pr.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
if(op.isArmed())
//this.openFile();
System.out.println("Open selected file");
if(cl.isArmed())
System.out.println("close selected");
if(cp.isArmed())
System.out.println("Copy selected");
if(pt.isArmed())
System.out.println("Paste is selected");
if(pr.getModel().isSelected())
System.out.println("Printing is started ...");
}
public static void main(String args[])
{
MenuCreation mc=new MenuCreation();
mc.setSize(500,400);
mc.setVisible(true);
}
}
OUTPUT:
25. AIM:
Write a Java program on Window Adapter class
PROGRAM:
import java.awt.*;
import java.awt.event.*;
public class AdapterExample{
Frame f;
AdapterExample(){
f=new Frame("Window Adapter");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
f.dispose();
}
});
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new AdapterExample();
}
}
OUTPUT:
26. AIM:
Write a Java program on Calculator
PROGRAM:

import java.awt.*;

import java.awt.event.*;

class Cal extends Frame implements ActionListener

String msg=" ";

int v1,v2,result;

TextField t1;

Button b[]=new Button[10];

Button add,sub,mul,div,clear,mod,EQ;

char OP;

Cal()

Color k=new Color(120,89,90);

setBackground(k);

t1=new TextField(10);

GridLayout gl=new GridLayout(4,5);

setLayout(gl);

for(int i=0;i<10;i++)

{
b[i]=new Button(""+i);

add=new Button("add");

sub=new Button("sub");

mul=new Button("mul");

div=new Button("div");

mod=new Button("mod");

clear=new Button("clear");

EQ=new Button("EQ");

t1.addActionListener(this);

add(t1);

for(int i=0;i<10;i++)

add(b[i]);

add(add);

add(sub);

add(mul);

add(div);

add(mod);

add(clear);

add(EQ);

for(int i=0;i<10;i++)

b[i].addActionListener(this);
}

add.addActionListener(this);

sub.addActionListener(this);

mul.addActionListener(this);

div.addActionListener(this);

mod.addActionListener(this);

clear.addActionListener(this);

EQ.addActionListener(this);

addWindowListener(new WindowAdapter()

public void windowClosing(WindowEvent e)

System.exit(0);

});

public void actionPerformed(ActionEvent ae)

String str=ae.getActionCommand();

char ch=str.charAt(0);

if ( Character.isDigit(ch))

t1.setText(t1.getText()+str);

else

if(str.equals("add"))
{

v1=Integer.parseInt(t1.getText());

OP='+';

t1.setText("");

else if(str.equals("sub"))

v1=Integer.parseInt(t1.getText());

OP='-';

t1.setText("");

else if(str.equals("mul"))

v1=Integer.parseInt(t1.getText());

OP='*';

t1.setText("");

else if(str.equals("div"))

v1=Integer.parseInt(t1.getText());

OP='/';

t1.setText("");

else if(str.equals("mod"))

{
v1=Integer.parseInt(t1.getText());

OP='%';

t1.setText("");

if(str.equals("EQ"))

v2=Integer.parseInt(t1.getText());

if(OP=='+')

result=v1+v2;

else if(OP=='-')

result=v1-v2;

else if(OP=='*')

result=v1*v2;

else if(OP=='/')

result=v1/v2;

else if(OP=='%')

result=v1%v2;

t1.setText(""+result);

if(str.equals("clear"))

t1.setText("");

public static void main(String[] args)


{

Cal c=new Cal();

c.setTitle("My CALCULATOR");

c.setSize(600,500);

c.setVisible(true);

OUTPUT:
27. AIM:
Write a Java program on Dialog box
PROGRAM:
import java.awt.*;
import java.awt.event.*;
public class DilogEx
{
private static Dialog d;
DilogEx()
{
Frame f= new Frame();
d=new Dialog(f,"sunshine ",true);
d.setLayout(new FlowLayout());
Button b=new Button("ok");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
DilogEx.d.setVisible(false);
}
});
d.add(new Label("click ok to continue"));
d.add(b);
d.setSize(300,300);
d.show();
}
public static void main(String s[])
{
new DilogEx();
}
}
OUTPUT:

You might also like