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

Develop A Program To Accept Two Numbers and Display Product of Two Numbers When User Pressed "Multiply" Button

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

Develop A Program To Accept Two Numbers and Display Product of Two Numbers When User Pressed "Multiply" Button

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

 Develop a program to accept two numbers and display

product of two numbers when user pressed "Multiply"


button

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MultiplyProgram extends JFrame implements ActionListener {
private JTextField numField1, numField2, resultField;
private JButton multiplyButton;
public MultiplyProgram() {
setTitle("Multiply Program");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
numField1 = new JTextField(10);
numField2 = new JTextField(10);
resultField = new JTextField(10);
resultField.setEditable(false);
multiplyButton = new JButton("Multiply");
multiplyButton.addActionListener(this);

JPanel panel = new JPanel();


panel.setLayout(new GridLayout(4, 2));
panel.add(new JLabel("Number 1:"));
panel.add(numField1);
panel.add(new JLabel("Number 2:"));
panel.add(numField2);
panel.add(new JLabel("Result:"));
panel.add(resultField);
panel.add(multiplyButton);

add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == multiplyButton) {
try {
double num1 = Double.parseDouble(numField1.getText());
double num2 = Double.parseDouble(numField2.getText());
double result = num1 * num2;
resultField.setText(String.valueOf(result));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter valid numbers.");
}
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> new MultiplyProgram());
}
}
OUTPUT

You might also like