Multi Threading, Frame and EH
Multi Threading, Frame and EH
creating a program in which more than one process could execute simultaneously.for creating
a multithread programming we can use two techiques-
in both the cases you must override run() method which specifies the functionality of
a thread.The general form of run() method will be-
4. sleep()- this method pause the execution of thread for a short period of time.It throws the
InterruptedException. it contains time in millisecond as argument.
6. join()- stops the execution of next statement until all threads executed.
Thread t;
String name;
int i;
public thdemo(String n)
name=n;
t=new Thread(this);
t.start();
for(i=1;i<=5;i++)
System.out.println(name);
try{
Thread.sleep(1000);
}catch(InterruptedException e)
{}
class test
_____________________________________________________________
Thread t;
String name;
int i;
public thdemo(String n)
name=n;
t=new Thread(this);
t.start();
for(i=1;i<=5;i++)
{
System.out.println(name);
try{
Thread.sleep(2000);
}catch(InterruptedException e)
{}
class test
try{
t1.t.join();
t2.t.join();
t3.t.join();
}catch(InterruptedException e)
{}
_________________________________________________________________________
Synchronization in Thread- if more than one process (objects) want to access a single
resourse then the synchronization problem could occur.
class callme
System.out.print("["+name);
try{
Thread.sleep(1000);
}catch(InterruptedException e){}
System.out.println("]");
}
}
callme ob;
String msg;
ob=o;
msg=n;
new Thread(this).start();
ob.mainfun(msg);
class test
Working with Frame- Frame is used for creating a windows based application.
Constructors
Frame()
Frame(String title)
Methods
Example
import java.awt.*;
public win()
setSize(400,300);
setVisible(true);
}
class test
____________________________________
Layout Managers- The Layout Managers allows for setting the layout of a window means how
the controls will arrange on the window, it will set by Layout Managers.For setting layout
managers we must use setLayout() method.
1. FlowLayout
2. GridLayout
3. BorderLayout
1. FlowLayout- This is the default layout is the applet.and it arrange the components in
relative manner.
Constructors
FlowLayout()
FlowLayout(int align)
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
Example
import java.awt.*;
Label l1;
TextField t1;
Button b1;
FlowLayout flow;
public win()
flow=new FlowLayout(FlowLayout.LEFT);
setLayout(flow);
t1=new TextField(20);
b1=new Button("Click");
add(l1);
add(t1);
add(b1);
setSize(400,300);
setVisible(true);
}
}
class test
_________________________________________
Constructor
import java.awt.*;
Label l1,l2,l3;
TextField t1,t2;
Button b1;
GridLayout grid;
public win()
setLayout(grid);
l3=new Label("");
t1=new TextField(20);
t2=new TextField(20);
b1=new Button("Login");
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(b1);
setSize(400,150);
setVisible(true);
class test
}
}
____________________________________________
BorderLayout- This Layout Managers Class arrange the elements into specific zone of the
window may be East,West,North,South or Center by default it is Center & this is default
layout of the Frame.
Example
import java.awt.*;
Label l1,l2;
public win()
setLayout(new BorderLayout());
l1.setBackground(Color.pink);
l2=new Label("Naini");
add(l1,"North");
add(l2,"South");
setSize(400,150);
setVisible(true);
}
class test
__________________________________________________________
import java.awt.*;
Label l1,l2;
public win()
setLayout(new BorderLayout());
l1.setBackground(Color.pink);
l2=new Label("Naini");
add(l1,"North");
add(l2,"South");
setSize(400,150);
setVisible(true);
}
class test
__________________________________________
Note- for setting your own layout you can use setBounds() method with following syntax-
objectname.setBounds(x,y,width,height)
Example
import java.awt.*;
Label l1,l2;
TextField t1,t2;
Button b1;
public win()
setTitle("Login Window");
setLayout(null);
t1=new TextField(20);
t2=new TextField(20);
b1=new Button("Login");
l1.setBounds(10,40,150,20);
t1.setBounds(160,40,120,20);
l2.setBounds(10,70,150,20);
t2.setBounds(160,70,120,20);
b1.setBounds(160,100,120,20);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
setSize(400,150);
setVisible(true);
class test
{
win obj=new win();
Event Handling- Event is an action which indicates happening of any operation.for handling
the event in java we must import java.awt.event package & we must consider on two things-
1. Event Source
2. Event Listener
1. Event Source- It is an object like button, textbox etc on which we perform the event.Each
object support some common events and some special event also.
2. Event Listener Interfaces- java provides a lot of listener interfaces which allows you for
handling the particular event. The listener interfaces provides the scaleton on which we
implement the event.
for handling any event you must follow the following steps-
_______
b1.addListenerType(this);
keyReleased(KeyEvent)
keyTyped(KeyEvent)
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mousePressed(MouseEvent)
mouseReleased(MouseEvent)
windowClosed(WindowEvent)
windowClosing(WindowEvent)
windowDeactivated(WindowEvent)
windowDeiconified(WindowEvent)
windowIconified(WindowEvent)
windowOpened(WindowEvent)
Using ActionListener-
import java.awt.*;
import java.awt.event.*;
Button b1,b2,b3;
public win()
setLayout(new FlowLayout());
b1=new Button("Red");
b2=new Button("Blue");
b3=new Button("Green");
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
setSize(400,150);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
if(e.getSource()==b1)
setBackground(Color.red);
else if(e.getSource()==b2)
setBackground(Color.blue);
else if(e.getSource()==b3)
setBackground(Color.green);
class test
___________________________________________________________________
Using TextListener-
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
{
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1;
public text()
setTitle("Welcome");
setLayout(new GridLayout(4,2));
setSize(440,250);
l2=new Label("Ta");
l3=new Label("Da");
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
b1=new Button("Exit");
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
t1.addTextListener(this);
b1.addActionListener(this);
setVisible(true);
if(e.getSource()==b1)
System.exit(0);
if(e.getSource()==t1)
int bs,ta,da;
bs=Integer.parseInt(t1.getText());
ta=(bs*4)/100;
da=(bs*6)/100;
t2.setText(Integer.toString(ta));
t3.setText(Integer.toString(da));
____________________________________________________________
using ItemListener
import java.awt.*;
import java.awt.event.*;
Label l1;
Button b1;
public item(String s)
super(s);
setLayout(new FlowLayout());
setSize(240,150);
setBackground(Color.red);
c1.addItem("Red");
c1.addItem("Green");
c1.addItem("Blue");
c1.addItem("Yellow");
b1=new Button("Exit");
add(l1);
add(c1);
add(b1);
c1.addItemListener(this);
b1.addActionListener(this);
setVisible(true);
if(e.getSource()==b1)
System.exit(0);
}
}
if(e.getSource()==c1)
String s;
s=c1.getSelectedItem();
if(s.equals("Red"))
setBackground(Color.red);
else if(s.equals("Blue"))
setBackground(Color.blue);
else if(s.equals("Green"))
setBackground(Color.green);
else if(s.equals("Yellow"))
setBackground(Color.yellow);
________________________________________________________________
import java.awt.event.*;
Button b1;
int w=250,h=150,count=0;
public mouse(String s)
super(s);
setLayout(new FlowLayout());
setSize(w,h);
add(b1);
b1.addMouseListener(this);
setVisible(true);
if(e.getSource()==b1)
{
setBackground(Color.pink);
{}
{}
count++;
if(e.getSource()==b1)
w+=50;
h+=20;
setSize(w,h);
System.out.println(w+"\t"+h);
setBackground(Color.yellow);
}
}
_____________________________________________________
Key Listener
import java.awt.*;
import java.awt.event.*;
Button b1;
int x=50,y=200;
public key(String s)
super(s);
setLayout(new FlowLayout());
setSize(240,150);
b1=new Button("KeyPress");
add(b1);
b1.addKeyListener(this);
setVisible(true);
if(e.getSource()==b1)
setBackground(Color.red);
setSize(300,400);
setLocation(x,y);
x+=10;
y+=20;
b1.setLabel("Leave Me");
if(e.getSource()==b1)
setBackground(Color.blue);
b1.setLabel("Press Me");
setSize(240,150);
_________________________________________________________
Adapter Classes- The problem of Listener interfaces is that we must define all the handlers
either they are required or not?
We can solve this problem by using Adapter class. The adapter classes are-
1. KeyAdapter
2. MouseAdapter
3. WindowAdapter
Example
import java.awt.*;
import java.awt.event.*;
class win extends Frame
public win()
setLayout(new FlowLayout());
setSize(400,150);
addWindowListener(new WindowAdapter()
System.exit(0);
});
setVisible(true);
class test
}
}