What is WindowEvent?
We know whenever a component is interacted by the user, an event is generated. For example, when a button is clicked, the button raises ActionEvent. Similarly now, when the frame is clicked it raises WindowEvent and infact this object is passed as parameter to the seven methods. Handling the event raised by a component is the first and foremost step in event handling. By handling the WindowEvent, the application (here, frame) is closed with System.exit(0).
System.exit(0);
The above statement terminates the current application; in this case, the frame closes. The exit(0) indicates normal shutdown and any parameter other than 0 (like 1) is abnormal termination; of course, this is operating system dependent.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseClick extends JFrame implements ActionListener{
JButton b1,b2;
JLabel lbl;
int clicked;
final String str=(“Number Of Times OK-Button Clicked”);
public MouseClick(String strName){
super(strName);
getContentPane().setLayout(new FlowLayout());
JPanel p=new JPanel();
b1=new JButton(“OK”);
b2=new JButton(“QUIT”);
p.setLayout(new FlowLayout());
p.add(b1);
p.add(b2);
lbl=new JLabel(str+clicked,JLabel.CENTER);
getContentPane().add(p);
getContentPane().add(lbl);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
if(“OK”.equals(s)){
clicked++;
lbl.setText(str + clicked);
}
if(“QUIT”.equals(s)){
System.exit(0);
}
}
public static void main(String[]ar){
MouseClick mc=new MouseClick(“MOUSE CLICK COUNT”);
mc.setSize(400,400);
mc.setVisible(true);
}
}
// How I add mouse ENTER & OUT facility in this program.
Implement MouseListener and override, mouseEntered() and mouseExited().
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame
{
public static void main(String args[])
{
MyFrame f=new MyFrame();
f.setTitle(“My AWT Frame”);
f.setSize(300,400);
f.setVisible(true);
f.addWindowListener(new Myclass());
}
}
class Myclass implements WindowListener
{
// override the 7 abstract methods of WindowListener
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeconified(WindowEvent e){}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowClosed(WindowEvent e){}
public void windowOpened(WindowEvent e){}
}
// this program is not run
there is an error Myclass is not abstract and we can not override the abstract method of WL interface
Your program is very correct but for a small spelling mistake. Correct like this:
public void windowDeiconified(WindowEvent e){}
thanks sir,
please tell me we can put the access specifer before constructor in java?
from my knowledge of c++ we r not use access specifer ……
Yes, you can place. But before placing “private” think twice.
sir that was very nice …..
import java.awt.*;
import java.awt.event.*;
public class Demo extends Frame
{
public Demo(String title)
{
super(title) ;
Label lab=new Label();
lab.setText(“ok”);
add(lab,”North”);
}
public static void main(String args[])
{
Demo d=new Demo(“pl”);
d.setVisible(true);
d.setSize(400,400);
d.addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
/* while executing this prog. it is not colosing the window after all i hv used window listener plg explain */
Try this:
import java.awt.*;
import java.awt.event.*;
public class Demo extends Frame
{
public Demo()
{
Label lab=new Label();
lab.setText(“ok”);
add(lab,”North”);
}
public static void main(String args[])
{
Demo d=new Demo();
d.setVisible(true);
d.setSize(400,400);
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class Demo1 extends JFrame
{
public Demo1(String title)
{
super(title);
Label lab=new Label();
lab.setText(“ok”);
add(lab,”North”);
}
public static void main(String args[])
{
Demo1 d=new Demo1(“pl”);
d.setVisible(true);
d.setSize(400,400);
d.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
d.addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
What you want me in this code?
public abstract void windowClosing(java.awt.event.WindowEvent);
why written abstrat in this methode
WindowListener, being interface, all methods must be abstract.
difference b/w WindowAdapter and WindowListener?
WA is abstract class and WL is interface. WA makes event handling code simple.