Get paid to share your links!

2011

Program Kalkulator

Calculator.java

Lama gk posting sahabat blogger.
Sesuai tema blog ini, Berbagi lebih baik saya mau memberikan source code untuk kalkulator dari java.
semoga bermanfaat.





import java.math.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;


public class Calculator implements MouseListener,  ActionListener{

    private JFrame myFrame;
    private JTextField text;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;
    private JButton button9;
    private JButton button0;
    private JButton buttonBac;
    private JButton buttonC;
    private JButton buttonBagi;
    private JButton buttonSQR;
    private JButton buttonKali;
    private JButton buttonPersen;
    private JButton buttonKurang;
    private JButton buttonSatuBagi;
    private JButton buttonBalik;
    private JButton buttonTitik;
    private JButton buttonTambah;
    private JButton buttonQ;
   
    private JMenuItem reset;
    private JMenuItem exit;
   
    private Float temp1;
    private Float temp2;
    private String opr;
    private String temp;
   
    /* Konstruktor */
    /**
     * Inisialisasi objek Calculator dengan class JFrame dengan membuat judul aplikasi dengan "My calculator"
     */

    public Calculator(){
        myFrame = new JFrame("My calculator");        //membuat judul frame
       
        myFrame.setVisible(true);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //menutup aplikasi semua aplikasi yang berkaian seperti cmd
       
        //membuat judul setiap buutton
        button1 = new JButton("1");
        button2 = new JButton("2");
        button3 = new JButton("3");
        button4 = new JButton("4");
        button5 = new JButton("5");
        button6 = new JButton("6");
        button7 = new JButton("7");
        button8 = new JButton("8");
        button9 = new JButton("9");
        button0 = new JButton("0");
       
        buttonBac = new JButton("Backspace");
        buttonC = new JButton("C");
        buttonBagi = new JButton("/");
        buttonSQR = new JButton("sqr");
        buttonKali = new JButton("x");
        buttonPersen = new JButton("%");
        buttonKurang = new JButton("-");
        buttonSatuBagi = new JButton("1/x");
        buttonBalik = new JButton("+/-");
        buttonTitik = new JButton(".");
        buttonTambah = new JButton("+");
        buttonQ = new JButton("=");
       
       
    }
   
    //setter
    /**
     * Mengubah isi text
     * @param newText nilai baru untuk text
     */
    public void setText(String newText){
        text.setText(newText);
    }
   
    //getter
    /**
     * Mengambil Text
     * @return text Text
     */
    public String getText(){
        return text.getText();
    }
   
    /**
    * Mengatur layout
    * @param layout yang digunakan layout dari class BorderLayout
    */

    public void setLayout(){
        myFrame.setLayout(new BorderLayout());
    }
    /**
    * Menambah Component
    * @param penambahan component berupa menu bar, Panel Up dan Panel Bottom
    */
    public void addComponent(){
        myFrame.setJMenuBar(makeMenu());
        myFrame.add(makePanelUp(), BorderLayout.NORTH);
        myFrame.add(makePanelBottom(), BorderLayout.CENTER);
    }
   
    /**
    * Mengatur Pack
    * @param pengaturan Pack sehingga layout sesuai dengan banyak nya objek
    */
    public void setPack(){
        myFrame.pack();
        myFrame.setLocationRelativeTo(null);    //memindahkan aplikasi ke tengah layar
    }
   
    /**
    * Membuat menu bar
    * @return mb JMenuBar
    */
    public JMenuBar makeMenu(){
        JMenuBar mb = new JMenuBar();
        JMenu file = new JMenu("File");
        mb.add(file);
        reset = new JMenuItem("Reset");
        exit = new JMenuItem("Exit");
       
        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;
    }
   
    /**
    * membuat panel UP
    * @return panel merupkan penambahan objeck text di atas aplikasi
    */
   
    public JPanel makePanelUp(){
        JPanel panel = new JPanel();
        text = new JTextField(20);
        text.setText("");
        panel.add(text);
   
        return panel;
    }
   
    /**
    * membuat panel Bottom
    * @return panel dari penambahan beberapa bottom yang di perlukan
    */
    public JPanel makePanelBottom(){
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
       
        GridBagConstraints gb = new GridBagConstraints();
       
        panel.setLayout(new GridBagLayout());
        gb.gridwidth = 3;
        gb.gridheight = 1;
        gb.gridx = 0;
        gb.gridy = 0;
        gb.fill = GridBagConstraints.BOTH;
        panel.add(buttonBac, gb);
        gb.gridx = 3;
        panel.add(buttonC, gb);
       
       
        gb.gridwidth = 1;
        gb.gridheight = 1;
       
        gb.gridy++;
        gb.gridx = 0;
        gb.gridwidth = 1;
        panel.add(button7, gb);
        gb.gridx ++;
        panel.add(button8, gb);
        gb.gridx ++;
        panel.add(button9, gb);
        gb.gridx ++;
        panel.add(buttonBagi, gb);
        gb.gridx ++;
        panel.add(buttonSQR, gb);
       
        gb.gridy++;
        gb.gridx = 0;
        gb.gridwidth = 1;
        panel.add(button4, gb);
        gb.gridx ++;
        panel.add(button5, gb);
        gb.gridx ++;
        panel.add(button6, gb);
        gb.gridx ++;
        panel.add(buttonKali, gb);
        gb.gridx ++;
        panel.add(buttonPersen, gb);
       
       
        gb.gridy++;
        gb.gridx = 0;
        gb.gridwidth = 1;
        panel.add(button1, gb);
        gb.gridx ++;
        panel.add(button2, gb);
        gb.gridx ++;
        panel.add(button3, gb);
        gb.gridx ++;
        panel.add(buttonKurang, gb);
        gb.gridx ++;
        panel.add(buttonSatuBagi, gb);
       
        gb.gridy++;
        gb.gridx = 0;
        gb.gridwidth = 1;
        panel.add(button0, gb);
        gb.gridx ++;
        panel.add(buttonBalik, gb);
        gb.gridx ++;
        panel.add(buttonTitik, gb);
        gb.gridx ++;
        panel.add(buttonTambah, gb);
        gb.gridx ++;
        panel.add(buttonQ, gb);
       
       
        return panel;
   
    //menset listener kepada button serta
    }
    public void addListener(){
        button1.addMouseListener(this); //Langkah III
        button2.addMouseListener(this);
        button3.addMouseListener(this);
        button4.addMouseListener(this);
        button5.addMouseListener(this);
        button6.addMouseListener(this);
        button7.addMouseListener(this);
        button8.addMouseListener(this);
        button9.addMouseListener(this);
        button0.addMouseListener(this);
       
        //kusus untuk reset dan exit ditambahkan action listener
        reset.addActionListener(this);
        exit.addActionListener(this);
       
        buttonBac.addMouseListener(this);
        buttonC.addMouseListener(this);
        buttonBagi.addMouseListener(this);
        buttonSQR.addMouseListener(this);
        buttonKali.addMouseListener(this);
        buttonPersen.addMouseListener(this);
        buttonKurang.addMouseListener(this);
        buttonSatuBagi.addMouseListener(this);
        buttonBalik.addMouseListener(this);
        buttonTitik.addMouseListener(this);
        buttonTambah.addMouseListener(this);
        buttonQ.addMouseListener(this);
    }
   
    //kusus untuk mengisi action listener pada menu bar
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == exit){
            myFrame.dispose();            //menutup aplikasi
        }if (e.getSource() == reset){
            text.setText("0");            //menclear text dan mengisi nya dengan 0
        }
    }

    //kusus untuk mengisi mouse listener  pada tombol yang ada
    public void mouseClicked(MouseEvent e){    //Langkah II
       
        if (text.getText().equals("0") ){
            text.setText("");                    //menghapus angga 0 apabila text hanya barisi 0
        }
       
        if (e.getSource() == button1)
            text.setText(text.getText()+"1");        //menambah angka 1
        else if (e.getSource() == button2)
            text.setText(text.getText()+"2");        //menambah angka 2
        else if (e.getSource() == button3)
            text.setText(text.getText()+"3");        //menambah angka 3
        else if (e.getSource() == button4)
            text.setText(text.getText()+"4");        //menambah angka 4
        else if (e.getSource() == button5)
            text.setText(text.getText()+"5");        //menambah angka 5
        else if (e.getSource() == button6)
            text.setText(text.getText()+"6");        //menambah angka 6
        else if (e.getSource() == button7)
            text.setText(text.getText()+"7");        //menambah angka 7
        else if (e.getSource() == button8)
            text.setText(text.getText()+"8");         //menambah angka 8
        else if (e.getSource() == button9)
            text.setText(text.getText()+"9");        //menambah angka 9
        else if (e.getSource() == button0)
            text.setText(getText() + "0");            //menambah angka 0
        else if (e.getSource() == buttonC)
            text.setText("0");                        //menghapus dan menset nilai menjadi 0
       
        else if (e.getSource() == buttonBac){            //menghapus satu carakter dari kanan
            if (text.getText().equals("")== false) {
                text.setText(text.getText().substring(0,(text.getText().length())-1));
            }
        }
       
       
        else if (e.getSource() == buttonBagi){            // menyimpan nilai text dan menset operasi pembagian

            temp1 = Float.valueOf(text.getText());
            opr = "/";
            text.setText("0");
        }
       
        else if (e.getSource() == buttonSQR){            // menyimpan nilai text dan menset operasi sqr

            if (text.getText().equals("")==false){           
                text.setText(String.valueOf( Math.sqrt(Double.valueOf(text.getText()))  ) );
            }
        }
       
        else if (e.getSource() == buttonKali){            // menyimpan nilai text dan menset operasi perkalian

            temp1 = Float.valueOf(text.getText());
            opr = "x";
            text.setText("0");
        }
       
        else if (e.getSource() == buttonPersen){            // melakukan  operasi persen  terhadap text

            temp1 = Float.valueOf(text.getText());
            temp1 = (temp1 * 100)/100;
            text.setText(String.valueOf(temp1) );
        }
       
        else if (e.getSource() == buttonKurang){            // menyimpan nilai text dan menset operasi pengurangan

            temp1 = Float.valueOf(text.getText());
            opr = "-";
            text.setText("0");
        }
       
        else if (e.getSource() == buttonSatuBagi){            // melakukan operasi seper text

            temp1 = Float.valueOf(text.getText());
            temp1 = 1/temp1;
            text.setText(String.valueOf(temp1) );
        }
       
        else if (e.getSource() == buttonBalik){            // melakukan operasi perkalian dengan (-1) atau operasi membalikkan bilangan dari positif (+) menjadi negatif (-) dan sebaliknya

            temp1 = Float.valueOf(text.getText());
            temp1 = temp1*(-1);
            text.setText(String.valueOf(temp1) );
        }
        else if (e.getSource() == buttonTitik){            // melakukan penambahan bilangan decimal
            temp = text.getText();
            if (temp.contains(".")== false) {
                if (text.getText().equals("")== false){
                    text.setText(text.getText()+".");
                }else {
                    text.setText(0+".");
                }
            }
        }
        else if (e.getSource() == buttonTambah){            //// menyimpan nilai text dan menset operasi pengurangan

            temp1 = Float.valueOf(text.getText());
            opr = "+";
            text.setText("0");
        }
       
       
       
        else if (e.getSource() == buttonQ){        //Mengeluarkan nilai hasil dari operasi pembagian, perkalian, pengurangan, serta penambahan
            temp2 = Float.valueOf(text.getText());
            if (opr.equals("/"))
                temp1 = temp1 / temp2;            //melakukan pembagian terhadap text yang telah disimpan sebelum nya dengan text yang ada
            else if (opr.equals("x"))
                temp1 = temp1 * temp2;    //melakukan perkalian terhadap text yang telah disimpan sebelum nya dengan text yang ada
            else if (opr.equals("-"))
                temp1 = temp1 - temp2;    //melakukan pengurangan terhadap text yang telah disimpan sebelum nya dengan text yang ada
            else if (opr.equals("+"))
                temp1 = temp1 + temp2; //melakukan penambahan terhadap text yang telah disimpan sebelum nya dengan text yang ada
           
            text.setText(String.valueOf(temp1) ); //menampilkan hasil operasi ke text
        }
    }
   
    // megoverride semua method yang ada pada class mouse listener
    public void mousePressed(MouseEvent e) {           
       
    }//and override three other method
    //continue to next slide
    public void mouseReleased(MouseEvent e) {  
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
   
    //method main yang akan dijalankan
    public static void main(String[] args){
        Calculator cal = new Calculator();        //membuat objek cl dari constructur Calculator
        cal.setLayout();                            //mengatur bentuk layout nya
        cal.addComponent();                        //menambah komponent pada calculator
        cal.setPack();                            //mengatur besar tampilan dalam hal ini disesuaikan dengan component yang digunakan
        cal.addListener();                        //menambah listener kepada beberapa component yang ada pada calculator
    }
   
   
   
}

Newer Posts