Tuesday, April 22

Java Applet Layouts Demo [Flow, Grid, Border]

This program will explain the basic overview on how to create Applets with Flow, Grid or Border layout. Flow layout is default layout and is selected automatically. For the rest of the types you need to select them manually.


A basic example code about Applets is shown below -
Code :

import java.applet.*;
import java.awt.*;

public class BasicAppletDemo extends Applet
{
 public void init()
 {
  TextField a;
  a = new TextField(15);
  add(a);
 }
}

HTML:

<html>
<head>
<title>Basic Applet Demo</title>
</head>
 <body>
   <applet code=BasicAppletDemo.class width=500 height=500> </applet>
 </body>
</html>



An example of Applet using Grid Layout is shown below -
Code :

import java.applet.*;
import java.awt.*;

public class AppletDemoGridLayout extends Applet
{
 public void init()
 {
  setLayout(new GridLayout(4,2));

  Label nTitle = new Label("Name");
  TextField name= new TextField(15);
  Label rTitle = new Label("Roll Number");
  TextField rollNo=new TextField(15);
  Label addTitle = new Label("Address");
  TextField address = new TextField(15);
  Button save = new Button("Save");
  Button cancel = new Button("Cancel");
  
  add(nTitle);
  add(name);
  add(rTitle);
  add(rollNo);
  add(addTitle);
  add(address);
  add(save);
  add(cancel);
 }
}

HTML:

<html>
<head>
  <title>Applet Grid Layout Demo</title>
</head>
 <body>
<applet code=AppletDemoGridLayout.class width=500 height=500> </applet> </body> </html>



Another example of Applet using Border Layout is shown below -
Code :

import java.applet.*;
import java.awt.*;

public class AppletDemoBorderLayout extends Applet
{
 public void init()
 {
  setLayout(new BorderLayout(5,5));
  Panel p = new Panel();
  p.setLayout(new GridLayout(4,2));

  Label nameLabel = new Label("Name");
  TextField name= new TextField(15);
  Label rollLabel = new Label("Roll Number");
  TextField rollNo=new TextField(15);
  Label addrLabel = new Label("Address");
  TextField address = new TextField(15);
  Button save = new Button("Save");
  Button cancel = new Button("Cancel");
  Label north = new Label("North");
  Label south = new Label("South");
  Label east = new Label("East");
  Label west = new Label("West");
  
  p.add(nameLabel);
  p.add(name);
  p.add(rollLabel);
  p.add(rollNo);
  p.add(addrLabel);
  p.add(address);
  p.add(save);
  p.add(cancel);
  
  add(p,BorderLayout.CENTER);
  add(north,BorderLayout.NORTH);
  add(east,BorderLayout.EAST);
  add(west,BorderLayout.WEST);
  add(south,BorderLayout.SOUTH);
 }
}

HTML:

<html>
<head>
  <title>Basic Applet Demo</title>
</head>
 <body>
   <applet code=AppletDemoBorderLayout.class width=500 height=500> </applet>
 </body>
</html>

You can download all the above source code from the link here.

No comments:

Post a Comment

Comment anything you want. Just be polite and give respect to others!
I am simply going to remove the comments which are offensive or are off topic.
And please don't spam your website links in comments. I don't, neither should you.