public class CalculatorApplet extends JApplet
1: import javax.swing.*;
2: import java.awt.*;
3: import java.awt.event.*;
4:
5: public class CalculatorApplet extends JApplet
6: implements ActionListener
7: {
8: int num1 = 0;
9: int num2 = 0;
10: int result = 0;
11: String input;
12:
13: public void init()
14: {
15: Container contentPane = getContentPane();
16: contentPane.setBackground(Color.YELLOW);
17:
18: contentPane.setLayout(new FlowLayout());
19:
20: JButton plusButton = new JButton("Add");
21: contentPane.add(plusButton);
22: plusButton.addActionListener(this);
23:
24: JButton subtractButton = new JButton("Subtract");
25: contentPane.add(subtractButton);
26: subtractButton.addActionListener(this);
27: }
28:
29: public void actionPerformed(ActionEvent e)
30: {
31: if(e.getActionCommand().equals("Add"))
32: {
33: input = JOptionPane.showInputDialog("Enter x: ");
34: num1 = Integer.parseInt(input);
35:
36: input = JOptionPane.showInputDialog("Enter y: ");
37: num2 = Integer.parseInt(input);
38:
39: result = num1 + num2;
40:
41: JOptionPane.showMessageDialog(null,
42: "The result of " + num1 + "+" + num2 + " is: " + result);
43: }
44: else if(e.getActionCommand().equals("Subtract"))
45: {
46: input = JOptionPane.showInputDialog("Enter x: ");
47: num1 = Integer.parseInt(input);
48:
49: input = JOptionPane.showInputDialog("Enter y: ");
50: num2 = Integer.parseInt(input);
51:
52: result = num1 - num2;
53:
54: JOptionPane.showMessageDialog(null,
55: "The result of " + num1 + "-" + num2 + " is: " + result);
56: }
57:
58: }
59: }