// import javax.swing.*; import java.awt.event.*; import java.awt.*; public class CalcApplet extends JApplet implements ActionListener { private JTextField text1 = new JTextField(10); private JTextField text2 = new JTextField(10); private JButton plusButton = new JButton("+"); private JButton minusButton = new JButton("-"); private JButton timesButton = new JButton("*"); private JButton divideButton = new JButton("/"); private JTextField svarstext = new JTextField(10); public void actionPerformed(ActionEvent event) { String s1 = text1.getText(); String s2 = text2.getText(); try { double num1 = Double.parseDouble(s1); double num2 = Double.parseDouble(s2); String op = ((JButton)event.getSource()).getText(); double result = 0; switch (op.charAt(0)) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; } svarstext.setText(result + ""); } catch (NumberFormatException exc) { svarstext.setText("ERROR"); } } // actionPerformed public void init() { plusButton.addActionListener(this); minusButton.addActionListener(this); timesButton.addActionListener(this); divideButton.addActionListener(this); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(text1); cp.add(text2); cp.add(plusButton); cp.add(minusButton); cp.add(timesButton); cp.add(divideButton); cp.add(svarstext); } public static void main(String[] args) { CalcApplet applet = new CalcApplet(); JFrame frame = new JFrame("CalcApplet"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(applet); frame.setSize(250, 150); applet.init(); applet.start(); frame.setVisible(true); } } // class CalcApplet