SWING Lab1
SWING Lab1
FlowLayout is the default layout manager for every JPanel. It simply lays out
components in a single row one after the other.
Java GridBagLayout
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.
ActionEvent ActionListener
MouseEvent MouseListener and
MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
Steps to perform Event Handling
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class AEvent extends JFrame implements ActionListener
{
AEvent() public void actionPerformed(ActionEvent e)
{ {
//create components System.exit(0);
Button b=new Button("quit"); }
b.setBounds(100,120,80,30); public static void main(String args[])
{
//register listener new AEvent();
//passing current instance }
b.addActionListener(this); }
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent()
{ public void actionPerformed(ActionEvent e)
//create components {
tf=new TextField(); tf.setText("Welcome");
tf.setBounds(60,50,170,20); }
Button b=new Button("click me"); public static void main(String args[])
b.setBounds(100,120,80,30); {
new AEvent();
//register listener }
b.addActionListener(this);//passing current instance