Event handling programs
Event handling programs
lab1()
{
setLayout(new FlowLayout());
add(l1);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setTitle("Action Event Demonstration");
setSize(500,600);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("OK"))
l1.setText("OK BUTTON CLICKED");
else
l1.setText("CANCEL BUTTON CLICKED");
}
public static void main(String[] args)
{
new lab1();
}
}
//Action Events getwhen() example
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import javax.swing.*;
Action2()
{
l1.setBounds(50,350,60,30);
l2.setBounds(50,400,200,200);
t1.setBounds(100,100,300,60);
b1.setBounds(100,200,100,60);
setLayout(null);
add(t1);add(b1);add(l1);add(l2);
b1.addActionListener(this);
setSize(500,600);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String x;
x=t1.getText();
l1.setText(x);
long when = e.getWhen();
Date date = new Date(when);
l2.setText("The button was clicked"+date);
}
public static void main(String[] args)
{
new Action2();
}
}
check1.addItemListener( this);
check2.addItemListener( this);
add(check1);
add(check2);
l.setBounds(150,300,200,100);
add(l);
setVisible(true);
setSize(400,500);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getItemSelectable() == check1)
{
l.setText("clicked checkbox
1"+(e.getStateChange()==1?"State changed":"Not Changed"));
}
else if(e.getItem()==check2)
{
l.setText("clicked checkbox 2");
}
}
public static void main(String[] args )
{
new item();
}
}
//Key Event Example
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
char code2=e.getKeyChar();
System.out.println("getKeyChar()"+code2);
String s=e.getKeyModifiersText(code1);
System.out.println("getKeyModifiersText"+s);
String s1=e.getKeyText(code1);
System.out.println("getKeyText"+s1);
}
public void keyPressed(KeyEvent e)
{
int code=e.getKeyCode();
String s=null;
switch(code)
{
case KeyEvent.VK_SHIFT : s= "SHift Key Pressed";
break;
case KeyEvent.VK_CONTROL: s= "Control Key
Pressed";
break;
case KeyEvent.VK_ALT : s= "Alt Key Pressed";
break;
}
l1.setText(s);
}
public void keyReleased(KeyEvent e)
{
l1.setText("Key Released");
}
public static void main(String[] args)
{
new key();
}
}
window1()
{
setTitle("Window EVent Example");
setSize(500,500);
setVisible(true);
addWindowListener(this);
addWindowFocusListener(this);
}
@Override
public void windowGainedFocus(WindowEvent arg0)
{
System.out.println("Window Gained Focus\n");
}
@Override
public void windowLostFocus(WindowEvent arg0)
{
System.out.println("Window Lost Focus\n");
}
public static void main(String[] args)
{
window1 w=new window1();
}
@Override
public void windowActivated(WindowEvent arg0)
{
System.out.println("Window Activated\n");
}
@Override
public void windowClosed(WindowEvent arg0)
{
System.out.println("Window Closed\n");
}
@Override
public void windowClosing(WindowEvent arg0)
{
System.exit(0);
}
@Override
public void windowDeactivated(WindowEvent arg0)
{
System.out.println("Window DeActivated\n");
}
@Override
public void windowDeiconified(WindowEvent arg0)
{
System.out.println("Window DeIconified \n");
}
@Override
public void windowIconified(WindowEvent arg0)
{
System.out.println("Window Iconfified\n");
}
@Override
public void windowOpened(WindowEvent arg0)
{
System.out.println("Window Opened\n");
}