You are on page 1of 80

Web Technology Lab Record

CONTENTS
SNo 1 2 3 4 5 6 7 8 TOPIC
Creation of Static Webpages using HTML Creation of Web Pages using CSS Programs using JavaScript Programs using VBScript Creation of Dynamic Webpages using DHTML Chat applications using Socket Programming in Java Event Driven Programs using Java Applets Programs using AWT Controls and Layouts in Java

Date
27/07/12 03/08/12 09/08/12 16/08/12 23/08/12 20/09/12 04/10/12 11/10/12

Creation of Static Webpages using HTML Ex. No: 1 Date : 27/07/12

PROBLEM STATEMENT To Design Static WebPages using HTML. CODE:


<html> <head> <title>Internet and WWW How to program-Tables</title> </head> <body> <h1>Table Example Page</h1> <p> <center> Here is a more compicated example</center> <table border=1> <tr> <td rowspan=2><img src="camel" alt='Camel photo' height =150 width =200> </td> <td colspan=4 align=center> <br/> <font size = 5> <b>Camelid comparison</b><br/><br/> </font> <font size = 2> <p>approximate as of 9/2002 </p></font> <br/><br/> </td> <tr>

<th># of humps</th> <th>Indigenous region</th> <th>splits</th> <th>produces wool</th> </tr> <tr> <th>Camels <bactrian></th> <td>2</td> <td>Afica/Asia</td> <td rowspan =2>Llama</td> <td rowspan =2>Llama</td> </tr> <tr> <th>Llamas</th> <td>1</td> <td>Andes Mountains</td> </tr>

</table> </body> </html>

SAMPLE OUTPUT:

CODE:

<html> <body align=justify> <center>

<h1>PROFILE</h1> <ul> <li><a href=acad.html>ACADEMIC PROFILE</li> <li><a href=miniproj.html>MINI PROJECTS</li> <li><a href=personal.html>PERSONAL pROFILE</li> </ul> </center> <body> </html>

SAMPLE OUTPUT:

CODE:
<html>

<frameset cols="25%,*"> <frame src = "intro.html" name ="frame1"/> <frame name="frame2"/> </frameset> </html> intro.html <html> <body> <ul> <li><a href="rose.html" target="frame2">RoSE</a></li> <li><a href="jasmine.html" target="frame2">JasminE</a></li> <li><a href="lotus.html" target="frame2">LoTus</a></li> </ul>

</body> </html> [flower].html <rose,jasmine,lotus> <html> <body> <h1>[flower]</h1> </body> </html>

SAMPLE OUTPUT:

CODE:

<html> <body> <img src="camel" usemap="#mymap" /> <map name="mymap" id="mymap"> <area shape="circle" coords="10,10,50" href="q1.html" /> <area shape ="circle" coords="40,40,100" href="1b.html"/> </map> </body> </html>

SAMPLE OUTPUT:

RESULT: Static webpages were created successfully using HTML.

Creation of Web Pages using CSS Ex. No: 2 Date : 03/08/12

PROBLEM STATEMENT Webpage Creation using CSS CODE:

<html> <head> <style type="text/css"> ul{margin-left:75} .nested{margin-left:15} em{color:red;} a{color:blue;text-decoration:none} a:hover{text-decoration:underline;color:red;background-color:aqua;} </style> </head> <body> <ul> <li>Milk</li> <li>Bread</li> <ul class="nested">

<li>White bread</li> <li>Rye Bread</li> <li>Whole wheat bread</li> </ul> <li>Rice</li> <li>Potatoes</li> <li>Pizza <em>with mushroom</em></li>

</ul> <br/><br/> <center><a href=" ">Go to the grocery store</a></center> </body> </html>

SAMPLE OUTPUT:

CODE:

<html> <head>

<link type="text/css" rel="stylesheet" href="c22.css"/>

</head> <body> <ul> <li>Milk</li> <li>Bread</li> <ul class="nested">

<li>White bread</li> <li>Rye Bread</li> <li>Whole wheat bread</li> </ul> <li>Rice</li> <li>Potatoes</li> <li>Pizza <em>with mushroom</em></li> </ul> <br/><br/> <center><a href=" ">Go to the grocery store</a></center> </body> </html>

c22.css

ul{margin-left:75} .nested{margin-left:15} em{color:red;} a{color:blue;text-decoration:none} a:hover{text-decoration:underline;color:red;background-color:aqua;}

SAMPLE OUTPUT:

CODE:
<html> <body> <p><center> Thanks for visiting my web site. I hope you enjoy a lot. Please Note: This site will be moving soon. Check periodically for updates. </p></center>

</body> </html>

SAMPLE OUTPUT:

CODE:
<html>

<body> <input type="radio" name="color" value="Red" CHECKED >RED</input><br/> <input type="radio" name="color" value="Blue" >BLUE</input><br/> <input type="radio" name="color" value="yellow" >Yellow</input><br/> <input type="radio" name="color" value="Green" >Green</input><br/> <input type="submit"value="SUBMIT" action="reset"></input><br/><br/> </body> <table> <tr> <td style="background-color:red;color=black;">RED---25%</td> </tr> <tr> <td style="background-color:blue;color=black;">BLUE--15%</td> </tr> <tr> <td style="background-color:yellow;color=black;">YELLOW--20%</td> </tr> <tr>

<td style="background-color:green;color=black;">GREEN--40%</td> </tr>

</html>

SAMPLE OUTPUT:

CODE:
<html> <head> <style type="text/css"> h1{padding:0.5em;border:groove;margin:0.5em;} .greenMove{color:green;margin-top:25;margin-left:15} </style> </head> <body> <h1> This is of type h1</h1> <h1 class="greenMove"> this para follows a style of class greenMove</h1> </body> </html>

SAMPLE OUTPUT:

Result: Web pages were created using CSS

Programs using JavaScript Ex. No: 3 Date : 09/08/12

PROBLEM STATEMENT Programming using JavaScript CODE:


<html> <head>

</head> <body>

<input type="text" id="num" /> <input type="submit" value="calc" id="cal" onclick="func()"/> <script type="text/javascript"> function func() { var x=document.getElementById("num").value;

towers('1','3','2',document.getElementById("num").value);

} function towers(i,f,m,num) { num=+num; if(num===1) { document.write("move disk "+ num +" from "+ i +" to "+ f +"<br/>");}

else { towers(i,m,f,num-1); document.write("move disk "+ num +" from "+ i +" to "+ f +"<br/>"); towers(m,f,i,num-1); } } </script> </body> </html>

Sample Output:

CODE:
<html> <head> <title> perfect number </title> <script type="text/javascript"> function perfect(num) { /*num=+num;*/ var temp=0; var i=1; while(i<=(num/2)) { if(num%i==0)

temp=temp+i; i++; } if(temp===num) return 1; else return 0; /*document.write(num);*/ } </script>

</head> <body> <script type="text/javascript">

var i=2; while(i<500) { if(perfect(i)) document.write(i+"<br/>"); i++; } </script> </body> </html>

Sample Output:

CODE:
<html> <head> <script type="text/javascript"> function getInput() { var x = document.getElementById("aid").value.toString(); var xlength = x.length;

var z = new Array();

for(i=0;i<xlength;i++) { z[i] = x[xlength-1-i]; }

var count = 0; for(i=0;i<xlength;i++) { if(x[i] == z[i]) { count++; } }

if(count == xlength) { document.write("Yes it a palindrome!!"); }

else { document.write("No!"); } } </script> </head> <body> <input type="text" id="aid" /> <input type="submit" value="accept" id="acc" onclick="getInput()" /> </body> </html>

Sample output:

CODE:

<html> <head> <script type="text/javascript"> function sort(arr) { var i=0,j; var n=arr.length; while(i<n) { j=0; while(j<(n-i+1)) { if(arr[j]>arr[j+1]) { var temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } j++; }i++; }

document.write(arr.toString()); } </script> </head> <body> <script type="text/javascript"> var arr=[4,6,1,8,3];

sort(arr); </script> </body> </html>

Sample Output:

Programs using VBScript Ex. No: 4 Date : 16/08/12

PROBLEM STATEMENT Programming using VBScript CODE:


<html> <head> <title> Compound interest </title> <script type= "text/javascript"> var p=0,n=0,r=0; function compound() { p=parseFloat(principle.value); r=parseFloat(rate.value); n=parseInt(years.value); if(!p || !r || !n) { alert("ERROR");

} else { var ci=0;

var t=0; ci=p*Math.pow((100+r)/100,n);

ci=ci;

document.write("Total Return is "+ci+"<br/>"); ci=ci-p; document.write("Compound Interest is "+ci); }

} </script> </head>

<body> <p>Principle :-

<input type="text" name="princliple" id="principle"/> <br/>

Duration in Years:<input type="text" name= "years" id="years" /> <br/>

Rate

:-

<input type="text" name="rate" id="rate"/> <br/> <input type="submit" onclick="compound()" value="calculate" /> </body> </html>

Sample Output:

CODE:
<html> <head> <title> Upper Lower Case </title> <script type="text/javascript"> function upperlower() { var string=str.value; document.write("first "+string.toLowerCase()+"<br/>"); document.write("second "+string.toUpperCase()+<br/>);

</script> <body>

<form id="upperlower" method="post"> <label>String</label> <input type="text" id="str" name="str" /> <input type="submit" value="accept" onclick="upperlower()" /> </form>

</body> </html>

Sample Output:

Code:
<html>

<head> <script type="text/javascript"> function tokenize() { var p=str.value;

var a=p.split(" ").reverse();

document.getElementById("result").innerHTML=a.join(" "); }

</script>

</head> <body> <label>Enter:</label> <input type="text" id="str" name="str" /> <input type="button" value="accept" id="tok" onclick="tokenize()" />

<div id="result" /> </body> </html>

Sample output:

Code:
<html> <head> <script type="text/javascript"> function linearsearch() {

var p=str.value; var key=parseInt(red.value); if(!p || !key) { alert("ERROR"); } var a=p.split(" "); var i=0; while(i< a.length) { if(a[i]==key)

break; i++; } if(i< a.length) { document.getElementById("result").innerHTML="found at position "+i+"<br/>" } else { document.getElementById("result").innerHTML="NOT found "+"<br/>" } }

</script>

<body> <label>Enter the numbers: </label> <input type="text" id="str" name="str"/> <br/> <label>key: </label> <input type="text" id="red" name="red"/> <br/> <input type="button" id="s" value="search" onclick="linearsearch()"/> <br/> <div id="result" /> </body> </html>

Sample Output:

Code:
<html> <head> <title> Your Best Website </title> </head> <body>

Select your favorite website: <select id='menu'> <option id='wikipedia'>Wikipedia</option> <option id='facebook'>Facebook</option> <option id='google'>Google</option> <option id='yahoo'>Yahoo!</option> <option id='reddit'>redddit</option> <option id='tumblr'>tumblr</option> </select>

<div id="result"></div>

<script> document.getElementById('menu').onchange = function(e) { var chosenItem = this.options[this.selectedIndex];

document.getElementById('result').innerHTML = 'You chose ' + chosenItem.value; e.preventDefault(); return false; } </script> </body> </html>

Sample Output:

Creation of Dynamic Webpages using DHTML Ex. No: 5 Date : 23/08/12

PROBLEM STATEMENT Webpage Creation using DHTML Code:


<html> <head> <script language="JavaScript"> function moveDown(objectID) { var thisObj = document.getElementById(objectID); thisObj.style.top = parseInt(thisObj.style.top) - 20; if (parseInt(thisObj.style.top) < 1000) { window.setTimeout("moveUp('" + objectID + "')",150); } } function moveUp(objectID) { var thisObj = document.getElementById(objectID); thisObj.style.top = parseInt(thisObj.style.top) + 20;

if (parseInt(thisObj.style.top) < 1000) { window.setTimeout("moveDown('" + objectID + "')",150); } }

</script> </head> <body onLoad="moveDown('myObject');"> <div id="myObject" style="position: absolute; left: 50px; top: 50px; height: 50px; width: 50px; background-color: #cccccc;"></div>

</body> </html>

Sample Output:

Code:
<html> <head> <title>Calculator</title>

</head> <body> <input type="text" id="scr" size=20 /></br> <input type="button" value="1" id="num1" onclick="func(this.value)" /> <input type="button" value="2" id="num2" onclick="func(this.value)" /> <input type="button" value="3" id="num3" onclick="func(this.value)" /> <input type="button" value="%" id="op%" onclick="ops(this.value)" /> </br> <input type="button" value="4" id="num4" onclick="func(this.value)" /> <input type="button" value="5" id="num5" onclick="func(this.value)" /> <input type="button" value="6" id="num6" onclick="func(this.value)" /> <input type="button" value="*" id="op*" onclick="ops(this.value)" /> </br> <input type="button" value="7" id="num7" onclick="func(this.value)" /> <input type="button" value="8" id="num8" onclick="func(this.value)" /> <input type="button" value="9" id="num9" onclick="func(this.value)" /> <input type="button" value="/" id="op/" onclick="ops(this.value)" /> </br> <input type="button" value="0" id="num0" onclick="func(this.value)" /> <input type="button" value="+" id="op+" onclick="ops(this.value)" /> <input type="button" value="-" id="op-" onclick="ops(this.value)" /> <input type="button" value="=" id="op=" onclick="equalto()" />

</br> <input type="button" value="sin" id="sin" onclick="trig(this.value)" /> <input type="button" value="cos" id="cos" onclick="trig(this.value)" /> <input type="button" value="tan" id="tan" onclick="trig(this.value)" /> </br> <input type="button" value="CE"id="num0" onclick="ce()" />

<script type="text/javascript"> var a,b; var op; var clearflag=0; var res; function ce() { document.getElementById("scr").value=""; } function trig(value) { a=parseInt(document.getElementById("scr").value); var str=value;

if(str=="sin") document.getElementById("scr").value=Math.sin(a); else if(str=="sin") document.getElementById("scr").value=Math.cos(a); else if(str=="sin") document.getElementById("scr").value=Math.tan(a);

} function func(value) { if(clearflag==1) { document.getElementById("scr").value=""; clearflag=0; } document.getElementById("scr").value+=value; } function ops(value) { a=parseInt(document.getElementById("scr").value); clearflag=1;

switch(value) { case '+' : op= '+'; break; case '-' : op= '-'; break;

case '/' : op= '/';

break;

case '*' : op= '*'; break;

case '%' : op= '%'; break; } } function equalto() { b=parseInt(document.getElementById("scr").value); var num; switch(op) { case '+' : num = a + b; break; case '-' : num= a - b; break;

case '/' : num= a/b; break;

case '*' : num= a*b; break;

case '%' : num= a%b; break;

} document.getElementById("scr").value=num; } </script> </body> </html>

Sample Output:

Code:
<html> <head><title></title> <script type="text/javascript"> function hello() { var name1=document.getElementById("name1").value; if(name1=="" ) { alert("enter valid name"); } var flag=1; for(i=0;i<name1.length;i++) { if((name1[i]>='A' && name1[i]<='Z') || (name1[i]>='a' && name1[i]<='z')); else { flag=0; break; } } if(flag==0) { alert("Invalid name"); }

if(document.getElementById("age").value!="" && document.getElementById("age").value>1 && document.getElementById("age").value<100); else

{ alert("enter correct age"); } var phno=document.getElementById("phno").value; var count=0; flag=0; for(i=0;i<phno.length;i++) { if(phno[i]>='0' && phno[i]<='9') { count++; } else if(phno[i]==' ' && flag==0) { if(count==3 || count==4) { count=0; flag=1; } else { alert("Invalid std code is 3 to 4"); break; } } else { alert("invalid phone number"); break;

} } if(count!=6 && count!=8) alert("phone number only between 6 to 8"); var email=document.getElementById("email").value; flag=0; i=0;

while(email[i]!='@' && i!=email.length-1) { if((email[i]>='A' && email[i]<='Z') || (email[i]>='a' && email[i]<='z')); else {

alert("invalid @ "); return; }

i++; } if(i==email.length-1) { alert("invalid email id"); return; } i++; while(email[i]!='.' && i!=email.length-1 ) {

if((email[i]>='A' && email[i]<='Z') || (email[i]>='a' && email[i]<='z')); else { alert("invalid email id . "); return; } i++; } if(i==email.length-1) { alert("invalid email id"); return; } i++; while(i!=email.length-1) { if((email[i]>='A' && email[i]<='Z') || (email[i]>='a' && email[i]<='z')); else {

alert("invalid email id after . "); return; }

} } </script> </head> <body>

<form> Name: <input type=text id="name1" /><br /> Age: <input type=text id="age" /><br /> Address: <input type=text id="address" /><br /> Sex: <input type=text id="sex" /><br /> Hobbies: <input type=text id="hobbies" /><br /> Phone Number: <input type=text id="phno" /><br /> E-mail ID: <input type=text id="email" /><br /> <input type="submit" value="Submit" onclick="hello()" />

</form> </body> </html>

Sample Output:

Code:

<html> <head> <script language="JavaScript"> function change(objectID){ var thisObj = document.getElementById(objectID); thisObj.rows[0].cells[0].innerHTML="Thanks" } </script> </head> <body> <table id="table1"> <tr> <td>Welcome</td> </tr> <tr> <td><input type="button" name="OK" value="OK" onClick="change('table1');"></td> </tr> </table> </body> </html>

Sample Output:

Result: Webpages were successfully created using DHTML

Chat Applications using Socket Programming in Java Ex. No: 6 Date : 20/09/12

PROBLEM STATEMENT Create a Chat application using socket programming in Java CODE: 1. Chat application using TCP Server.java import java.io.*; import java.net.*; public class Server{ static static static SocketclientSocket = null; ServerSocketserverSocket = null; clientThread t[] = new clientThread[10];

public static void main(String args[]) { intport_number=2222; System.out.println("Usage: java MultiThreadChatServer \n"+"Now using port number="+port_nu mber); try{ serverSocket = new ServerSocket(port_number); catch (IOException e){ System.out.prin tln(e); } while(true){ try { clientSocket = serverSocket.accept(); for(int i=0; i<=9; i++){ if(t[i]==null) { (t[i] = new clientThread(clientSocket,t)).start(); break; } } }

catch (IOException e) {

System.out.println(e);} } } classclientThread extends Thread { DataInputStream is = null; PrintStreamos = null; Socket clientSocket = null; clientThread t[]; publicclientThread(Socket clientSocket, clientThread[] t){ this.clientSocket=clientSocket; this.t=t; } public void run() { String line; String name; try{ is = new DataInputStream(clientSocket.getInputSt ream()); os = new PrintStream(clientSocket.getOutputStrea m()); os.println("Enter your name."); name = is.readLine(); while (true) { line = is.readLine(); if(line.startsWit h("1")) break; for(int i=0; i<=9; i++) if (t[i]!=null) t[i].os.println(name+": "+line); } for(int i=0; i<=9; i++) if (t[i]!=null && t[i]!=this) t[i].os.println(" The user "+name+" is leaving the chat room !!! " ); os.println(" Bye "+name); for(int i=0; i<=9; i++) if (t[i]==this) t[i]=null; is.close(); os.close(); clientSocket.c lose(); } catch(IOException e){};}} }

Client.java import java.io.*; import java.net.*; public class Client implements Runnable{ static Socket clientSocket = null; staticPrintStreamos = null; staticDataInputStream is = null; staticBufferedReaderinput Line = null; staticboolean closed = false; public static void main(String[] args) { intport_number=2222;// The default port String host="localhost"; if (args.length< 2){ System.out.println("Usage: java MultiThreadChatClient \n"+ "Now using host="+host+",port_number="+port_number); } else host=args[0]; { port_number=Integer.valueOf(args[1]).intValue(); } try { clientSocket = new Socket(host, port_number); inputLine = new BufferedReader(new InputStreamReader(System.in)); os = new PrintStream(clientSocket.getOutputStream()); is = new DataInputStream(clientSocket.getInputStream());

catch (UnknownHostException e) { System.err.println("Don't know about host "+host); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to the host "+host); } try { catch (IOException e) { System.err.println("IOExcep tion: } SAMPLE OUTPUT: Start the Server.java program C:\Program

" + e);

Files\Java\jdk1.5.0_06\bin>javac Server.java C:\Program Files\Java\jdk1.5.0_06\bin>java Server Usage: java MultiThreadChatServer Now using port number=2222 Start the Client.java program for client1 C:\Program Files\Java\jdk1.5.0_06\bin>javac MultiThreadChatClient.java -Xlint C:\Program Files\Java\jdk1.5.0_06\bin>java MultiThreadChatClient Usage: java MultiThreadChatClient Now using host=localhost, port_number=2222 Enter your name. abc Welcome abc xyz: hi hi abc: hi Start the Client.java program for client2 C:\Program Files\Java\jdk1.5.0_06\bin>javac MultiThreadChatClient.java -Xlint C:\Program Files\Java\jdk1.5.0_06\bin>java MultiThreadChatClient Usage: java MultiThreadChatClient Now using host=localhost, port_number=2222 Enter your name. xyz Welcome xyz hi

Chat application using UDP:


import java.net.*; import java.io.*; public class chat { public static void main(String args[]) throws Exception { DatagramSocketioSocket; String toName = "localhost"; String userIn; bytesendBuffer[]= new byte[128]; DatagramPacketsendPacket= new DatagramPacket(sendBuffer,128); BufferedReader in = new BufferedReader(newInputStreamReader(System.in)); intioPort = 111, receiverPort = ioPort; if (args.length> 0) toName = args[0]; elsetoName = "localhost";

if (toName.equals("localhost"))// Use two ports for localhost receiverPort = 222;

try { ioSocket = new DatagramSocket(ioPort); } catch (Exception e) { ioPort = 222; receiverPort = 111; ioSocket = new DatagramSocket(ioPort); } System.out.println("chat on port " + ioPort + ""); new receiver(ioSocket); // Start receiver thread while ((userIn=in.readLine())!=null) { sendBuffer = userIn.getBytes(); sendPacket = new DatagramPacket( sendBuffer, sendBuffer.length, InetAddress.getByName(toName), receiverPort); ioSocket.send(sendPacket); // Send datagram } System.exit(0); // Exit, stopping receiver } } class receiver implements Runnable { DatagramSocketioSocket; receiver(DatagramSocketio Socket) { this.ioSocket = ioSocket; new Thread(this).start();// Start thread at run() } public void run() { bytereceiveBuffer[] = new byte[128]; DatagramPacketreceivePacket = new DatagramPacket(receiveBuffer, 128); while(true) { try { ioSocket.receive(receivePacket); }// Wait for datagram catch (Exception e) {} System.out.println( new String(receivePacket.getData(), 0, receivePacket.getLength())); } }

} SAMPLE OUTPUT: Start the Chat.java program for client1 C:\Program Files\Java\jdk1.5.0_06\bin>javac chat.java -Xlint C:\Program Files\Java\jdk1.5.0_06\bin>java chat Hi Rec : Hello Start the Chat.java program for client2 C:\Program Files\Java\jdk1.5.0_06\bin>javac chat.java -Xlint C:\Program Files\Java\jdk1.5.0_06\bin>java chat

Rec: Hi hello RESULT:Two way TCP / IP and UDP chat application is done using Java.

Event Driven Programs using Java Applets Ex. No: 7 Date : 04/10/12

PROBLEM STATEMENT Create a Event Driven Programs using Java Applets CODE:
import java.applet.Applet; import java.awt.*;

public class pga extends Applet{ String temp; public void init(){ } public void paint (Graphics g) { int num=5; g.drawString("Displayed string",10,10); g.drawLine(55,55,32,32); g.drawOval(200,50,50,50); g.fillOval(70,100,40,40); g.drawOval(10,30,20,40); g.fillOval(100,150,30,40); g.drawRect(170,170,40,15); g.fillRect(120,21,70,15); g.drawArc(50,50,70,70,0,75); g.fillArc(175,30,30,50,0,90);

int x[]={30,100,30,100,30}; int y[]={30,100,30,100,30}; g.drawPolygon(x,y,num); }}

Sample output:

Code:
import java.applet.Applet; import java.awt.*;

public class pgb extends Applet { String s; public void init(){ } public void start(){

} public void paint(Graphics g){

g.drawString("font families:", 10, 20); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String fontList[] = ge.getAvailableFontFamilyNames(); for(int i=0;i<fontList.length;i++) g.drawString(fontList[i],10,30 + 10*i); }}

Sample Output:

Code:

import java.applet.Applet; import java.awt.*; public class pgc extends Applet { String S; public void init() { setBackground(Color.CYAN); setForeground(Color.BLUE); } public void start() { S="Applet STARTS"; } @Override public void paint(Graphics g) { g.drawString(S,50,20); showStatus("STATUS MESSAGE"); } }

Sample Output:

Code:
import java.applet.Applet; import java.awt.*; import java.net.*; import java.util.*;

public class pgd extends Applet{ URL u1,u2; String s1,s2;

public void paint(Graphics g) { u1=getCodeBase(); s1="code base:"+u1.toString(); g.drawString(s1,10,20); u2=getDocumentBase(); s2="doc base:"+u2.toString(); g.drawString(s2,10,60); }

public void init(){ } }

Sample Output:

Result: Event Driven Programs using Java Applets were created successfully.

Programs using AWT Controls and Layouts in Java Ex. No: 8 Date : 11/10/12

PROBLEM STATEMENT Use AWT controls and program in Java Code:


import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;

public class QuestionA implements ActionListener { private JFrame frame; private JButton buttonYes; private JButton buttonNo; private JLabel label; private JPanel panel;

public static void main(String[] args) { QuestionA qa = new QuestionA(); qa.go();

public void go() { //frame frame = new JFrame();

//button buttonYes = new JButton("Yes"); buttonYes.addActionListener(this);

buttonNo = new JButton("No"); buttonNo.addActionListener(this);

frame.setSize(200, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//panel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

//adding components frame.getContentPane().add(panel); panel.add(buttonYes); panel.add(buttonNo); label = new JLabel(); panel.add(label);

frame.setVisible(true); }

public void actionPerformed(ActionEvent ae) { JButton tempButton = (JButton)ae.getSource();

if(tempButton.getText().equals("Yes")) { label.setText("Yes"); } else { label.setText("No"); } } }

Sample Output:

Code:
import java.awt.GridLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;

public class QuestionB implements ItemListener { private JFrame frame; private JCheckBox[] checkbox = new JCheckBox[4]; private JLabel[] label = new JLabel[4]; private JPanel panel; private String[] str = {"WINDOWS 98","WINDOWS NT","SOLARIS","MACOS"};

public void go() { frame = new JFrame(); frame.setSize(400,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel(); panel.setLayout(new GridLayout(4,2));

int i; for(i=0;i<4;i++) { checkbox[i] = new JCheckBox(str[i]); if(i==0) { checkbox[i].setSelected(true); } checkbox[i].addItemListener(this); label[i] = new JLabel(); }

//adding components frame.getContentPane().add(panel);

for(i=0;i<4;i++) { panel.add(checkbox[i]); panel.add(label[i]);

if(checkbox[i].isSelected()) { label[i].setText("Selected"); }

else {

label[i].setText("Not Selected"); } }

frame.setVisible(true); }

public void itemStateChanged(ItemEvent ie) { JCheckBox tempCheckBox = (JCheckBox)ie.getSource(); String lbl = tempCheckBox.getText();

int index=0; int i; for(i=0;i<4;i++) { if(lbl.equals(str[i])) { break; } else { index++; } }

if(tempCheckBox.isSelected()) {

label[index].setText("Selected"); }

else { label[index].setText("Not Selected"); } }

public static void main(String[] args) { QuestionB qb = new QuestionB(); qb.go(); } }

Sample Output:

Code:
import java.awt.Checkbox; import java.awt.CheckboxGroup; import java.awt.GridLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;

public class QuestionC implements ItemListener { private JFrame frame; private CheckboxGroup cbg = new CheckboxGroup(); private Checkbox[] checkbox = new Checkbox[4]; private JLabel[] label = new JLabel[4]; private JPanel panel; private String[] str = {"WINDOWS 98","WINDOWS NT","SOLARIS","MACOS"};

public void go() { frame = new JFrame(); frame.setSize(400,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel();

panel.setLayout(new GridLayout(4,2));

int i; for(i=0;i<4;i++) { if(i==0) { checkbox[i] = new Checkbox(str[i], cbg, true); } else { checkbox[i] = new Checkbox(str[i], cbg, false); } checkbox[i].addItemListener(this); label[i] = new JLabel(); }

//adding components frame.getContentPane().add(panel);

for(i=0;i<4;i++) { panel.add(checkbox[i]); panel.add(label[i]);

if(checkbox[i].getState()) {

label[i].setText("Selected"); } else { label[i].setText("Not Selected"); } }

frame.setVisible(true); }

public void itemStateChanged(ItemEvent ie) { Checkbox tempCheckBox = (Checkbox)ie.getSource(); String lbl = tempCheckBox.getLabel();

int index=0; int i; for(i=0;i<4;i++) { if(lbl.equals(str[i])) { break; } else { index++; }

for(i=0;i<4;i++) { if(i==index) { label[i].setText("Selected"); }

else { label[i].setText("Not Selected"); } } }

public static void main(String[] args) { QuestionC qc = new QuestionC(); qc.go(); } }

Sample Output:

Code:

import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.ListSelectionModel; import javax.swing.ScrollPaneConstants; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener;

public class QuestionD implements ListSelectionListener { private String[] list_os_entries = {"WINDOWS 98","WINDOWS NT","LINUX","SOLARIS","MACOS"}; private String[] list_browser_entries = {"IE","NETSCAPE","LYNX","FIREFOX","OPERA"}; private JList os_list; private JList browser_list; private JFrame frame; private JPanel panel; private JLabel label;

public void go() { frame = new JFrame();

frame.setSize(400,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

label = new JLabel();

os_list = new JList(list_os_entries); os_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane scroller1 = new JScrollPane(os_list); scroller1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

scroller1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

browser_list = new JList(list_browser_entries); browser_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane scroller2 = new JScrollPane(browser_list); scroller2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

scroller2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

panel = new JPanel(); panel.setLayout(new GridLayout(3,1)); frame.getContentPane().add(panel); panel.add(scroller1); panel.add(scroller2); panel.add(label);

browser_list.addListSelectionListener(this); browser_list.setSelectedIndex(0); os_list.addListSelectionListener(this); os_list.setSelectedIndex(0); frame.setVisible(true); }

public void valueChanged(ListSelectionEvent lse) { if(!lse.getValueIsAdjusting()) { String os = (String)os_list.getSelectedValue(); String browser = (String)browser_list.getSelectedValue(); label.setText("Message: "+browser+" open in "+os+" OS"); } }

public static void main(String[] args) { QuestionD qd = new QuestionD(); qd.go(); } }

Sample Output:

Code:

import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel;

public class QuestionE implements ActionListener { private JFrame frame; private JPanel panel; private JButton[] button = new JButton[16];

public void go() { frame = new JFrame(); frame.setSize(400,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel(); panel.setLayout(new GridLayout(4, 4));

int i; for(i=1;i<=16;i++)

{ button[i-1] = new JButton(Integer.toString(i)); button[i-1].addActionListener(this); panel.add(button[i-1]); }

frame.getContentPane().add(panel); frame.setVisible(true); }

public void actionPerformed(ActionEvent ae) { JButton tempButton = (JButton)ae.getSource(); JOptionPane.showMessageDialog(frame, "Button "+tempButton.getText()+" was clicked!!"); }

public static void main(String[] args) { QuestionE qe = new QuestionE(); qe.go(); } }

Sample Output:

Result: Programs using AWT Controls and Layouts in Java were created successfully.

You might also like