0% found this document useful (0 votes)
4 views3 pages

Ajp 10

The document contains Java code examples demonstrating the use of key events and action listeners in GUI applications. The first two examples show how to capture key presses and display corresponding messages, while the third example illustrates a simple multiplication application using text fields and a button. Each example creates a window that responds to user input and updates the display accordingly.

Uploaded by

pranavdhumal61
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)
4 views3 pages

Ajp 10

The document contains Java code examples demonstrating the use of key events and action listeners in GUI applications. The first two examples show how to capture key presses and display corresponding messages, while the third example illustrates a simple multiplication application using text fields and a button. Each example creates a window that responds to user input and updates the display accordingly.

Uploaded by

pranavdhumal61
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
You are on page 1/ 3

Practical 10

X.
1.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class keyevent extends JFrame O/P-


implements KeyListener {
Label label;
keyevent() {
label = new Label("Waiting for key
press...");
add(label,
BorderLayout.CENTER);
addKeyListener(this);
setSize(300, 200);
setVisible(true);
}

public void keyPressed(KeyEvent e) {


label.setText("Key pressed");
}

public void keyReleased(KeyEvent e) {


}

public void keyTyped(KeyEvent e) {


}

public static void main(String args[]) {


new keyevent();
}
}
2.
import java.awt.*;
import java.awt.event.*;
public class keyevent2 extends Frame implements KeyListener {
Label label;
keyevent2(){
label = new Label("Waiting for key press...");
add(label);
addKeyListener(this);
setSize(300, 200);
setVisible(true);
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_F1:
label.setText("F1 key pressed");
break;
case KeyEvent.VK_F2:
label.setText("F2 key pressed");
break;
case KeyEvent.VK_F3:
label.setText("F3 key pressed");
break;
case KeyEvent.VK_F4:
label.setText("F4 key pressed");
break;
case KeyEvent.VK_F5: O/P-
label.setText("F5 key pressed");
break;
case KeyEvent.VK_UP:
label.setText("Up arrow key pressed");
break;
case KeyEvent.VK_DOWN:
label.setText("Down arrow key
pressed");
break;
case KeyEvent.VK_LEFT:
label.setText("Left arrow key pressed");
break;
case KeyEvent.VK_RIGHT:
label.setText("Right arrow key pressed");
break;
default:
label.setText("Key pressed: " + keyCode);
}}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
public static void main(String args[]) {
new keyevent2();
}}
XIII.

3.
import java.awt.*;
import java.awt.event.*;

class mul extends Frame implements


ActionListener { O/P-
TextField num1, num2, result;
Button multiplyButton;

mul() {
num1 = new TextField(10);
num2 = new TextField(10);
result = new TextField(10);
multiplyButton = new
Button("Multiply");
multiplyButton.addActionListener(this);

add(num1);
add(num2);
add(multiplyButton);
add(result);

setLayout(new FlowLayout());
setSize(300, 100);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
int product = n1 * n2;
result.setText(String.valueOf(product));
}

public static void main(String args[]) {


new mul();
}
}

You might also like