© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies


Java Program To Demonstrate Border Layout

Algorithm Steps:

Step 1. Include the applet tag with code set to the class name of the applet as comment

Step 2. Import all the required packages and classes

Step 3. Define class which extends Applet and implements ActionListener

Step 4. Declare buttons green, red, blue, red and font

Step 5. Declare msg of type String and initialize to "sethu Institute"

Step 6. Declare a Textarea area

Step 7. In the init method of the applet, do the following

            i) Set the layout to border layout

            ii) Create area object and set its text to msg and add it to the center of the borderlayout

            iii) Create green button and add it to the north of borderlayout. Similarly, create red, blue,font buttons and add them to south,east, west of border layout.

           iv) Add action listener to all the buttons

Step 8: In the actionperformed method do the following

            i) Get the action command of a button pressed in a string str

           ii) If str is equal to any one of the color button's text then set the foreground to that color.
          iii) If str is equal to font then create a new font of type Garmond and set the area's font to the newly created font

          iv)  Repaint the applet

Step 9: write the paint method with no body (empty paint method)

Java Program To Demonstrate Border Layout

import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ public class BorderLayoutApplet extends Applet implements ActionListener{ Button green=new Button("green"); Button red=new Button("red"); Button blue=new Button("blue"); Button yellow=new Button("yellow"); String msg="BorderLayoutDemo"; TextArea area; public void init() { setLayout(new BorderLayout()); area = new TextArea(); area.setText(msg); add(green,BorderLayout.NORTH); add(red,BorderLayout.SOUTH); add(blue,BorderLayout.EAST); add(yellow,BorderLayout.WEST); add(area,BorderLayout.CENTER); green.addActionListener(this); red.addActionListener(this); blue.addActionListener(this); yellow.addActionListener(this); } public void actionPerformed (ActionEvent a) { String str= a.getActionCommand(); if(str.equals("green")) { Font myfont = new Font("Garamond",Font.BOLD,25); area.setFont(myfont); area.setForeground(Color.green); } else if(str.equals("red")) { area.setForeground(Color.red); } else if(str.equals("blue")) { area.setForeground(Color.blue); } else if(str.equals("yellow")) { area.setForeground(Color.yellow); } else { Font myfont = new Font("Garamond",Font.ITALIC,25); area.setFont(myfont); area.setForeground(Color.cyan); } repaint(); } public void paint(Graphics g) {} }

SAMPLE OUTPUT SCREEN:

 

 
SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑
bottom