ActionListeners example in java
import java.awt.*;
import java.awt.event.*;
class click extends Frame implements ActionListener
{
Button b,p;
click()
{
this.setLayout(null);
b= new Button("pink");
p= new Button("blue");
b.setBounds(100,299,70,20);
p.setBounds(200,299,70,20);
this.add(p);
this.add(b);
b.addActionListener(this);
p.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==p)
setBackground(Color.pink);
if(ae.getSource()==b)
setBackground(Color.blue);
}
public static void main(String a[])
{
click c=new click();
c.setTitle("adding button");
c.setSize(500,500);
c.setVisible(true);
}
}
Decimal to Binary calculation in java
//import java.io.*;
class dectobin
{
public static void main(String arg[])
{
int b;
//int a=Integer.parseInt(10);
int a=10;
while(a<=0)
{
b=a%2;
a=a/2;
System.out.print(b);
}
System.out.println(" Binary to decimal convertor in java source code");
}
}
Creating A frame in java
import java.awt.*;
class demoframe extends Frame
{
public static void main(String arg[])
{
demoframe f=new demoframe();
f.setSize(300,300);
f.setVisible(true);
f.setTitle("Dallas cowboy xkeycode");
}
}
Ceaser Encryption in java
import java.awt.*;
import java.awt.event.*;
//import java.awt.text.*;
import java.lang.*;
public class encryp extends Frame implements ActionListener
{
Label l1,l2,l3;
TextField t;
TextField t2,k;
String s1;
Button b,e;
encryp()
{
this.setLayout(null);
l1=new Label("Input :");
l1.setBounds(350,50,50,20);
this.add(l1);
//l1.addActionListener(this);
l2=new Label("key :");
l2.setBounds(350,100,50,20);
this.add(l2);
l3=new Label("Output :");
l3.setBounds(350,350,50,20);
this.add(l3);
t=new TextField(4);
t.setBounds(400,50,100,20);
this.add(t);
t.addActionListener(this);
k=new TextField(4);
k.setBounds(400,100,100,20);
this.add(k);
k.addActionListener(this);
t2=new TextField(4);
t2.setBounds(400,350,100,20);
this.add(t2);
t2.addActionListener(this);
b=new Button("Ceaser");
b.setBounds(300,140,50,30);
this.add(b);
b.addActionListener(this);
e=new Button("exit");
e.setBounds(410,300,50,30);
this.add(e);
e.addActionListener(this);
}
public void work()
{
System.out.println(" work ");
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b)
{
s1=t.getText();
StringBuffer s=new StringBuffer(s1);
System.out.println(t.getText());
//s.reverse();
System.out.println(s);
t2.setText(s.reverse());
}
if(ae.getSource()==e)
{
System.exit(0);
}
}
public void textChanged(TextEvent te)
{
System.out.println(" java source code ");
}
public static void main(String arg[])
{
encryp e=new encryp();
e.setTitle(" encryption ");
e.setSize(900,600);
e.setVisible(true);
}
Simple applications, copy paste and run it...
http://belazy.blog.com/
No comments:
Post a Comment
Your feedback may help others !!!