CalculatorApplet

CalculatorApplet


CalculatorApplet





========================================================================



import java.applet.*;


import java.awt.*;


import java.awt.event.*;





public class CalculatorApplet extends Applet {


private TextField opKiriTextField;


private TextField opKananTextField;


private Choice OperatorChoice;


private Button samaDenganButton;


private Label hasilLabel;





private Panel panel;





private String listOperator[]={"x",":","+","-"};





public CalculatorApplet(){


panel=new Panel();


opKiriTextField=new TextField();


opKananTextField=new TextField();


OperatorChoice=new Choice();


samaDenganButton=new Button("=");


hasilLabel=new Label();





OperatorChoice.removeAll();


for(int i=0; i<listOperator.length; i++){


OperatorChoice.addItem(listOperator[i]);


}





this.add(panel);


panel.setSize(560,120);


panel.setLayout(null);





panel.add(opKiriTextField);


panel.add(opKananTextField);


panel.add(OperatorChoice);


panel.add(samaDenganButton);


panel.add(hasilLabel);





opKiriTextField.setBounds(30,30,70,30);


OperatorChoice.setBounds(130,30,70,30);


opKananTextField.setBounds(230,30,70,30);


samaDenganButton.setBounds(330,30,70,30);


hasilLabel.setBounds(430,30,100,30);





samaDenganButton.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent evt){


hitungButtonActionPerformed(evt);


}


});


}





private void hitungButtonActionPerformed(ActionEvent evt){


double opKiri=0, opKanan=0, hasil=0;





try{


opKiri=Double.parseDouble(opKiriTextField.getText());


}catch(Exception ex){}





try{


opKanan=Double.parseDouble(opKananTextField.getText());


}catch(Exception ex){}





switch(OperatorChoice.getSelectedIndex()){


case 0: hasil = opKiri * opKanan ; break;


case 1: hasil = opKiri / opKanan ; break;


case 2: hasil = opKiri + opKanan ; break;


case 3: hasil = opKiri - opKanan ; break;


}





hasilLabel.setText(Double.toString(hasil));


}


}







========================================================================


html calculator




========================================================================



<html>


<head>


<title>Calculator menggunakan Applet</title>


</head>


<body>


<applet> code="CalculatorApplet" width=560 height=120></applet>


</body>


</html>



Share this:

Enter your email address to get update from Kompi Ajaib.

Tidak ada komentar