Welcome To Uday Satya Blog

91-100 PROGRAMS




91  Program to Make a Login Window



import java.applet.*;

import java.awt.*;
import java.awt.event.*;
public class login extends Applet implements ActionListener{
  TextField un,pass;
   Button b1,b2;
  String msg="";
public void init(){
un=new TextField(12);
pass=new TextField(8);
pass.setEchoChar('*');
b1=new Button("Verify");
b2=new Button("Cancel");
add(un);
add(pass);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
   String s=e.getActionCommand();
if(s.equals("Verify")){
String user=un.getText();
String pwd=pass.getText();
if(user.equals("student") && pwd.equals("loyola")){
   msg="Login Successful";
}
else
  msg="You cannot login.Check your username and password";
}
else{
   un.setText("");
   pass.setText("");
msg="";
}
repaint();
}
public void paint(Graphics g){
g.drawString(msg,40,40);
}}//end of class


92  Program to Make a Login Focus
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class loginFocus extends Applet implements ActionListener,FocusListener {
 TextField un,pass;
 Button b1,b2;
 String msg="";
public void init() {
un=new TextField(12);
pass=new TextField(8);
pass.setEchoChar('*');
b1=new Button("Verify");
b2=new Button("Cancel");
add(un);
add(pass);
add(b1);
add(b2);
un.addFocusListener(this);
pass.addFocusListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void focusGained(FocusEvent fe) {
if(fe.getSource()==un) {
un.setText("Enter UserName");
}}
public void focusLost(FocusEvent fe) {
if(fe.getSource()==pass) {
   if(pass.getText().equals(""))  {
      msg="Enter the password to login";
repaint();
    }}}
public void actionPerformed(ActionEvent e) {
   String s=e.getActionCommand();
if(s.equals("Verify")){
String user=un.getText();
String pwd=pass.getText();
if(user.equals("student") && pwd.equals("loyala")) {
   msg="Login Successful";
}
else
  msg="You cannot login.Check your username and password";
}
else {
   un.setText("");
   pass.setText("");
msg="";
}
repaint();
}
public void paint(Graphics g){
g.drawString(msg,40,40);
}}//end of class


93  Program to Demonstrate Mouse Adapter
import java.awt.event.*;
import java.applet.*;
public class MouseAdapterApplet extends Applet {
public void init() {
 addMouseListener(new MouseAdapter(){
  public void mousePressed(MouseEvent e){
          showStatus("Mouse Pressed");}});}}


94  Program to Demonstrate Mouse Events

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MouseEventsApplet extends Applet implements MouseListener,MouseMotionListener {
 String msg;
public void init() {
msg="";
 addMouseListener(this);
  addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent e){
    msg="mouseClicked";
repaint();
}
public void mouseEntered(MouseEvent e){
    msg="mouseentered";
repaint();
}
public void mouseExited(MouseEvent e){
msg="mouseexit";
repaint();}
public void mousePressed(MouseEvent e){
    msg="mousepressed";
repaint();}
public void mouseReleased(MouseEvent e){
    msg="mousereleased";
repaint();}
public void mouseDragged(MouseEvent e){
    msg="*";
repaint();
}
public void mouseMoved(MouseEvent e){
    msg="mousemoved";
repaint();
}
public void paint(Graphics g){
g.drawString(msg,10,20);
}}


95  Program to Demonstrate the key event handlers

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class SimpleKey extends Applet
implements KeyListener {
String msg = "";
int X = 10, Y = 20; // output coordinates
public void init() {
addKeyListener(this);
requestFocus(); // request input focus
}
public void keyPressed(KeyEvent ke) {
showStatus("Key Down");
}
public void keyReleased(KeyEvent ke) {
showStatus("Key Up");
}
public void keyTyped(KeyEvent ke) {
msg += ke.getKeyChar();
repaint();
}
// Display keystrokes.
public void paint(Graphics g) {
g.drawString(msg, X, Y);
}
}


96  Program to Demonstrate Border Layout

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class TestBL extends Applet {
public void init() {
  setLayout(new BorderLayout());
add(new Scrollbar(Scrollbar.HORIZONTAL),BorderLayout.NORTH);
add(new Scrollbar(Scrollbar.HORIZONTAL),BorderLayout.SOUTH);
add(new Scrollbar(Scrollbar.VERTICAL),BorderLayout.EAST);
add(new Scrollbar(Scrollbar.VERTICAL),BorderLayout.WEST);
add (new TextArea("I am in the center"),BorderLayout.CENTER);
}}


97  Program to Demonstrate Card Layout

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class TestCL extends Applet implements ActionListener {
Panel p,p1,p2;
CardLayout cl1;
Label l1;
Button b1,b2;
CheckboxGroup cbg;
Checkbox c1,c2;
public void init()  {
  l1=new Label("Licesence Agreement");
  b1=new Button("Accept");
  b2=new Button("Exit");
  cl1=new CardLayout();
  p=new Panel();
   p.setLayout(cl1);
p1=new Panel();
p1.add(l1);
   p1. add(b1);
p1.add(b2);   
  p2=new Panel();
  cbg=new CheckboxGroup();
c1=new Checkbox("Typical",cbg,true);
  c2=new Checkbox("Custom",cbg,false);
p2.add(c1);
  p2.add(c2);
p.add(p1,"panel1");
p.add(p2,"panel2");
  add(p);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b1) {
  cl1.show(p,"panel2");
}}


98  Program to Test Fonts Using Mouse Events

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class TestFonts extends Applet{
  int n=0;
  Font f;
String msg;
public void init(){
f=new Font("Dialog",Font.PLAIN,12);
msg="Hello World";
setFont(f);
addMouseListener(new MyMouseAdapter(this));
}
public void paint(Graphics g){
g.drawString(msg,10,40);
}}
class MyMouseAdapter extends MouseAdapter{
TestFonts tfonts;
public MyMouseAdapter(TestFonts f){
tfonts=f;
}
public void mousePressed(MouseEvent e){
tfonts.n++;
switch(tfonts.n){
case 1:
tfonts.f=new Font("DialogInput",Font.BOLD,20);
break;
case 2:
tfonts.f=new Font("SansSerif",Font.BOLD,22);
break;
case 3:
tfonts.f=new Font("Serif",Font.BOLD,24);
break;
case 4:
tfonts.f=new Font("Monospaced",Font.BOLD,26);
break;
}
if(tfonts.n==4){
tfonts.n=-1;
tfonts.setFont(tfonts.f);
tfonts.repaint();
}} }


99  Program to Demonstrate Grid Layouts


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class TestGL extends Applet{
public void init(){
  setLayout(new FlowLayout(FlowLayout.CENTER));
add(new TextField("0"));
setLayout(new GridLayout(3,3));
setFont(new Font("SansSerif",Font.BOLD,16));
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int k=i*3+j;
if(k>0)
  add(new Button(""+k));
}}}}


100  Program to Demonstrate Scrollbar

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class TestScrollBar extends Applet implements AdjustmentListener
{
  Scrollbar s1,s2,s3;
  public void init()
  {
     s1=new Scrollbar(Scrollbar.HORIZONTAL,100,10,0,255);
     s2=new Scrollbar(Scrollbar.VERTICAL,50,10,0,255);
  s3=new Scrollbar(Scrollbar.VERTICAL,150,10,0,255);
    setLayout(new BorderLayout());
add(s1,BorderLayout.SOUTH);
 add(s2,BorderLayout.EAST);
 add(s3,BorderLayout.WEST);
    //register to recieve adjustment events
 s1.addAdjustmentListener(this);
  s2.addAdjustmentListener(this); 
 s3.addAdjustmentListener(this);
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
  repaint();
}
public void paint(Graphics g)
{
   int r=s1.getValue();
   int g1=s2.getValue();
   int b=s3.getValue();
  Color c=new Color(r,g1,b);
 setBackground(c);
    }
}

0 comments:

Post a Comment