0% found this document useful (0 votes)
83 views

Java PGM Using Components

The document discusses several Java GUI layout managers including FlowLayout, BorderLayout, GridLayout, CardLayout, and GridBagLayout. It provides code examples for creating frames and applets using each layout manager. It also discusses using components like buttons, labels, text fields, checkboxes, lists, and text areas within the different layouts. Additional topics covered include using external style sheets, creating a basic web page with frames and links, and displaying an HTML form submission using a servlet.

Uploaded by

gayathri devi
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Java PGM Using Components

The document discusses several Java GUI layout managers including FlowLayout, BorderLayout, GridLayout, CardLayout, and GridBagLayout. It provides code examples for creating frames and applets using each layout manager. It also discusses using components like buttons, labels, text fields, checkboxes, lists, and text areas within the different layouts. Additional topics covered include using external style sheets, creating a basic web page with frames and links, and displaying an HTML form submission using a servlet.

Uploaded by

gayathri devi
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

JAVA PGM USING COMPONENTS:

import java.awt.*;
import java.applet.*;
/*<applet code="SimpleApplet.class" width=60 height=250></applet>*/
public class SimpleApplet extends Applet
{Button b1;
public void init( )
{setLayout(new GridLayout(20,2));
Label l1=new Label("Name:",Label.LEFT);
add(l1);
TextField t1=new TextField();
add(t1);
TextArea ta=new TextArea(5,8);
Label l2=new Label("ABOUT",Label.LEFT);
ta.setText("Example\nfor the\nScroll bar\nusing \nText Area");
add(l2);add(ta);
ta.setEditable(false);
setLayout(new GridLayout(25,30));
Checkbox c1=new Checkbox("IP",true);
Checkbox c2=new Checkbox("OS");
add(c1);
add(c2);
Choice color=new Choice();
color.add("RED");
color.add("BLUE");
color.add("GREEN");
add(color);
setLayout(new FlowLayout(FlowLayout.CENTER));
b1=new Button("OK");
add(b1);
}}

FLOW LAYOUT:
import java.awt.*;
class fl extends Frame
{fl(String s){
super(s);
setSize(300,140);
setLayout(new FlowLayout());
for(int i=1;i<=3;i++)
add(new Button("Button No"+i));
setVisible(true);}}
class test
{
public static void main(String args[])
{fl f=new fl("FLOW LAYOUT"); }}

1
BORDER LAYOUT:
import java.awt.*;
class bl extends Frame
{bl(String s)
{super(s);
setSize(300,140);
add(new Button("North"),BorderLayout.NORTH);
add(new Button("South"),BorderLayout.SOUTH);
add(new Button("East"),BorderLayout.EAST);
add(new Button("West"),BorderLayout.WEST);
add(new Button("<--------------->"),BorderLayout.CENTER);
setVisible(true);}}
class test2
{public static void main(String args[])
{bl b=new bl("BORDER LAYOUT");
}}

GRID LAYOUT:
import java.awt.*;
class gl extends Frame
{
gl(String s)
{
super(s);
setSize(300,140);
setLayout(new GridLayout(3,4));
for(int i=1;i<=3;i++)
add(new Button("Button No"+i));
setVisible(true);
}}
class test3
{
public static void main(String a[])
{
gl g=new gl("GRID LAYOUT");
}}

CARD LAYOUT:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="card" width=300 height=300>
</applet>*/
public class card extends Applet implements ActionListener
{

2
Button b1,b2;
CardLayout card1;
Panel pan,pan1,pan2;
public void init()
{
pan=new Panel();
add(pan);
card1=new CardLayout();
pan.setLayout(card1);
pan1=new Panel();
add(pan1);
pan2=new Panel();
b1=new Button("Button1");
b2=new Button("Button2");
b1.addActionListener(this);
b2.addActionListener(this);
add(b1);
add(b2);
pan1.add("Button1",b1);
pan2.add("Button2",b2);
pan.add(pan1,"Button1");
pan.add(pan2,"Button2");
}
public void actionPerformed(ActionEvent ae)
{
card1.next(pan);
repaint();
}
public void paint(Graphics g)
{
}
}

GRID BAG LAYOUT:


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="Gbag" width=400 height=200>
</applet>
*/
public class Gbag extends Applet
{
public void init()
{
GridBagLayout gb=new GridBagLayout();
setLayout(gb);

3
GridBagConstraints Cons1=new GridBagConstraints();
Cons1.fill=GridBagConstraints.BOTH;
Cons1.gridwidth=1;
Cons1.gridheight=1;
Cons1.gridx=0;
Cons1.gridy=0;

GridBagConstraints Cons2=new GridBagConstraints();


Cons2.fill=GridBagConstraints.BOTH;
Cons2.gridwidth=1;
Cons2.gridheight=1;
Cons2.gridx=1;
Cons2.gridy=0;

GridBagConstraints Cons3=new GridBagConstraints();


Cons3.fill=GridBagConstraints.BOTH;
Cons3.gridwidth=2;
Cons3.gridheight=2;
Cons3.gridx=0;
Cons3.gridy=1;

Button b1=new Button("one");


Button b2=new Button("two");
Button b3=new Button("three");
gb.setConstraints(b1,Cons1);
add(b1);
gb.setConstraints(b2,Cons2);
add(b2);
gb.setConstraints(b3,Cons3);
add(b3);
} }

COLOR PALETTE:
import java.awt.*; import java.awt.event.*; import java.applet.*;
/*<applet code=palette height=600 width=600></applet>*/
public class palette extends Applet implements ActionListener,ItemListener
{Button[] Colors;
Checkbox fg,bg;
TextArea wa;
CheckboxGroup cbg;
Panel buttonpanel,checkpanel,palettepanel;
String color;
public void init()
{buttonpanel=new Panel();
buttonpanel.setLayout(new GridLayout(3,3));

4
Colors=new Button[9];
Colors[0]=new Button("RED");
Colors[1]=new Button("GREEN");
Colors[2]=new Button("BLUE");
Colors[3]=new Button("CYAN");
Colors[4]=new Button("ORANGE");
Colors[5]=new Button("WHITE");
Colors[6]=new Button("BLACK");
Colors[7]=new Button("YELLOW");
Colors[8]=new Button("PINK");
for(int i=0;i<9;i++)
{Colors[i].addActionListener(this);
buttonpanel.add(Colors[i]);}
checkpanel=new Panel();
checkpanel.setLayout(new FlowLayout());
cbg=new CheckboxGroup();
fg=new Checkbox("FOREGROUND",cbg,true);
bg=new Checkbox("BACKGROUND",cbg,false);
fg.addItemListener(this);
bg.addItemListener(this);
checkpanel.add(fg); checkpanel.add(bg);
wa=new TextArea(8,40);
wa.setFont(new Font("Garamond",Font.BOLD,20));
palettepanel=new Panel();
palettepanel.setLayout(new BorderLayout());
palettepanel.add(wa,BorderLayout.CENTER);
palettepanel.add(checkpanel,BorderLayout.EAST);
palettepanel.add(buttonpanel,BorderLayout.SOUTH);
add(palettepanel);}
public void itemStateChanged(ItemEvent ie){}
public void actionPerformed(ActionEvent ae)
{color=ae.getActionCommand();
if(fg.getState()==true)
wa.setForeground(getColor());
if(bg.getState()==true)
wa.setBackground(getColor());}
public Color getColor()
{Color mycolor=null;
if(color.equals("RED"))
mycolor=Color.red;
if(color.equals("GREEN"))
mycolor=Color.green;
if(color.equals("BLUE"))
mycolor=Color.blue;
if(color.equals("CYAN"))
mycolor=Color.cyan;

5
if(color.equals("ORANGE"))
mycolor=Color.orange;
if(color.equals("WHITE"))
mycolor=Color.white;
if(color.equals("BLACK"))
mycolor=Color.black;
if(color.equals("YELLOW"))
mycolor=Color.yellow;
if(color.equals("PINK"))
mycolor=Color.pink;
return mycolor;}}

URL:
import java.net.*;
import java.util.*;
import java.io.*;
class UrlDemo
{
public static void main(String[] arg)throws MalformedURLException,IOException
{
try
{
URL u= new URL("file:\\d:\\mathi\\hai.html");
URLConnection uc=u.openConnection();
InputStream in=uc.getInputStream();
int c;
while(( c=in.read())!= -1)
System.out.print((char)c);
System.out.println("content length:"+uc.getContentLength());
System.out.println("content type:"+uc.getContentType());
System.out.println("content encoding:"+uc.getContentEncoding());
System.out.println("date:"+new Date(uc.getDate()));
System.out.println("modified date:"+new Date(uc.getLastModified()));
System.out.println("expired date:"+new Date(uc.getExpiration()));
}
catch(MalformedURLException e)
{
System.out.println("exception caught:"+e);
}
catch(IOException e1)
{
System.out.println("exception caught:"+e1);
}

}}

6
WEB PAGE USING CASCADING STYLE SHEETS

index.html
<html>
<frameset rows="70,*">
<frame src="title.html" name="f1">
<frameset cols="35%,*">
<frame src="link.html" name="f2">
<frame name="f3">
</frameset>
</frameset>
</html>

Title.html
<html>
<head><title>index</title>
</head>
<body bgcolor="magenta">
<center><h1>J2EE AND CASCADING STYLES</h1></center>
</body>
</html>

Link.html
<html>
<head> <title> linking...</title>
</head>
<body bgcolor="green">
<center>
<h2>DO U WANT TO KNOW..</h2>
<br>
<br>
<a href="j2ee.html" target="f3">J2EE</a><br>
<a href="style.html" target="f3">STYLES</a><br>
<br>
<br>
click the appropriate option..<br><br>
<marquee>choose ur lovely platforms....</marquee>
</body>
</html>

J2ee.html
<html>
<head>
<title> working with style sheets </title>
<style type="text/css">
h1 {font-family:monotype corsiva}

7
h2 {background-color:green;background-repeat:repeat-x}
p {font-size:12pt;font-weight:bold;color:#23238e;border-style:groove}
ul {list-style-type:lower-roman}
</style>
</head>
<body bgcolor="pink">
<center>
<h1>WELCOME TO J2EE WORLD</h1>
<h2>Here, you will be able to create interactive server side programs....</h2><br>
<p>J2EE provides many platforms to create server side programming applications... each
application
provides many features... some of the platforms are as follows....</p>
<ul>
<li>SERVLETS</li>
<li>JMS</li>
<li>RMI</li>
<li>CORBA</li>
</ul>
<br><br><br>
<h1><marquee behaviour="right">Thanks for visiting....</marquee></h1>
</body>
</html>

Style.html
<html>
<head>
<title>external style sheets</title>
<link rel=stylesheet href="external.css">
</head>
<body bgcolor="tee">
<center><h2>STYLE INFORMATION</h2></center>
<p>style sheets are powerful mechanism for adding styles to web documents.HTML
elements on a web page
can then be bound to the style sheet.The advantages of a style sheet includes the ability to
make
global changes to all documents from a single location.</p><br>
style attributes are....<br>
<ul type="1" color="red">
<li>font attributes</li>
<li>text attributes</li>
<li>color and background attributes</li>
<li>margin attriutes</li>
</ul>

</body>
</html>

8
External.css
p {font size:12pt;font-weight:bold}
body {margin-top=10%}
h2 {backgroung-image:url(redrose.jpg) }

DISPLAY HTML FORM USING SERVLET


HTML CODING:

<html>
<head><title>WELCOME TO MY SERVLET</title></head>
<body>

<form action="HelloServlet" method="post">


<center>
<br><br><br><br><br>
Name : <input type="text" name="name"><br><br>
Password: <input type="password" name="pwd"><br><br>
<input type="SUBMIT" name="submit" value="submit">
</center>
</form>
</body>
</html>

SERVLET CODING:

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class HelloServlet extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException
{
PrintWriter out=res.getWriter();
res.setContentType("text/html");
String name=req.getParameter("name");
String pwd=req.getParameter("pwd");
out.println("<html>");
out.println("<body>");
out.println("WELCOME!!!!!!" + name);
out.println("</html>");
out.println("</body>");
out.close();
}

9
}

CHAT APPLICATION:
SERVER:
import java.io.*;
import java.net.*;
import java.lang.*;

class Myserver
{
public static void main(String[] args)
{
try{
String str1,str2;
DatagramSocket ds;
DatagramPacket dp1,dp2;
InetAddress ia;
byte[] data1=new byte[1024]; byte[] data2=new byte[1024];
BufferedReader br1;
ds=new DatagramSocket(1500);
ia=InetAddress.getLocalHost();
br1=new BufferedReader(new InputStreamReader(System.in));
while(true)
{

System.out.println("Enter the data to send");


str1=br1.readLine();
data1=str1.getBytes();
dp1=new DatagramPacket(data1,data1.length,ia,1700);
ds.send(dp1);
if(str1.equals("bye"))
break;
dp2=new DatagramPacket(data2,data2.length);
ds.receive(dp2);
str2=new String(dp2.getData(),0,dp2.getLength());
System.out.println("The Received Data "+str2);
if(str2.equals("bye"))
break;
}
}
catch(BindException e)
{
System.out.println(e);
}
catch(SocketException e)
{

10
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}

}
}
CLIENT:
import java.io.*;
import java.net.*;
class Myclient
{
public static void main(String[] args)
{
String str1,str2;
DatagramSocket ds;
DatagramPacket dp1,dp2;
InetAddress ia;
byte[] data1=new byte[1024]; byte[] data2=new byte[1024];
BufferedReader br;
try
{
ds=new DatagramSocket(1700);
ia=InetAddress.getLocalHost();
br=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
dp1=new DatagramPacket(data1,data1.length);
ds.receive(dp1);
str1=new String(dp1.getData(),0,dp1.getLength());
System.out.println("Length is "+ dp1.getLength());
System.out.println("The Received Data "+ str1);
if(str1.equals("bye"))
break;
System.out.println("Enter the data to send");
str2=br.readLine();
data2=str2.getBytes();
dp1=new DatagramPacket(data2,data2.length,ia,1500);
ds.send(dp1);
}
}
catch(BindException e)
{
System.out.println(e);

11
}
catch(SocketException e)
{
System.out.println(e);
}
catch(IOException e)
{ System.out.println(e);
}
}
}

map.html
<html>
<head><title>Image Map</title><head>
<body>
<h1 align="Center">India map<h1>
<center>
<img src="D:\second\winter.jpg" align="center" usemap="#Cities" height="672"
width="597" TITLE="INDIA MAP" ALT="IMAGE DOES NOT EXIST">
</center>
<Map Name="cities">
<area shape="CIRCLE" COORDS="240,556,25" href="d:\second\chennai.html"
TITLE="This Connects to chennai" TABINDEX="1" >
<area shape="CIRCLE" COORDS="119,489,25" href="d:\second\goa.html"
TITLE="This Connects to goa" TABINDEX="1" >
<area shape="CIRCLE" COORDS="195,208,25" href="d:\second\delhi.html"
TITLE="This Connects to delhi" TABINDEX="1" >
<area shape="CIRCLE" COORDS="409,335,25" href="d:\second\kolkatta.html"
TITLE="This Connects to kolkatta" TABINDEX="1" >
</map>
</body>
</html>

Chennai.html
<html>
<head><title>CHENNAI</title></head>
<body BGCOLOR="BLUE" TEXT="WHITE">
<h1><MARQUEE> WELCOME TO SINGARA CHENNAI</MARQUEE></h1>
<BR><BR><BR><BR><BR>
<CENTER><h3>Intersting Places to visit in Chennai</h3></CENTER>
<CENTER>
<OL>
<LI>BEECH</LI>
<LI>VANDALOOR ZOO</LI>
<LI>SPENCER PLAZA</LI>
</OL>

12
<BR><BR><BR><BR><BR><BR>
<B>VISIT US AT........</B>
<BR>
www.suntour.com
</CENTER>
</body></html>

Goa.html
<html>
<head><title>GOA</title></head>
<body BGCOLOR="BLUE" TEXT="WHITE">
<h1><MARQUEE> WELCOME TO GOA</MARQUEE></h1>
<BR><BR><BR><BR><BR>
<CENTER><h3>Intersting Places to visit in goa</h3></CENTER>
<CENTER>
<OL>
<LI>BEECH</LI>
<LI>TEMPLES</LI>
</OL>
<BR><BR><BR><BR><BR><BR>
<B>VISIT US AT........</B>
<BR>
www.suntour.com
</CENTER>
</body>
</html>

Delhi.html
<html>
<head><title>delhi</title></head>
<body BGCOLOR="BLUE" TEXT="WHITE">
<h1><MARQUEE> WELCOME TO DELHI</MARQUEE></h1>
<BR><BR><BR><BR><BR>
<CENTER><h3>Intersting Places to visit in delhi</h3></CENTER>
<CENTER>
<OL>
<LI>RED FORT</LI>
<LI>PARLIAMENT</LI>
<LI>TAJMAHAL</LI>
</OL>
<BR><BR><BR><BR><BR><BR>
<B>VISIT US AT........</B>
<BR>
www.suntour.com
</CENTER>
</body></html>

13
Kolkatta.html
<html>
<head><title>KOLKATTA</title></head>
<body BGCOLOR="green" TEXT="WHITE">
<h1><MARQUEE> WELCOME TO KOLKATTA</MARQUEE></h1>
<BR><BR><BR><BR><BR>
<CENTER><h3>Intersting Places to visit in kolkatta</h3></CENTER>
<CENTER>
<OL>
<LI>VICTORIA PALACE</LI>
<LI>HOWRAH BRIDGE</LI>
</OL>
<BR><BR><BR><BR><BR><BR>
<B>VISIT US AT........</B>
<BR>
www.suntour.com
</CENTER>
</body>
</html>

14
ONLINE EXAMINATION:
SERVLET:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="Servlet", urlPatterns={"/Servlet"})
public class Servlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

int mark=Integer.parseInt(request.getParameter("total"));
out.println("<center><h1>ONLINE EXAM RESULT</h1></center><br>");
out.println("<h1>TOTAL MARK is:"+mark+"</h1>");
} finally {
out.close();
}
}
HTML:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function ff()
{
var m=0;
if(document.f1.p1[2].checked==true)
m=m+1;
if(document.f1.p2[2].checked==true)
m=m+1;
if(document.f1.p3[2].checked==true)
m=m+1;
document.f1.total.value=m;
}
function reset()

15
{
document.f1.p1.visibility=false;
document.f1.p2.visibility=false;
document.f1.p3.visibility=false;
m=0;
}
</script>
</head>
<body>
<center> <h1>ONLINE EXAMINATION</h1></center>
<form name="f1" method="get" action="NewServlet">
<ul><li>Which is the best hard disk seek time?<br>
<input type="radio" name="p1">30ms
<input type="radio" name="p1">20ms
<input type="radio" name="p1">5ms
</li>
<li>The Unix OS was developed using<br>
<input type="radio" name="p2">30ms
<input type="radio" name="p2">20ms
<input type="radio" name="p2">5ms
</li>
<li>A built in memory is termed as<br>
<input type="radio" name="p3">30ms
<input type="radio" name="p3">20ms
<input type="radio" name="p3">5ms
</li>
</ul>
<input type="text" name="total">
<input type="button" value="OK" onclick="ff()">
<input type="submit" value="submit">
<input type="button" value="reset" onclick="reset()">
</form>
</body>
</html>

16

You might also like