TextField Program: 9(sample program to Math calculation in button)
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="tf9.java" width=500 height=300></applet>*/
public class b8 extends Applet implements ActionListener
{
Button b1,b2,b3,b4,b5,b6,b7;
Label l1,l2;
TextField t1,t2;
double x, y;
public void init()
{
b1=new Button("sin");
b2=new Button("cos");
b3=new Button("log");
b4=new Button("ln");
b5=new Button("sq");
b6=new Button("sqrt");
b7=new Button("clear");
l1=new Label("x");
l2=new Label("y");
t1=new TextField(8);
t2=new TextField(8);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
t1.addActionListener(this);
t2.addActionListener(this);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
}
public void actionPerformed(ActionEvent e)
{
String s1=e.getActionCommand();
Graphics g=getGraphics();
if(s1.equals("sin"))
{
x=Double.parseDouble(t1.getText());
y=Math.sin(x);
t2.setText(String.valueOf(y));
}
else if(s1.equals("cos"))
{
x=Double.parseDouble(t1.getText());
y=Math.cos(x);
t2.setText(String.valueOf(y));
}
else if(s1.equals("log"))
{
x=Double.parseDouble(t1.getText());
y=Math.log(x);
t2.setText(String.valueOf(y));
}
else if(s1.equals("ln"))
{
x=Double.parseDouble(t1.getText());
y=Math.log(x);
t2.setText(String.valueOf(y));
}
else if(s1.equals("sq"))
{
x=Double.parseDouble(t1.getText());
y=Math.pow(x,2);
t2.setText(String.valueOf(y));
}
else if(s1.equals("sqrt"))
{
x=Double.parseDouble(t1.getText());
y=Math.sqrt(x);
t2.setText(String.valueOf(y));
}
if(s1.equals("clear"))
{
t1.setText(" ");
t2.setText(" ");
}
}
}
Compile:
F:\jdk-1.0.6\bin>javac b8.java
Run:
F:\jdk-1.0.6\bin>appletviewer b8.java
Output:
Notes:-
TextField Program: 11(program for create a employee details)
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/* <applet code="tf11.java" width=500 height=500>
</applet>*/
public class tf11 extends Applet implements ActionListener
{
Button b1,b2;
Label l1,l2,l3,l4,l5,l6,l7,l8,l9;
TextField t1,t2,t3,t4,t5,t6,t7,t8,t9;
public void init()
{
b1=new Button("CALCULATE");
b2=new Button("CLEAR");
l1=new Label("Employee name");
l2=new Label("Employee no");
l3=new Label("Salary");
l4=new Label("HRA");
l5=new Label("DA");
l6=new Label("TA");
l7=new Label("IT");
l8=new Label("Gross pay");
l9=new Label("net pay");
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
t4=new TextField(10);
t5=new TextField(10);
t6=new TextField(10);
t7=new TextField(10);
t8=new TextField(10);
t9=new TextField(10);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(l4);
add(t4);
add(l5);
add(t5);
add(l6);
add(t6);
add(l7);
add(t7);
add(l8);
add(t8);
add(l9);
add(t9);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
double sal, hra, da, it, ta, gross, net;
t1.getText();
t2.getText();
sal=Double.parseDouble(t3.getText());
if(s.equals("CALCULATE"))
{
hra=sal*0.2;
t4.setText(String.valueOf(hra));
da=sal*0.4;
t5.setText(String.valueOf(da));
ta=sal*0.6;
t6.setText(String.valueOf(ta));
it=sal*0.8;
t7.setText(String.valueOf(it));
gross=hra+da+ta;
t8.setText(String.valueOf(gross));
net=gross-it;
t9.setText(String.valueOf(net));
}
else if(equals("CLEAR"))
{
t1.setText(" ");
t2.setText(" ");
t3.setText(" ");
t4.setText(" ");
t5.setText(" ");
t6.setText(" ");
t7.setText(" ");
t8.setText(" ");
t9.setText(" ");
}
}
}
Compile:
F:\jdk-1.0.6\bin>javac tf11.java
Run:
F:\jdk-1.0.6\bin>appletviewer tf11.java
Output:-
Notes:-
TextField Program: 12(sample program for student details)
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="tf12.java" width=500 height=500> </applet>*/
public class tf12 extends Applet implements ActionListener
{
Button b1,b2;
Label l1,l2,l3,l4,l5,l6,l7,l8,l9;
TextField t1,t2,t3,t4,t5,t6,t7,t8,t9;
public void init()
{
b1=new Button("CALCULATE");
b2=new Button("CLEAR");
l1=new Label("Student name");
l2=new Label("Student no");
l3=new Label("Standard");
l4=new Label("mark1");
l5=new Label("mark2");
l6=new Label("mark3");
l7=new Label("total");
l8=new Label("avg");
l9=new Label("result");
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
t4=new TextField(10);
t5=new TextField(10);
t6=new TextField(10);
t7=new TextField(10);
t8=new TextField(10);
t9=new TextField(10);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(l4);
add(t4);
add(l5);
add(t5);
add(l6);
add(t6);
add(l7);
add(t7);
add(l8);
add(t8);
add(l9);
add(t9);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("CALCULATE"))
{
int sno,std,m1,m2,m3,tot;
double avg;
String sname, fname, res;
sname=t1.getText();
sno=Integer.parseInt(t2.getText());
std=Integer.parseInt(t3.getText());
m1=Integer.parseInt(t4.getText());
m2=Integer.parseInt(t5.getText());
m3=Integer.parseInt(t6.getText());
tot=(m1+m2+m3);
t7.setText(String.valueOf(tot));
avg=(tot/3);
t8.setText(String.valueOf(avg));
if(m1>=50 && m2>=50 && m3>=50)
{
t9.setText("pass");
}
else{
t9.setText("Fail");
}
}
else if(s.equals("CLEAR"))
{
t1.setText(" ");
t2.setText(" ");
t3.setText(" ");
t4.setText(" ");
t5.setText(" ");
t6.setText(" ");
t7.setText(" ");
t8.setText(" ");
t9.setText(" ");
}
}
}
Compile:
F:\jdk-1.0.6\bin>javac tf12.java
Run:
F:\jdk-1.0.6\bin>appletviewer tf12.java
Output:-
Notes:-