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

Ajp PR10

Uploaded by

Aditya Moholkar
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)
24 views3 pages

Ajp PR10

Uploaded by

Aditya Moholkar
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

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class AJP_PR10 extends Applet implements KeyListener


{
TextField t; TextArea ta; public void init()
{
t=new TextField(20); ta=new TextArea(); add(t);
add(ta);
t.addKeyListener(this);
//add listner to my text field component
}
public void keyPressed(KeyEvent ke)
{
if(ke.getSource()==t) ta.append("key pressed");
}
public void keyReleased(KeyEvent ke)
{
if(ke.getSource()==t) ta.append("Key Released");
}
public void keyTyped(KeyEvent ke)
{
if(ke.getSource()==t)
{
char c=ke.getKeyChar(); ta.append("KeyTyped"+c);
}
}
}
/* <applet code="AJP_PR10.class" height="400" width="400"> </applet> */

OUTPUT=
PRACTICAL 10 EXERCISE

Q.3

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MultiplyNumbers {

public static void main(String[] args) {


// Create a new frame (window)
JFrame frame = new JFrame("Multiply Two Numbers");

// Create text fields for input


JTextField num1Field = new JTextField(10);
JTextField num2Field = new JTextField(10);

// Create a label to display the result


JLabel resultLabel = new JLabel("Result: ");

// Create a button to trigger multiplication


JButton multiplyButton = new JButton("Multiply");

// Add an action listener to the button


multiplyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get numbers from text fields
try {
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());

// Calculate product
double product = num1 * num2;

// Display the result


resultLabel.setText("Result: " + product);
} catch (NumberFormatException ex) {
// Handle invalid input
JOptionPane.showMessageDialog(frame, "Please enter valid numbers", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
});

// Create a panel to organize components


PRACTICAL 10
JPanel panel = new JPanel();

// Add components to the panel


panel.add(new JLabel("Number 1:"));
panel.add(num1Field);
panel.add(new JLabel("Number 2:"));
panel.add(num2Field);
panel.add(multiplyButton);
panel.add(resultLabel);

// Add the panel to the frame


frame.add(panel);

// Set up the frame


frame.setSize(300, 150); // Set the frame size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exit when the window is
closed
frame.setLocationRelativeTo(null); // Center the window
frame.setVisible(true); // Make the frame visible
}
}

OUTPUT=

You might also like