Advertisement
LendraD

Kalkulator (dasar)

Jun 16th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4.  
  5. class Kalkulator implements ActionListener{
  6.    
  7.     //=====================================
  8.     private final byte NULL=9;
  9.     private final byte TAMBAH=0;
  10.     private final byte KURANG=1;
  11.     private final byte KALI=2;
  12.     private final byte BAGI=3;
  13.     //=====================================
  14.     private final JFrame mainF;
  15.     private JPanel io;
  16.     private JPanel inputP;
  17.     private JPanel operatorP;
  18.     private JButton[]angka;
  19.     private JButton[]operator;
  20.     private JTextField in;
  21.     private JTextField out;
  22.     //=====================================
  23.     private double inNum=0.0;
  24.     private double hasil=0.0;
  25.     private byte lastOperator=NULL;
  26.    
  27.     Kalkulator(){
  28.         setup();
  29.         mainF=new JFrame("Kalkulator");
  30.         mainF.add(inputP);
  31.         mainF.add(operatorP);
  32.         mainF.add(io);
  33.         mainF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.         mainF.setSize(500, 500);
  35.         mainF.setLayout(null);
  36.         mainF.setResizable(false);
  37.         mainF.setLocationByPlatform(true);
  38.         mainF.setVisible(true);
  39.     }
  40.    
  41.     private void setup(){
  42.         //Setup Panel
  43.         inputP=new JPanel();
  44.         inputP.setBounds(5,50,180,115);
  45.         inputP.setLayout(null);
  46.         inputP.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 3));
  47.         operatorP=new JPanel();
  48.         operatorP.setBounds(190,50,125,115);
  49.         operatorP.setLayout(null);
  50.         operatorP.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 3));
  51.         io=new JPanel();
  52.         io.setBounds(5,5,(190+120),40);
  53.         io.setLayout(null);
  54.         io.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 3));
  55.         /*Setup input*/{
  56.             String[]txt={
  57.                 " 1 "," 2 "," 3 "," 4 "," 5 ",
  58.                 " 6 "," 7 "," 8 "," 9 "," 0 "
  59.             };
  60.             angka=new JButton[10];
  61.             Rectangle pos=new Rectangle(10,10,50,20);
  62.             for(byte i=0;i<10;i++){
  63.                 angka[i]=new JButton(txt[i]);
  64.                 angka[i].setBounds(pos);
  65.                 if((i+1)%3==0){
  66.                     pos.y+=25;
  67.                     pos.x-=(55*3);
  68.                 }
  69.                 pos.x+=55;
  70.                 inputP.add(angka[i]);
  71.                 angka[i].addActionListener(this);
  72.             }
  73.         }
  74.         /*setup operator*/{
  75.             String[]txt={
  76.                 " + "," - "," * "," / ",
  77.                 " = ","bs"," C "
  78.             };
  79.             operator=new JButton[7];
  80.             Rectangle pos=new Rectangle(10,10,50,20);
  81.             for(byte i=0;i<4;i++){
  82.                 operator[i]=new JButton(txt[i]);
  83.                 operator[i].setBounds(pos);
  84.                 if((i+1)%2==0){
  85.                     pos.y+=25;
  86.                     pos.x-=(55*2);
  87.                 }
  88.                 if(i==6)pos.width+=100;
  89.                 pos.x+=55;
  90.                 operatorP.add(operator[i]);
  91.                 operator[i].addActionListener(this);
  92.             }
  93.             pos=new Rectangle(operator[2].getBounds());
  94.             pos.width=105;
  95.             pos.y+=25;
  96.             operator[4]=new JButton(txt[4]);
  97.             operator[4].setBounds(pos);
  98.             operator[4].addActionListener(this);
  99.             operatorP.add(operator[4]);
  100.             pos.y+=25;
  101.             pos.width=operator[0].getBounds().width;
  102.             for(byte i=5;i<7;i++){
  103.                 operator[i]=new JButton(txt[i]);
  104.                 operator[i].setBounds(pos);
  105.                 operatorP.add(operator[i]);
  106.                 operator[i].addActionListener(this);
  107.                 pos.x+=55;
  108.             }
  109.         }
  110.         /*setup field text*/{
  111.             Rectangle pos=new Rectangle(5,10,170,20);
  112.             in=new JTextField(100);
  113.             out=new JTextField(100);
  114.             in.setBounds(pos);
  115.             pos=new Rectangle(190,10,115,20);
  116.             out.setBounds(pos);
  117.             io.add(in);
  118.             io.add(out);
  119.             in.setForeground(Color.red);
  120.             //=======================================
  121.             // * Sementara
  122.             //in.setEnabled(false);
  123.             //out.setEnabled(false);
  124.         }
  125.     }
  126.    
  127.     @Override
  128.     public void actionPerformed(ActionEvent e){
  129.        if(in.getText().equals(""))in.setText("0");
  130.        String input_num=e.getActionCommand();
  131.        //==============================================
  132.        if(input_num.charAt(1)>='0'&&input_num.charAt(1)<='9'){
  133.            in.setText(in.getText()+input_num.charAt(1));
  134.        }
  135.        //==============================================
  136.        if(input_num.equals("bs"))
  137.            in.setText(in.getText().substring(0, in.getText().length()-1));
  138.        //==============================================
  139.        if(input_num.equals(" C ")){
  140.            in.setText("");
  141.            hasil=0;
  142.            lastOperator=NULL;
  143.        }
  144.        //==============================================
  145.        if(input_num.charAt(1)=='+'){
  146.            if(lastOperator==NULL){
  147.                hasil=Double.parseDouble(in.getText());
  148.            }else{
  149.                inNum=Double.parseDouble(in.getText());
  150.                hitung();
  151.            }
  152.            lastOperator=TAMBAH;
  153.        }else
  154.        if(input_num.charAt(1)=='-'){
  155.            if(lastOperator==NULL){
  156.                hasil=Double.parseDouble(in.getText());
  157.            }else{
  158.                inNum=Double.parseDouble(in.getText());
  159.                hitung();
  160.            }
  161.            lastOperator=KURANG;
  162.        }else
  163.        if(input_num.charAt(1)=='*'){
  164.            if(lastOperator==NULL){
  165.                hasil=Double.parseDouble(in.getText());
  166.            }else{
  167.                inNum=Double.parseDouble(in.getText());
  168.                hitung();
  169.            }
  170.            lastOperator=KALI;
  171.        }else
  172.        if(input_num.charAt(1)=='/'){
  173.            if(lastOperator==NULL){
  174.                hasil=Double.parseDouble(in.getText());
  175.            }else{
  176.                inNum=Double.parseDouble(in.getText());
  177.                hitung();
  178.            }
  179.            lastOperator=BAGI;
  180.        }else
  181.        if(input_num.charAt(1)=='='){
  182.            if(lastOperator==NULL)
  183.                hasil=Double.parseDouble(in.getText());
  184.            else
  185.                inNum=Double.parseDouble(in.getText());
  186.            //-----------------------------------------
  187.            hitung();
  188.            Double h=hasil;
  189.            //-----------------------------------------
  190.            out.setText(h.toString());
  191.            hasil=inNum=0;
  192.            in.setText("");
  193.            lastOperator=NULL;
  194.        }
  195.     }
  196.    
  197.     private void hitung(){
  198.        switch(lastOperator){
  199.            case TAMBAH:
  200.                hasil+=inNum;
  201.                break;
  202.            case KURANG:
  203.                hasil-=inNum;
  204.                break;
  205.            case BAGI:
  206.                hasil/=inNum;
  207.                break;
  208.            case KALI:
  209.                hasil*=inNum;
  210.                break;
  211.        }
  212.     }
  213.    
  214.     public static void main(String[]args){
  215.         Kalkulator k=new Kalkulator();
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement