0% found this document useful (0 votes)
7 views5 pages

JavaAssignment 10

The document contains four Java programs demonstrating event handling in GUI applications using AWT. The first program handles button clicks, the second manages checkbox selections, the third responds to key presses and mouse clicks, and the fourth implements a login and registration form with action listeners. Each program includes a main method to create a GUI window and manage user interactions.

Uploaded by

Bharti Dangi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

JavaAssignment 10

The document contains four Java programs demonstrating event handling in GUI applications using AWT. The first program handles button clicks, the second manages checkbox selections, the third responds to key presses and mouse clicks, and the fourth implements a login and registration form with action listeners. Each program includes a main method to create a GUI window and manage user interactions.

Uploaded by

Bharti Dangi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab Assignment – 10

1. WAP to handle event on button using action listener.


import [Link].*;
import [Link].*;
class ButtonEventExample extends Frame implements ActionListener {
Button b;
ButtonEventExample() {
b = new Button("Click Me");
[Link](100, 100, 100, 40);
add(b);
setSize(300, 300);
setLayout(null);
setVisible(true);

[Link](this);
}
public void actionPerformed(ActionEvent e) {
[Link]("Clicked!");
}
public static void main(String[] args) {
new ButtonEventExample();
}
}

2. WAP to handle event on checkbook using item listener.

import [Link].*;
import [Link].*;

class CheckboxExample extends Frame implements ItemListener {


Checkbox c1, c2;
Label l;
CheckboxExample() {
c1 = new Checkbox("Java");
c2 = new Checkbox("Python");

[Link](100, 100, 80, 30);


[Link](100, 140, 80, 30);

l = new Label();
[Link](100, 180, 200, 30);

add(c1); add(c2); add(l);

[Link](this);
[Link](this);

setSize(300, 300);
setLayout(null);
setVisible(true);
}

public void itemStateChanged(ItemEvent e) {


String msg = "Selected: ";
if ([Link]()) msg += "Java ";
if ([Link]()) msg += "Python";

[Link](msg);
}

public static void main(String[] args) {


new CheckboxExample();
}
}
3. WAP to handle key listener and mouse listener.
import [Link].*;
import [Link].*;

class KeyMouseExample extends Frame implements KeyListener, MouseListener {


Label l;

KeyMouseExample() {
l = new Label("Press key or click mouse");
[Link](50, 100, 250, 30);

add(l);

addKeyListener(this);
addMouseListener(this);

setSize(300, 300);
setLayout(null);
setVisible(true);
}

// KeyListener methods
public void keyPressed(KeyEvent e) {
[Link]("Key Pressed: " + [Link]());
}

public void keyReleased(KeyEvent e) {}


public void keyTyped(KeyEvent e) {}

// MouseListener methods
public void mouseClicked(MouseEvent e) {
[Link]("Mouse Clicked at: " + [Link]() + "," + [Link]());
}

public void mousePressed(MouseEvent e) {}


public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

public static void main(String[] args) {


new KeyMouseExample();
}
}
4. WAP to handle events on login and registration page.
import [Link].*;
import [Link].*;

class LoginForm extends Frame implements ActionListener {


Label l1, l2, msg;
TextField t1, t2;
Button login, register;

LoginForm() {
l1 = new Label("Username:");
l2 = new Label("Password:");

t1 = new TextField();
t2 = new TextField();
[Link]('*');

login = new Button("Login");


register = new Button("Register");

msg = new Label();

[Link](50, 100, 80, 30);


[Link](140, 100, 100, 30);

[Link](50, 150, 80, 30);


[Link](140, 150, 100, 30);

[Link](50, 200, 80, 30);


[Link](150, 200, 80, 30);

[Link](50, 250, 200, 30);

add(l1); add(t1);
add(l2); add(t2);
add(login); add(register);
add(msg);

[Link](this);
[Link](this);

setSize(300, 350);
setLayout(null);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


if ([Link]() == login) {
[Link]("Login Clicked");
} else if ([Link]() == register) {
[Link]("Register Clicked");
}
}

public static void main(String[] args) {


new LoginForm();
}
}

You might also like