Handle Action Events for AWT Button Example
- /*
- Handle Action Events for AWT Button Example
- This java example shows how to handle action event of AWT Button by implementing
- ActionListener interface.
- */
- import java.applet.Applet;
- import java.awt.Button;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- /*
- <applet code="HandleActionEventExample" width=200 height=200>
- </applet>
- */
- public class HandleActionEventExample extends Applet implements ActionListener{
- String actionMessage="";
- public void init(){
- //create Buttons
- Button Button1 = new Button("Ok");
- Button Button2 = new Button("Cancel");
- //add Buttons
- add(Button1);
- add(Button2);
- //set action listeners for buttons
- Button1.addActionListener(this);
- Button2.addActionListener(this);
- }
- public void paint(Graphics g){
- g.drawString(actionMessage,10,50);
- }
- public void actionPerformed(ActionEvent ae){
- /*
- * Get the action command using
- * String getActionCommand() method.
- */
- String action = ae.getActionCommand();
- if(action.equals("Ok"))
- actionMessage = "Ok Button Pressed";
- else if(action.equals("Cancel"))
- actionMessage = "Cancel Button Pressed";
- repaint();
- }
- }
Example Output
Mouse Event Handling in a Frame Window Example
- /*
- Mouse Event Handling in a Frame Window Example
- This java example shows how to handle mouse events in a Frame window
- using MouseListener.
- */
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- /*
- * To create a stand alone window, class should be extended from
- * Frame and not from Applet class.
- */
- public class HandleMouseListenerInWindowExample extends Frame implementsMouseListener{
- int x=0, y=0;
- String strEvent = "";
- HandleMouseListenerInWindowExample(String title){
- //call superclass constructor with window title
- super(title);
- //add window listener
- addWindowListener(new MyWindowAdapter(this));
- //add mouse listener
- addMouseListener(this);
- //set window size
- setSize(300,300);
- //show the window
- setVisible(true);
- }
- public void mouseClicked(MouseEvent e) {
- strEvent = "MouseClicked";
- x = e.getX();
- y = getY();
- repaint();
- }
- public void mousePressed(MouseEvent e) {
- strEvent = "MousePressed";
- x = e.getX();
- y = getY();
- repaint();
- }
- public void mouseReleased(MouseEvent e) {
- strEvent = "MouseReleased";
- x = e.getX();
- y = getY();
- repaint();
- }
- public void mouseEntered(MouseEvent e) {
- strEvent = "MouseEntered";
- x = e.getX();
- y = getY();
- repaint();
- }
- public void mouseExited(MouseEvent e) {
- strEvent = "MouseExited";
- x = e.getX();
- y = getY();
- repaint();
- }
- public void paint(Graphics g){
- g.drawString(strEvent + " at " + x + "," + y, 50,50);
- }
- public static void main(String[] args) {
- HandleMouseListenerInWindowExample myWindow =
- new HandleMouseListenerInWindowExample("Window With Mouse Events Example");
- }
- }
- class MyWindowAdapter extends WindowAdapter{
- HandleMouseListenerInWindowExample myWindow = null;
- MyWindowAdapter(HandleMouseListenerInWindowExample myWindow){
- this.myWindow = myWindow;
- }
- public void windowClosing(WindowEvent we){
- myWindow.setVisible(false);
- }
- }
Example Output
Key Event Example
import java.awt.*;import java.awt.event.*;public class KeyTypedExample {
Label label;
public KeyTypedExample() {
Frame frame = new Frame();
TextField textField = new TextField();
frame.add(textField, BorderLayout.NORTH);
label = new Label();
frame.add(label, BorderLayout.CENTER);
frame.setSize(450, 400);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
textField.addKeyListener(new KeyAdapter() {
/**
* When you type the character "a" into the text field you will see
* an information dialog box
*/
public void keyTyped(KeyEvent ke) {
char keyChar = ke.getKeyChar();
if (keyChar == 'a') {
System.out.println("You typed 'a'");
}
}
/**
* When you type the character "b" into the text field you will see
* an information dialog box
*/
public void keyPressed(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if (keyCode == 66) {
System.out.println("You Typed b");
}
}
/**
* When you type the character "c" into the text field you will see
* an information dialog box
*/
public void keyReleased(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if (keyCode == 67) {
System.out.println("You Typed c");
}
}
});
}
public static void main(String[] args) {
new KeyTypedExample();
}
}
Label label;
public KeyTypedExample() {
Frame frame = new Frame();
TextField textField = new TextField();
frame.add(textField, BorderLayout.NORTH);
label = new Label();
frame.add(label, BorderLayout.CENTER);
frame.setSize(450, 400);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
textField.addKeyListener(new KeyAdapter() {
/**
* When you type the character "a" into the text field you will see
* an information dialog box
*/
public void keyTyped(KeyEvent ke) {
char keyChar = ke.getKeyChar();
if (keyChar == 'a') {
System.out.println("You typed 'a'");
}
}
/**
* When you type the character "b" into the text field you will see
* an information dialog box
*/
public void keyPressed(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if (keyCode == 66) {
System.out.println("You Typed b");
}
}
/**
* When you type the character "c" into the text field you will see
* an information dialog box
*/
public void keyReleased(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if (keyCode == 67) {
System.out.println("You Typed c");
}
}
});
}
public static void main(String[] args) {
new KeyTypedExample();
}
}
Handle Checkbox Event Example
- /*
- Handle Checkbox Event Example
- This java example shows how to handle checkbox event. When checkbox
- is selected and deselected, item event is generated.
- */
- import java.applet.Applet;
- import java.awt.Checkbox;
- import java.awt.Graphics;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- /*
- <applet code="HandleCheckboxEvent" width=200 height=200>
- </applet>
- */
- public class HandleCheckboxEvent extends Applet implements ItemListener{
- Checkbox java = null;
- Checkbox vb = null;
- Checkbox c = null;
- public void init(){
- //create checkboxes
- java = new Checkbox("Java");
- vb = new Checkbox("Visual Basic");
- c = new Checkbox("C");
- add(java);
- add(vb);
- add(c);
- //add item listeners
- java.addItemListener(this);
- vb.addItemListener(this);
- c.addItemListener(this);
- }
- public void paint(Graphics g){
- g.drawString("Java: " + java.getState(),10,80);
- g.drawString("VB: " + vb.getState(), 10, 100);
- g.drawString("C: " + c.getState(), 10, 120);
- }
- public void itemStateChanged(ItemEvent ie) {
- repaint();
- }
- }
Example Output
No comments:
Post a Comment