Вы находитесь на странице: 1из 10

import java.awt.

*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JApplet;
import java.awt.event.MouseListener;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;

/*<applet code="CalculatorApplet" width=600 height=500> </applet>*/

public class CalculatorApplet extends JApplet implements MouseListener,ActionListener


{
//Atribut
private JTextField myText;
private JButton myButton1;
private JButton myButton2;
private JButton myButton3;
private JButton myButton4;
private JButton myButton5;
private JButton myButton6;
private JButton myButton7;
private JButton myButton8;
private JButton myButton9;
private JButton myButton10;
private JButton myButton11;
private JButton myButton12;
private JButton myButton13;
private JButton myButton14;
private JButton myButton15;
private JButton myButton16;
private JButton myButton17;
private JButton myButton18;
private JButton myButton19;
private JButton myButton20;
private JButton myButton21;
private JButton myButton22;
private String temp1;
private String temp2;
private Float objOne;
private Float objTwo;
private Float hasil;
private Double hasil2;
private String operator;
private char ch;
private JMenuItem reset;
private JMenuItem exit;

//Konstraktor tanpa parameter


public CalculatorApplet() {
myButton1=new JButton("BackSpace");
myButton2=new JButton("C");
myButton3=new JButton("7");
myButton4=new JButton("8");
myButton5=new JButton("9");
myButton6=new JButton("/");
myButton7=new JButton("sqrt");
myButton8=new JButton("4");
myButton9=new JButton("5");
myButton10=new JButton("6");
myButton11=new JButton("x");
myButton12=new JButton("%");
myButton13=new JButton("1");
myButton14=new JButton("2");
myButton15=new JButton("3");
myButton16=new JButton("-");
myButton17=new JButton("1/x");
myButton18=new JButton("0");
myButton19=new JButton("+/-");
myButton20=new JButton(".");
myButton21=new JButton("+");
myButton22=new JButton("=");
reset = new JMenuItem("Reset");
exit = new JMenuItem("Exit");
temp1="";
objOne=null;
objTwo=null;
hasil=null;
hasil2=null;
operator = null;
addListener();
}

private void addListener() {


myButton1.addMouseListener(this);
myButton2.addMouseListener(this);
myButton3.addMouseListener(this);
myButton4.addMouseListener(this);
myButton5.addMouseListener(this);
myButton6.addMouseListener(this);
myButton7.addMouseListener(this);
myButton8.addMouseListener(this);
myButton9.addMouseListener(this);
myButton10.addMouseListener(this);
myButton11.addMouseListener(this);
myButton12.addMouseListener(this);
myButton13.addMouseListener(this);
myButton14.addMouseListener(this);
myButton15.addMouseListener(this);
myButton16.addMouseListener(this);
myButton17.addMouseListener(this);
myButton18.addMouseListener(this);
myButton19.addMouseListener(this);
myButton20.addMouseListener(this);
myButton21.addMouseListener(this);
myButton22.addMouseListener(this);
reset.addActionListener(this);
exit.addActionListener(this);
}

//setter
private void setText(String newText) {
myText.setText(newText);
}

//getter
public String getText() {
return myText.getText();
}

public void setLayout() {


setLayout(new BorderLayout());
}

public void addComponent() {


setJMenuBar(makeMenu());
add(makePanelUp(), BorderLayout.NORTH);
add(makePanelBottom(), BorderLayout.CENTER);
}

public void mouseClicked(MouseEvent e) {


int ind=-1;
int len;

if (myText.getText().equals("0")) {
myText.setText("");
}
if (e.getSource() == myButton1) {
temp1=myText.getText();
len=temp1.length();
if(len>0) {
temp2=temp1.substring(0,len-1);
objOne=null;
hasil=null;
objTwo=null;
myText.setText(temp2);
temp1=temp2;
temp2=null;
if(temp1.equals("")) {
myText.setText("0");
}
}
} else if(e.getSource() == myButton2) {
temp1="";
objOne=null;
hasil=null;
objTwo=null;
myText.setText("0");
} else if (e.getSource() == myButton3) {
temp1=temp1 + "7";
myText.setText(temp1);
} else if(e.getSource() == myButton4) {
temp1=temp1 + "8";
myText.setText(temp1);
} else if(e.getSource() == myButton5) {
temp1=temp1 + "9";
myText.setText(temp1);
} else if(e.getSource() == myButton6) {
objOne=Float.parseFloat(temp1);
temp1="";
operator ="/";
myText.setText("0");
} else if(e.getSource() == myButton7) {
hasil2=Double.parseDouble(temp1);
hasil2=Math.sqrt(hasil2);
temp1=hasil2.toString();
myText.setText(temp1);
} else if(e.getSource() == myButton8) {
temp1=temp1 + "4";
myText.setText(temp1);
} else if(e.getSource() == myButton9) {
temp1=temp1 + "5";
myText.setText(temp1);
} else if(e.getSource() == myButton10) {
temp1=temp1 + "6";
myText.setText(temp1);
} else if(e.getSource() == myButton11) {
objOne=Float.parseFloat(temp1);
temp1="";
operator ="x";
myText.setText("0");
} else if(e.getSource() == myButton12) {
objOne=Float.parseFloat(temp1)/100;
temp1=objOne.toString();
myText.setText(temp1);
} else if(e.getSource() == myButton13) {
temp1=temp1 + "1";
myText.setText(temp1);
} else if(e.getSource() == myButton14) {
temp1=temp1 + "2";
myText.setText(temp1);
} else if(e.getSource() == myButton15) {
temp1=temp1 + "3";
myText.setText(temp1);
} else if(e.getSource() == myButton16) {
objOne=Float.parseFloat(temp1);
temp1="";
operator ="-";
myText.setText("0");
} else if(e.getSource() == myButton17) {
objOne=Float.parseFloat(temp1);
objOne=1 / objOne;
temp1=objOne.toString();
myText.setText(temp1);
} else if(e.getSource() == myButton18) {
temp1 = temp1 + "0";
myText.setText(temp1);
} else if(e.getSource() == myButton19) {
temp2=temp1.substring(0,1);
if(temp2.equals("-")) {
temp2=temp1.substring(1);
temp1=temp2;
} else {
temp1="-" + temp1;
}
myText.setText(temp1);
} else if(e.getSource() == myButton20) {
temp1=myText.getText();
ind=temp1.lastIndexOf(".");
if(ind==-1) {
temp1=temp1 + ".";
}
myText.setText(temp1);
} else if(e.getSource() == myButton21) {
objOne=Float.parseFloat(temp1);
temp1="";
operator ="+";

myText.setText("0");
} else if(e.getSource() == myButton22) {
objTwo=Float.parseFloat(temp1);
if(operator.equals("/")) {
hasil=objOne / objTwo;
} else if(operator.equals("x")) {
hasil=objOne * objTwo;
} else if(operator.equals("+")) {
hasil=objOne + objTwo;
} else if(operator.equals("-")) {
hasil=objOne - objTwo;
}
temp1=hasil.toString();
myText.setText(temp1);
}
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }

public JMenuBar makeMenu() {


JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
mb.add(file);
file.add(reset);
file.addSeparator();
file.add(exit);
JMenu view = new JMenu("View");
JMenuItem standard = new JMenuItem("Standard");
JMenuItem scientific = new JMenuItem("Scientific");
view.add(standard);
view.add(scientific);
mb.add(view);
return mb;
}
public JPanel makePanelUp() {
JPanel panel = new JPanel();
myText = new JTextField("0",21);
myText.setHorizontalAlignment(JTextField.RIGHT);
panel.add(myText);
return panel;
}

public void actionPerformed(ActionEvent e) {


if(e.getSource()==reset) {
temp1="";
objOne=null;
hasil=null;
objTwo=null;
myText.setText("");
} else if(e.getSource()==exit) {
System.exit(0);
}
}

public JPanel makePanelBottom() {


JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gb = new GridBagConstraints();
gb.fill=GridBagConstraints.HORIZONTAL;
panel.setLayout(new GridBagLayout());

gb.gridwidth = 3;
gb.gridheight = 1;
gb.gridx = 0;
gb.gridy = 0;
gb.fill = GridBagConstraints.BOTH;
panel.add(myButton1, gb);

gb.gridx = 3;
gb.gridy = 0;
gb.gridwidth = 2;
panel.add(myButton2, gb);

gb.gridx =0;
gb.gridy=1;
gb.gridwidth = 1;
panel.add(myButton3, gb);
gb.gridx = 1;
gb.gridy=1;
panel.add(myButton4, gb);

gb.gridx = 2;
gb.gridy=1;
panel.add(myButton5, gb);

gb.gridx = 3;
gb.gridy=1;
panel.add(myButton6, gb);

gb.gridx = 4;
gb.gridy=1;
panel.add(myButton7, gb);

gb.gridwidth = 1;
gb.gridx = 0;
gb.gridy=2;
panel.add(myButton8, gb);

gb.gridx = 1;
gb.gridy=2;
panel.add(myButton9, gb);

gb.gridx=2;
gb.gridy=2;
gb.gridwidth = 1;
panel.add(myButton10, gb);

gb.gridx = 3;
gb.gridy=2;
panel.add(myButton11, gb);

gb.gridx = 4;
gb.gridy=2;
panel.add(myButton12, gb);

gb.gridx = 0;
gb.gridy=3;
gb.gridwidth = 1;
panel.add(myButton13, gb);

gb.gridx = 1;
gb.gridy=3;
panel.add(myButton14, gb);
gb.gridx=2;
gb.gridy=3;
gb.gridwidth = 1;
panel.add(myButton15, gb);

gb.gridx = 3;
gb.gridy=3;
panel.add(myButton16, gb);

gb.gridx = 4;
gb.gridy=3;
panel.add(myButton17, gb);

gb.gridx = 0;
gb.gridy=4;
gb.gridwidth = 1;
panel.add(myButton18, gb);

gb.gridx = 1;
gb.gridy=4;
panel.add(myButton19, gb);

gb.gridx=2;
gb.gridy=4;
gb.gridwidth = 1;
panel.add(myButton20, gb);

gb.gridx = 3;
gb.gridy=4;
panel.add(myButton21, gb);

gb.gridx = 4;
gb.gridy=4;
panel.add(myButton22, gb);

return panel;
}

public void init(){


setLayout();
addComponent();
}
}
Maka hasilnya sebagai berikut :

Selamat mencoba…