Monday, May 27, 2019

Java program (Action Listener)


Java program to change text enter in textField into italic,bold,and  arial.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="magic3.java" width=100 height=1000>
</applet>
*/
public class magic3 extends Applet implements ActionListener
{
       TextField tf;
       Button arial,bold,italic;
       Label l;
       Font f;
       public void init()
       {
              setLayout(null);
              setBackground(Color.pink);
             
              tf=new TextField();
             
              tf.setBounds(100,100,250,30);
              add(tf);
             
              arial=new Button("Arial");
             
              arial.setBounds(120,150,50,20);
              add(arial);
                          
              bold=new Button("Bold");
      
              bold.setBounds(180,150,50,20);
                     add(bold);
                          
              italic=new Button("Italic");
              add(italic);
              italic.setBounds(240,150,50,20);
      
              l=new Label();
              l.setBounds(100,200,25,30);
              add(l);
             
              arial.addActionListener(this);
               bold.addActionListener(this);
                italic.addActionListener(this);
       }
       public void actionPerformed(ActionEvent ae)
       {
              if(ae.getSource()==arial)
              {
                     f=new Font("Arial Narrow",0,20);
                     l.setText(tf.getText());
                     l.setFont(f);
              }
              else if(ae.getSource()==bold)
              {      f=new Font("Arial Narrow",1,20);
                     l.setText(tf.getText());
                     l.setFont(f);
              }
              else if(ae.getSource()==italic)
              {      f=new Font("Arial Narrow",2,20);
                     l.setText(tf.getText());
                     l.setFont(f);
              }
       }
}
Java program to  perform operation such as Tan,sin,sqrt,in applet

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class sak1 extends Applet implements ActionListener
{
       TextField tf1,tf2;
       Button log,sin,sqrt,tan;
       public void init()
       {
              setLayout(null);
              setBackground(Color.gray);
      
              tf1=new TextField();
              tf1.setBounds(100,100,250,30);
              add(tf1);
      
              log=new Button("Log");
              log.setBounds(120,150,50,20);
              add(log);
             
              sin=new Button("Sin");
              sin.setBounds(180,150,50,20);
              add(sin);
             
              sqrt=new Button("Sqrt");
              sqrt.setBounds(240,150,50,20);
              add(log);

              tan=new Button("tan");
              tan.setBounds(300,150,50,20);
              add(log);

              tf2=new TextField();
              tf2.setBounds(100,200,250,30);
              add(tf2);
             
              log.addActionListener(this);
                     sin.addActionListener(this);
                     sqrt.addActionListener(this);
                     tan.addActionListener(this);
       }
       public void actionListener(ActionEvent ae)
       {
       if(ae.getSource()==log)
       {
              double d1=Double.parseDouble(tf1.getText());
              double ans=Math.log(d1);
              tf2.setText("Log("+d1+")="+ans);
       }
       else if(ae.getSource()==sin)
       {
             
              double d1=Double.parseDouble(tf1.getText());
              double ans=Math.sin(d1);
              tf2.setText("Sine("+d1+")="+ans);
       }
       else if(ae.getSource()==sqrt)
       {
             
              double d1=Double.parseDouble(tf1.getText());
              double ans=Math.sqrt(d1);
              tf2.setText("Sqrt("+d1+")="+ans);
       }
       else if(ae.getSource()==tan)
       {
      
              double d1=Double.parseDouble(tf1.getText());
              double ans=Math.tan(d1);
              tf2.setText("Tan("+d1+")="+ans);
       }
}
}
/*
<applet code="sak1.java" width=100 height=200>
</applet>
*/
     


java program to count number of vowel enter in textfield in applet


import java.applet.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.lang.*;

public class pst1 extends Applet implements ActionListener
{            
       TextField tf;
       TextArea ta;
       Button b1;
       int count=0;
       public void init()
       {
              setLayout(null);
             
              tf=new TextField();
              add(tf);
              tf.setBounds(20,20,120,30);
      
              ta=new TextArea();
              add(ta);
              ta.setBounds(20,60,120,30);

              b1=new Button("calculate");
              add(b1);
              b1.setBounds(20,120,100,40);
              b1.addActionListener(this);
       }
       public void actionPerformed(ActionEvent ae)
       {
              String str=ta.getText();
              int len=str.length();
              for(int i=0;i<len;i++)
              {
                     if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='u')
                     count++;
              }
       tf.setText(""+count);
       }
}

/*
<applet code="pst1.java" width=100 height=100>
</applet>
*/








 


No comments:

Post a Comment

java program (Tree)