UNIT 5 Oops
UNIT 5 Oops
Window
The window is the container that has no borders and menu bars. You must use frame, dialog or
another window for creating a window.
Panel
The Panel is the container that doesn't contain title bar and menu bars. It can have other components
like button, textfield etc.
Frame
The Frame is the container that contain title bar and can have menu bars. It can have other
components like button, textfield etc.
Example:
import java.awt.*;
import java.awt.event.*;
class MyLoginWindow extends Frame
{
TextField name,pass;
Button b1,b2;
MyLoginWindow()
{
setLayout(new FlowLayout());
this.setLayout(null);
Label n=new Label("Name:",Label.CENTER);
Label p=new Label("password:",Label.CENTER);
name=new TextField(20);
pass=new TextField(20);
pass.setEchoChar('#');
b1=new Button("submit");
b2=new Button("cancel");
this.add(n);
this.add(name);
this.add(p);
this.add(pass);
this.add(b1);
this.add(b2);
n.setBounds(70,90,90,60);
p.setBounds(70,130,90,60);
name.setBounds(200,100,90,20);
pass.setBounds(200,140,90,20);
b1.setBounds(100,260,70,40);
b2.setBounds(180,260,70,40);
}
public static void main(String args[])
{
MyLoginWindow ml=new MyLoginWindow();
ml.setVisible(true);
ml.setSize(400,400);
ml.setTitle("my login window");
}
}
Output:
➔Event handling:
Changing the state of an object is known as an event. For example, click on button, dragging mouse
etc. The java.awt.event package provides many event classes and Listener interfaces for event
handling.
For example:
Mouse Listener
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Mouse implements MouseListener
{
TextArea s;
public Mouse()
{
Frame d=new Frame("kkkk");
s=new TextArea("");
d.add(s);
s.addMouseListener(this);
d.setSize(190, 190);
d.show();
}
public void mousePressed(MouseEvent e)
{
System.out.println("MousePressed");
int a=e.getX();
int b=e.getY();
System.out.println("X="+a+"Y="+b);
}
public void mouseReleased(MouseEvent e)
{
System.out.println("MouseReleased");
}
public void mouseEntered(MouseEvent e)
{
System.out.println("MouseEntered");
}
public void mouseExited(MouseEvent e)
{
System.out.println("MouseExited");
}
public void mouseClicked(MouseEvent e)
{
System.out.println("MouseClicked");
}
public static void main(String arg[])
{
Mouse a=new Mouse();
}
}
ACTION LISTENER
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class A extends JFrame implements ActionListener
{
A()
{
JPanel buttonpanel = new JPanel();
JButton b1 = new JButton("Hai");
buttonpanel.add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
System.out.println(“Hai button”);
}
public static void main(String args[])
{
A f = new A();
f.setTitle("ActionListener");
f.setSize(500,500);
f.setVisible(true);
}