Unit 2
Unit 2
}
}
}
Inner Classes
■ Inner classes are nested classes, means this classes are defined inside the other classes.
■ By using inner classes we can access the members of outer class also.
■ Inner class allows us to simplify the code by using Adapter class.
■ Example:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<appletcode=InnerClassDemo width=500 height=500></applet>*/
public class InnerClassDemo extends Applet
{
public void init()
{
addMouseListener(new MymouseAdapter());//MymouseAdapter is userdefined class and here
we have not used this keyword. Because inner class provides direct access to outer class
methods .also there is no need to declare objects for outer class.
}
class MymouseAdapter extends MouseAdapter
{
public void mouseClicked(MouseEvent m)
{
showStatus(“mouse clicked”);
}
}
}
Different Types of Inner Classes
1. Member Inner Class
It is the inner class which is created inside the class but outside the method.
Example:
class outer
{
class inner
{
System.out.println(“INNER CLASS”);
}
public static void main(String[] args)
{
}
}
2. Anonymous Inner Class
It is the inner class with no name. this class is created for extending the class or implementing
interface.
Note* an Abstarct class is a class that is designed to be specifically used as a base class.
Example:
class a //created abstract class
{
abstract void m1(); // created abstract method
}
class abc
{
public static void main(String[] args)
{
a a1= new a(){ // created the object of class a and the same time giving the
definition of method of class a , then it is called Anonymous inner class.
void m1(){} // Anonymous inner class. And this is definition of method of class
a
};
}
}
3. Local Inner Class
It is the Inner Class created inside the method.
Example:
class outer
{
void m1()
{
class inner() // local inner class which is created inside the function
{ }
}
}
4. Static member classes
it is the inner class created inside the class but made as static class.
Example:
class outer
{
Static class inner // static member class
{
}
}
User Interface Components
Different user interface components are
■ Label
■ Buttons
■ Canvas
■ Scrollbars
■ Text components
■ Checkbox
■ Choices or combo boxes
■ List panels
■ Dialogs
■ Menubar
All these components have different classes, and all classes have different methods.
We can place all these components on frame for designing user interface. We need to set
layout of the frame. And most commonly used layout is FlowLayout.
Label
import java.awt.*;
class Use_Label
{
public static void main(String[] args)
{
Frame fr= new Frame(“This program is for displaying the label”);
fr.setSize(400,200);
fr.setLayout(new FlowLayout());
fr.setVisible(true);
Label L1= new Label(“ok”);
Label L2= new Label(“cancel”);
fr.add(L1);
fr.add(L2);
}
}
Buttons
import java.awt.*;
class Use_Button
{
public static void main(String[] args)
{
Frame fr= new Frame(“This program is for displaying the Button”);
fr.setSize(400,200);
fr.setLayout(new FlowLayout());
fr.setVisible(true);
Button B1= new Button(“ok”);
Button B2= new Button(“cancel”);
fr.add(B1);
fr.add(B2);
}
}
Canvas
• Canvas is the special area created on frame.
• It is specially used for drawing the graphical components such as oval, rectangle, line and so on.
import java.awt.*;
class Use__Canvas
{
public static void main(String[] args)
{
Frame fr=new Frame("this program has canvas");
Canvas C1= new Canvas();
C1.setSize(120,120);
C1.setBackground(Color.blue);
fr.setLayout(new FlowLayout());
fr.setSize(250,250);
fr.setVisible(true);
fr.add(C1);
}
}
Scrollbars
import java.awt.*;
class Use_ScrollBars
{
public static void main(String[] args)
{
Frame fr=new Frame("this program has a Scrollbars ");
Scrollbar H= new Scrollbar(Scrollbar.HORIZONTAL);
Scrollbar V= new Scrollbar(Scrollbar.VERTICAL);
fr.setLayout(new FlowLayout());
fr.setSize(250,250);
fr.setVisible(true);
fr.add(H);
fr.add(V);
}
}
Text Components
import java.awt.*;
class Use_TxtFld
{
public static void main(String[] args)
{
Frame fr= new Frame(“This program is for displaying the TextField”);
fr.setSize(400,200);
fr.setLayout(new FlowLayout());
fr.setVisible(true);
Label L1= new Label(“Enter name:”);
TextField T1= new TextField(10); // handles single line text
Label L2= new Label(“Enter address:”);
TextArea TA= new TextArea(10,20); //handles multi line text TextArea(int n, int m), where n is no.of lines and m is no.of characters
fr.add(L1);
fr.add(T1);
fr.add(L2);
fr.add(TA);
}
}
CheckBox
import java.awt.*;
class Use_ChkBox
{
public static void main(String[] args)
{
Frame fr=new Frame("this program is for displaying the checkbox");
fr.setLayout(new FlowLayout());
fr.setSize(250,250);
fr.setVisible(true);
Checkbox b1=new Checkbox(“candy”);
Checkbox b2=new Checkbox(“Ice-Cream”);
Checkbox b3=new Checkbox(“Juice”);
fr.add(b1);
fr.add(b2);
fr.add(b3);
}
}
Choices or ComboBoxes
import java.awt.*;
c2.add(“rose”);
class Use_Choice c2..add(“lily”);
{ c2..add(“lotus”);
public static void main(String[] args)
fr.add(c1);
{ fr.add(c2);
Frame fr=new Frame("this program is for
displaying the choicelist"); }
fr.setLayout(new FlowLayout()); }
fr.setSize(250,250);
fr.setVisible(true);
Choice c1=new Choice();
Choice c2=new Choice();
c1..add(“mango”);
c1..add(“Apple”);
c1..add(“Banana”);
Dialogs and Menubar
■ Dialog box are small window .
■ Dialog and frame are the subclass of window class
■ The only one difference between frame and dialog is dialog doesnot support menu bar.
■ It is used to hold the set of related controls.
■ For eg while installing any software if you find error then it is shown by dialogbox
■ There are two types of Dialog box modal and modeless Dialog box
■ In modal Dialog box user need to give input or response compulsory to dialogbox then
only other windows will be visible
■ In modeless Dialog box user don’t need to give input or response compulsory.
Graphics import java.awt.*;
public class RectangleDemo extends Canvas
{
■ Java has ability to draw the various public RectangleDemo()
graphical shapes. {
■ We can draw line , rectangle, oval and setSize(200,200);
other shapes etc. setBackground(Color.blue);
}
■ These object can be colored using
various colors. public static void main(String[] args)
{
■ Canvas is the special area, on which RectangleDemo obj=new RectangleDemo();
display can be created and we can use Frame fr=new Frame("Rectangle");
canvas to display various graphical fr.setSize(300,300);
shapes. fr.add(obj);
■ If we take the example of rectangle we fr.setVisible(true);
need to create a frame on that we need a }
special area that is canvas.
public void paint(Graphics g)
Example Code: {
g.drawRect(10,10,50,50);
}
}
Layout Managers
■ Layout manager used to arrange the Components in the particular manner.
■ Different types of Layouts
FlowLayout
BorderLayout
CardLayout
GridLayout
GridBagLayout
FlowLayout
■ It is the Default layout of java.
■ It is used to arrange GUI components line by line.
■ Example Code:
import java.awt.*;
import java.applet.*;
/*<appletcode=flowdemo width=400 height=400></applet>*/
public class flowdemo extends applet{ add(t1);
add(t2);
TextField t1,t2,t3; add(t3);
}
public void init(){
setLayout(new FlowLayout(FlowLayout.RIGHT)); }
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
Border Layout b4=new Button(“South”);
■ Border layout have Five regions and at that region we can
place GUI components it may be button ,checkbox etc. b5=new Button(“Center”);
add(b1,BorderLayout.EAST);
■ The five regions are East, West ,North, South and center. add(b2, BorderLayout.WEST);
■ Example Code: add(b3, BorderLayout.NORTH);
import java.awt.*; add(b4, BorderLayout.SOUTH);
add(b5, BorderLayout.CENTER);
import java.applet.*; }
/*<appletcode=borderdemo width=400 }
height=400></applet>*/
public class borderdemo extends applet{
Button b1,b2,b3,b4,b5;
public void init(){
setLayout(new BorderLayout());
b1=new Button(“East”);
b2=new Button(“West”);
b3=new Button(“North”);
GridLayout
■ Grid layout arrange GUI components in Grid b4=new Button(“4”);
manner. That is in the form of rows and columns. b5=new Button(“5”);
■ If we want to make 3x3 grid layout then b6=new Button(“6”);
b7=new Button(“7”);
■ Example Code: b8=new Button(“8”);
import java.awt.*; b9=new Button(“9”);
add(b1);
import java.applet.*;
add(b2);
/*<appletcode=griddemo width=400 add(b3);
height=400></applet>*/ add(b4);
public class griddemo extends applet{ add(b5);
add(b6);
Button b1,b2,b3,b4,b5,b6,b7,b8,b9; add(b7);
public void init(){ add(b8);
add(b9);
setLayout(new GridLayout(3,3)); }
b1=new Button(“1”); }
b2=new Button(“2”);
b3=new Button(“3”);
CardLayout
■ In card Layout we see at a time only one
components. add(b1);
■ We see other components only after handling add(b2);
1st component. add(b3);
}
■ Different methods of cardLayout is
next(),previous(),first(),last() Public boolean keyDown(Event e, int Key)
{
■ Example code:
new CardLayout().next(this);
import java.awt.*; return true;
import java.applet.*; }
}
/*<appletcode=carddemo width=400
height=400></applet>*/
public class carddemo extends applet{
Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
public void init(){
setLayout(new CardLayout());
b1=new Button(“card1”);
b2=new Button(“card2”);
b3=new Button(“card3”);