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

Java GUI Practical

This document contains code for several Java applets and applications that perform mathematical operations like calculating factorials, determining if a number is prime, and basic calculator operations. The code samples show how to create GUI elements like buttons, text fields, and labels, add action listeners, and perform number calculations when buttons are clicked.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Java GUI Practical

This document contains code for several Java applets and applications that perform mathematical operations like calculating factorials, determining if a number is prime, and basic calculator operations. The code samples show how to create GUI elements like buttons, text fields, and labels, add action listeners, and perform number calculations when buttons are clicked.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

1.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="myfact" width=300 height=300>
</applet>
*/
public class myfact extends Applet implements ActionListener
{
String msg="";
Button b1,b2;
Label l1;
TextField t1;

public void init()


{
setLayout(null);
setFont(new Font("SansSerif", Font.BOLD, 16));

b1=new Button("Factorial");
b2=new Button("clear");
l1=new Label("Enter the Number : ");
t1=new TextField(20);

add(l1);
add(t1);
add(b1);
add(b2);

l1.setBounds(40,50,160,30);
t1.setBounds(200,50,80,28);
b1.setBounds(100,110,100,40);
b2.setBounds(230,110,100,40);

t1.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent obj)
{
int n=Integer.parseInt(t1.getText());
String str=obj.getActionCommand();
if(str.equals("Factorial"))
{
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
msg="Factorial is : "+fact;
}
else if(str.equals("clear"))
{
t1.setText("");
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,60,200);
}
}

*************************************************************************************
2.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="operation" width=300 height=300>
</applet>
*/
public class operation extends Applet implements ActionListener
{
String msg="";
Button b1,b2,b3,b4;
Label l1;
TextField t1,t2;

public void init()


{
setLayout(null);
setFont(new Font("SansSerif", Font.BOLD, 16));
l1=new Label("Enter the Number : ");
t1=new TextField(20);
t2=new TextField();

b1=new Button("Factorial");
b2=new Button("Prime");
b3=new Button("Square");
b4=new Button("Circle");

add(l1);
add(t1);
add(b1);
add(b2);
add(b3);
add(b4);
add(t2);

l1.setBounds(40,50,160,30);
t1.setBounds(200,50,80,28);

b1.setBounds(40,150,80,30);
b2.setBounds(130,150,80,30);
b3.setBounds(220,150,80,30);
b4.setBounds(310,150,80,30);
t2.setBounds(80,200,220,30);

t1.addActionListener(this);
t2.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent obj)
{
int n=Integer.parseInt(t1.getText());
String str = obj.getActionCommand();
if(str.equals("Factorial"))
{
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
msg=" Factorial is : "+fact;
}
else if(str.equals("Prime"))
{
int f=1;
for(int i=2;i<n;i++)
{
if(n%i==0)
{
f=0;
break;
}
}
if(f==1)
{
msg=" Prime";
}
else
{
msg=" Not Prime";
}
}
else if(str.equals("Square"))
{
int sum=n*n;
msg=" Square is : "+sum;
}
else if(str.equals("Circle"))
{
double sum=3.14*n*n;
msg=" Area of Circle is : "+sum;
}
repaint();
}
public void paint(Graphics g)
{
t2.setText(msg);
}
}

*********************************************************************************
3.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="basic2" width=300 height=300>
</applet>
*/
public class basic2 extends Applet implements ActionListener,ItemListener
{
Choice c1;
String msg="";
TextField t1,t2;
Label l1,l2,l3;
public void init()
{
setLayout(null);
setFont(new Font("SansSerif", Font.BOLD, 14));

l1=new Label("First Number :");


l2=new Label("Second Number :");
l3=new Label("Result :");
t1=new TextField();
t2=new TextField();
c1=new Choice();

c1.add("+");
c1.add("-");
c1.add("x");
c1.add("/");

l1.setBounds(50,50,120,30);
t1.setBounds(190,50,100,30);
l2.setBounds(50,90,120,30);
t2.setBounds(190,90,100,30);
l3.setBounds(50,130,120,30);
c1.setBounds(190,130,100,30);

add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(c1);
t1.addActionListener(this);
t2.addActionListener(this);
c1.addItemListener(this);
}
public void actionPerformed(ActionEvent obj1)
{
repaint();
}
public void itemStateChanged(ItemEvent obj2)
{
int n3;
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
String str=c1.getSelectedItem();
if(str.equals("+"))
{
n3=n1+n2;
msg="Addition is : "+n3;
}
else if(str.equals("-"))
{
n3=n1-n2;
msg="Subtraction is : "+n3;
}
else if(str.equals("x"))
{
n3=n1*n2;
msg="Multiplication is : "+n3;
}
else if(str.equals("/"))
{
n3=n1/n2;
msg="Division is : "+n3;
}
repaint();
}
public void paint(Graphics g)
{

g.drawString(msg,70,200);
}
}

*******************************************************************************
4. Factorial Using Frame Class

import java.awt.*;
import java.awt.event.*;

class ex2 extends Frame implements ActionListener


{
Label l1,l2,l3;
TextField t1;
Button b1,b2;
GridLayout g;
Panel p;
ex2()
{
super("Factorial of a number");
p=new Panel();
setLayout(new FlowLayout());
setSize(500,400);
setVisible(true);
b1=new Button("Find");
b2=new Button("Clear");
l1=new Label("Enter the number:");
l2=new Label("The factorial is:");
l3=new Label(null);
t1=new TextField();

add(l1);

add(t1);

add(l2);

add(l3);

add(b1);
b2.setBounds(270,150,50,20);

g=new GridLayout(4,4,10,20);
p.setLayout(g);
p.add(l1);
p.add(t1);
p.add(l2);
p.add(l3);
p.add(b1);
add(p);
b1.addActionListener(this);
b2.addActionListener(this);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent ae)


{
int n;
if(ae.getSource()==b1)
{
n=Integer.parseInt(t1.getText().trim());
l3.setText(Integer.toString(fact(n)));
}
else
{
t1.setText(null);
l3.setText(null);
t1.requestFocus();
}
}

int fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
{
f=f*i;
}
return f;
}

public static void main(String s[])


{
ex2 ob=new ex2();
}
}

**************************************************************************
5. Calculator using Frame

import java.awt.*;
import java.awt.event.*;

class CalculatorFRAME implements ActionListener


{
//Declaring Objects
Frame f=new Frame();
Label l1=new Label("First Number");
Label l2=new Label("Second Number");
Label l3=new Label("Result");

TextField t1=new TextField();


TextField t2=new TextField();
TextField t3=new TextField();
Button b1=new Button("Add");
Button b2=new Button("Sub");
Button b3=new Button("Mul");
Button b4=new Button("Div");
Button b5=new Button("Cancel");
GridLayout g;
Panel p1,p2;

Calculator()
{

f.setLayout(new FlowLayout());
g=new GridLayout(3,2);
p1=new Panel();
p2=new Panel();
p1.setLayout(g);
p2.setLayout(new FlowLayout());

p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p1.add(l3);

p1.add(t3);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
f.add(p1);
f.add(p2);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);

f.setVisible(true);
f.setSize(400,350);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent e)


{
int n1=Integer.parseInt(t1.getText());
int n2=Integer.parseInt(t2.getText());
if(e.getSource()==b1)
{
t3.setText(String.valueOf(n1+n2));
}

if(e.getSource()==b2)
{
t3.setText(String.valueOf(n1-n2));
}

if(e.getSource()==b3)
{
t3.setText(String.valueOf(n1*n2));
}

if(e.getSource()==b4)
{
t3.setText(String.valueOf(n1/n2));
}

if(e.getSource()==b5)
{
System.exit(0);
}
}

public static void main(String args[])


{
new CalculatorFRAME();
}
}

6. Standard Calculator

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="calculator" width=360 height=350>
</applet>
*/
public class calculator extends Applet implements ActionListener
{
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
TextField t1;
char opr;
double n1=0,n2=0,r=0;
String msg="";

public void init()


{
setFont(new Font("SansSerif", Font.BOLD, 18));

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");
b10=new Button("+");
b11=new Button("-");
b12=new Button("x");
b13=new Button("/");
b14=new Button("=");
b15=new Button("0");
b16=new Button("c");

t1=new TextField();

setLayout(null);

t1.setBounds(10,10,338,60);
b9.setBounds(10,90,80,60);
b8.setBounds(95,90,80,60);
b7.setBounds(180,90,80,60);
b10.setBounds(265,90,80,60);
b4.setBounds(10,155,80,60);
b5.setBounds(95,155,80,60);
b6.setBounds(180,155,80,60);
b11.setBounds(265,155,80,60);
b1.setBounds(10,220,80,60);
b2.setBounds(95,220,80,60);
b3.setBounds(180,220,80,60);
b12.setBounds(265,220,80,60);
b16.setBounds(10,285,80,60);
b15.setBounds(95,285,80,60);
b14.setBounds(180,285,80,60);
b13.setBounds(265,285,80,60);

add(t1);
add(b1);
add(b2);
add(b3);

add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
add(b13);
add(b14);
add(b15);
add(b16);

t1.addActionListener(this);
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);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);

}
public void actionPerformed(ActionEvent obj)
{
String str=obj.getActionCommand();
if(str.equals("1"))
{
t1.setFont(new Font("SansSerif", Font.BOLD, 25));
String s=t1.getText()+b1.getActionCommand();
t1.setText(s);
}
else if(str.equals("2"))
{
t1.setFont(new Font("SansSerif", Font.BOLD, 25));
String s=t1.getText()+b2.getActionCommand();
t1.setText(s);
}
else if(str.equals("3"))
{
t1.setFont(new Font("SansSerif", Font.BOLD, 25));
String s=t1.getText()+b3.getActionCommand();
t1.setText(s);
}
else if(str.equals("4"))
{
t1.setFont(new Font("SansSerif", Font.BOLD, 25));
String s=t1.getText()+b4.getActionCommand();
t1.setText(s);
}
else if(str.equals("5"))
{
t1.setFont(new Font("SansSerif", Font.BOLD, 25));
String s=t1.getText()+b5.getActionCommand();
t1.setText(s);
}
else if(str.equals("6"))
{
t1.setFont(new Font("SansSerif", Font.BOLD, 25));
String s=t1.getText()+b6.getActionCommand();
t1.setText(s);
}
else if(str.equals("7"))
{
t1.setFont(new Font("SansSerif", Font.BOLD, 25));
String s=t1.getText()+b7.getActionCommand();
t1.setText(s);
}
else if(str.equals("8"))
{
t1.setFont(new Font("SansSerif", Font.BOLD, 25));
String s=t1.getText()+b8.getActionCommand();
t1.setText(s);
}
else if(str.equals("9"))
{
t1.setFont(new Font("SansSerif", Font.BOLD, 25));
String s=t1.getText()+b9.getActionCommand();
t1.setText(s);
}
else if(str.equals("0"))
{
t1.setFont(new Font("SansSerif", Font.BOLD, 25));
String s=t1.getText()+b15.getActionCommand();
t1.setText(s);
}
else if(str.equals("+"))
{
String s=t1.getText();
n1=Double.parseDouble(s);
opr='+';
t1.setText("");
}
else if(str.equals("-"))
{

String s=t1.getText();
n1=Double.parseDouble(s);
opr='-';
t1.setText("");
}
else if(str.equals("x"))
{

String s=t1.getText();
n1=Double.parseDouble(s);
opr='*';
t1.setText("");
}
else if(str.equals("/"))
{

String s=t1.getText();
n1=Double.parseDouble(s);
opr='/';
t1.setText("");
}
else if(str.equals("c"))
{
t1.setText("");

}
else if(str.equals("="))
{
t1.setFont(new Font("SansSerif", Font.BOLD, 25));
String s=t1.getText();
n2=Double.parseDouble(s);
if(opr=='+')
{
r=n1+n2;
t1.setText(String.valueOf(r)); //It convert the Int to String
}
if(opr=='-')
{
r=n1-n2;
t1.setText(String.valueOf(r));
}
if(opr=='*')
{
r=n1*n2;
t1.setText(String.valueOf(r));
}
if(opr=='/')
{
r=n1/n2;
t1.setText(String.valueOf(r));
}
}
}
}

Best PDF Encryption Reviews

You might also like