Thursday, April 12, 2007

Jan 2005 - Part B - Q3

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

public class Divide extends JApplet implements ActionListener{

private JButton aJButton;
private JTextField aText,bText,cText;
private JLabel aLabel,bLabel,cLabel;
private GridLayout aGridLayout;
private JPanel aJPanel;

private double number1,number2;

public void init(){

Container c = getContentPane();

aLabel = new JLabel("Masukkan No:");
bLabel = new JLabel("Masukkan Pembahagi:");
cLabel = new JLabel("Jawapan:");

aText = new JTextField();
bText = new JTextField();
cText = new JTextField();cText.setEditable(false);

aJPanel = new JPanel();
aGridLayout = new GridLayout(3,2);
aJPanel.setLayout(aGridLayout);

aJPanel.add(aLabel);
aJPanel.add(aText);
aJPanel.add(bLabel);
aJPanel.add(bText);
aJPanel.add(cLabel);
aJPanel.add(cText);
c.add(aJPanel,BorderLayout.NORTH);

aJButton = new JButton("HASIL BAHAGI");
aJButton.addActionListener(this);
c.add(aJButton,BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e){

number1=0;
number2=0;

try{

number1=Double.parseDouble(aText.getText());
number2=Double.parseDouble(bText.getText());

if(number2!=0){
cText.setText(""+number1/number2);
}else if(number2==0)
{
cText.setText("Can't divide by zero!");
}

}catch(NumberFormatException nfe){

cText.setFont(new Font("Arial",Font.PLAIN,10));
cText.setText("Please input numeric value at the above textfields.");

}
}

}

No comments:

Blog Archive