Wednesday, July 17, 2013

Use of String class function and Frame program



In this program we will use cardLayout and user will enter number of names which will be sorted and the sorted names will be displayed using a TextArea.

import java.awt.*;
import java.awt.event.*;
class A1 extends Frame implements ActionListener
{
Panel mp,cp1,cp2;
CardLayout cl;
int i,x,y;
TextField tf1,tf2;
String s1[]=new String[10];
String s2[]=new String[10];
String temp,s3;
Button b1,b2,b3,b4,b5,b6;
TextArea ta1,ta2;
A1()
{
i=0;
s3="";
cl=new CardLayout();
mp=new Panel();

tf1=new TextField(20);
tf2=new TextField(20);
ta1=new TextArea(10,20);
ta2=new TextArea(10,20);
b1=new Button("Submit");
b2=new Button("Show");
b3=new Button("Next");
b4=new Button("Submit");
b5=new Button("Show");
b6=new Button("Exit");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
mp.setLayout(cl);
cp1=new Panel();
cp2=new Panel();
cp1.add(tf1);
cp1.add(b1);
cp1.add(b2);
cp1.add(b3);
cp1.add(ta1);
cp2.add(tf2);
cp2.add(b4);
cp2.add(b5);
cp2.add(b6);
cp2.add(ta2);
mp.add(cp1,"pan1");
mp.add(cp2,"pan2");
add(mp);
setSize(400,400);
show();
}
public void actionPerformed(ActionEvent ae)
{
 Button b1=(Button)ae.getSource();
if(b==b1)
{
s1[i++]=tf1.getText();
tf1.setText("");
}

else if(b==b2)
{
for(x=0;x
{
 for(y=x+1;y
{
if(s1[x].compareTo(s1[y])>0)
{
 temp=s1[x];
s1[x]=s1[y];
s1[y]=temp;
}
}
}

for(x=0;x
{
 s3=s3+"\n"+s1[x];
}
ta1.setText(s3);
}
else if(b==b3)
{
i=0;s3="";
cl.next(mp);
}
else if(b==b4)
{
s2[i++]=tf2.getText();
tf2.setText("");
}
else if(b==b5)
for(x=0;x
{
 for(y=x+1;y
{
if(s1[x].compareToIgnoreCase(s1[y])>0)
{
 temp=s1[x];
s1[x]=s1[y];
s1[y]=temp;
}
}
}

for(x=0;x
{
 s3=s3+"\n"+s1[x];
}
ta2.setText(s3);
}
else if(b==b6)
{
System.exit(0);
}
}
}
class A
{
 public static void main(String args[])
{
 A1 ob=new A1();
}
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner