EXPERIMENT NO:13
X : PROGRAM CODE:
Q. Debug the following code and write the output.
Code:
import java.awt.*;
import java.awt.event.*;
public class program
{
Frame f;
program() {
f=new Frame("Window Adapter");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
f.dispose();
}
});
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String []args) {
new program();
}}
Output:
XIII : EXERCISE:
1. Program to demonstrate the use of WindowAdapter class.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class program extends WindowAdapter {
program() {
JFrame f=new JFrame();
f.addWindowListener(this);
EXPERIMENT NO:13
f.setSize(400,400);
f.setVisible(true);
}
public void windowClosing(WindowEvent e) {
System.out.println("Thank You!! Window is Closed");
System.exit(0);
}
public static void main(String []args) {
new program();
}
}
Output:
2. Program to demonstrate the use of anonymous inner class.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class program extends JFrame {
JLabel l1;
program() {
setLayout(new FlowLayout());
l1=new JLabel(" ");
addKeyListener(new Test());
add(l1);
setSize(400,400);
setVisible(true);
}
public class Test extends KeyAdapter {
public void keyPressed(KeyEvent e) {
l1.setText(e.getKeyText(e.getKeyCode())+" Key Pressed");
}
}
public static void main(String []args) {
EXPERIMENT NO:13
new program();
}
}
Output:
3. Program to program using MouseMotionAdapter Class to implement only one
method mouseDragged().
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class program extends MouseMotionAdapter {
JFrame f;
JLabel l1;
program() {
f=new JFrame("MouseMotionAdapter");
f.setLayout(new FlowLayout());
l1=new JLabel(" ");
f.addMouseMotionListener(this);
f.add(l1);
f.setSize(400,400);
f.setVisible(true);
}
public void mouseDragged(MouseEvent e) {
l1.setText("Mouse Dragged at x= "+e.getX()+" y= "+e.getY());
}
public static void main(String []args) {
new program();
}
}
Output:
EXPERIMENT NO:13