/*ch1101.java*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ch1101 extends JApplet implements ActionListener
{   // Text fields for Number 1, Number 2,and Result
  private JTextField jtfNum1, jtfNum2,jtfResult;

    // Create the "Divide" button
  private JButton jbtDiv=new JButton("Divide");
 
  public ch1101()
  {   // Panel p1 to hold text fields and labels
    JPanel p1=new JPanel();
    p1.setLayout(new FlowLayout());
    p1.add(new JLabel("Number 1"));
    p1.add(jtfNum1=new JTextField(3));
    p1.add(new JLabel("Nubmer 2"));
    p1.add(jtfNum2=new JTextField(3));
    p1.add(new JLabel("Result"));
    p1.add(jtfResult=new JTextField(4));
    jtfResult.setEditable(false);
    jtfResult.setHorizontalAlignment(SwingConstants.RIGHT);
    getContentPane().add(p1,BorderLayout.CENTER);
    getContentPane().add(jbtDiv,BorderLayout.SOUTH);
  
      // Register listener
    jbtDiv.addActionListener(this);
  }
  
  /**Handle ActionEvent from the Divide button */
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource()==jbtDiv)
    {
      int num1=Integer.parseInt(jtfNum1.getText().trim());
      int num2=Integer.parseInt(jtfNum2.getText().trim());  
      try
      {
        int result = num1/num2;
          // Set result in jtfResult
        jtfResult.setText(String.valueOf(result));
      }
      catch(RuntimeException ex)
      {
        JOptionPane.showMessageDialog(this,ex.getMessage(),"Operation error",JOptionPane.ERROR_MESSAGE);
      }      
    }
  }
}
