Advertisement
akevintg

Java GUI Simple Calculator

Nov 29th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. package javagui;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. public class JavaGUI extends JFrame implements ActionListener{
  9.     String nilai1="0",nilai2="0",hasil="";
  10.     Font f=new Font("SansSerif",Font.BOLD, 50);
  11.     JTextField t=new JTextField();
  12.     JButton[] tombol=new JButton[10];
  13.     JButton b=new JButton("C");
  14.     JButton p=new JButton("+");
  15.     JButton m=new JButton("-");
  16.     JButton e=new JButton("=");
  17.     JButton b00=new JButton("00");
  18.     JButton d=new JButton("Del");
  19.     JPanel x= new JPanel(new GridLayout(4, 1, 1, 1));
  20.     JPanel y=new JPanel(new GridLayout(1,4,1,1));
  21.     JPanel y2=new JPanel(new GridLayout(1,4,1,1));
  22.     JPanel y3=new JPanel(new GridLayout(1,4,1,1));
  23.     JPanel x2= new JPanel(new GridLayout(1, 2, 1, 1));
  24.     JPanel x3= new JPanel(new GridLayout(1, 2, 1, 1));
  25.     JPanel xyz= new JPanel(new GridLayout(2, 1, 0, 25));
  26.     int flag=1;
  27.    
  28.     JavaGUI(){
  29.         fontadj();
  30.         layOut();
  31.         frame();
  32.         pack();
  33.     }
  34.    
  35.     void fontadj(){
  36.         t.setText("0");
  37.         t.setHorizontalAlignment(JTextField.RIGHT);
  38.         t.setFont(f);
  39.     }
  40.    
  41.     void frame(){
  42.         setLayout(new GridLayout(1,1,0,75));
  43.         setTitle("CALCULATOR JAVA");
  44.         setResizable(false);
  45.         setLocationRelativeTo(null);
  46.         setVisible(true);
  47.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  48.     }
  49.    
  50.     void layOut(){
  51.         for (int i = 0; i < tombol.length; i++) {
  52.             tombol[i]=new JButton(""+i);
  53.             tombol[i].addActionListener(this);
  54.         }
  55.         y.add(tombol[7]);
  56.         y.add(tombol[8]);
  57.         y.add(tombol[9]);
  58.         y.add(b);b.addActionListener(this);
  59.         x.add(y);
  60.         y2.add(tombol[4]);
  61.         y2.add(tombol[5]);
  62.         y2.add(tombol[6]);
  63.         y2.add(p);p.addActionListener(this);
  64.         x.add(y2);
  65.         y3.add(tombol[1]);
  66.         y3.add(tombol[2]);
  67.         y3.add(tombol[3]);
  68.         y3.add(m);m.addActionListener(this);
  69.         x.add(y3);
  70.         x3.add(tombol[0]);
  71.         x3.add(d);d.addActionListener(this);
  72.         x2.add(x3);
  73.         x2.add(e);e.addActionListener(this);
  74.         x.add(x2);
  75.         xyz.add(t);
  76.         xyz.add(x);
  77.         xyz.setBorder(BorderFactory.createEmptyBorder(10, 25, 10, 25));
  78.         add(xyz);
  79.     }
  80.    
  81.     void reset(){
  82.         nilai1="0";
  83.         nilai2="0";
  84.         flag=1;
  85.     }
  86.    
  87.     public void actionPerformed(ActionEvent e) {
  88.         t.setHorizontalAlignment(JTextField.RIGHT);
  89.         System.out.println(e.getActionCommand()+",Flag="+flag);
  90.         if(e.getActionCommand().equals("C")){
  91.             t.setText("0");
  92.             reset();
  93.         }
  94.         else if(e.getActionCommand().equals("Del")&&flag==1&&!nilai1.equals("")){
  95.             int temp;
  96.             System.out.println("masuk");
  97.             if(Integer.parseInt(nilai1)%10!=0)
  98.                 temp=(Integer.parseInt(nilai1)-Integer.parseInt(nilai1)%10)/10;
  99.             else
  100.                 temp=Integer.parseInt(nilai1)/10;
  101.             nilai1=""+temp;
  102.             if(nilai1.equals("0"))
  103.                 nilai1="";
  104.             t.setText(nilai1);
  105.         }
  106.         else if(e.getActionCommand().equals("Del")&&flag!=1&&!nilai2.equals("")){
  107.             int temp;
  108.             if(Integer.parseInt(nilai2)%10!=0)
  109.                 temp=(Integer.parseInt(nilai2)-Integer.parseInt(nilai2)%10)/10;
  110.             else
  111.                 temp=Integer.parseInt(nilai2)/10;
  112.             nilai2=""+temp;
  113.             if(nilai2.equals("0"))
  114.                 nilai2="";
  115.             t.setText(nilai2);
  116.         }
  117.         else if(e.getActionCommand().equals("+")){
  118.             flag=3;
  119.         }
  120.         else if(e.getActionCommand().equals("-")){
  121.             flag=4;
  122.            
  123.         }
  124.         else if(flag==1&&!e.getActionCommand().equals("=")&&!e.getActionCommand().equals("Del")){
  125.             System.out.println("Masuk samadengan data1");
  126.             if(nilai1.equals("0"))
  127.                 nilai1="";
  128.             nilai1+=e.getActionCommand();
  129.             t.setText(nilai1);
  130.         }
  131.         else if(e.getActionCommand().equals("=")&&flag==3){
  132.             int has;
  133.             has=Integer.parseInt(nilai1)+Integer.parseInt(nilai2);
  134.             hasil=""+has;
  135.             t.setText(hasil);
  136.             reset();
  137.         }
  138.         else if(e.getActionCommand().equals("=")&&!nilai1.equals("")){
  139.             int has;
  140.             System.out.println("masuk");
  141.             has=Integer.parseInt(nilai1)-Integer.parseInt(nilai2);
  142.             hasil=""+has;
  143.             t.setText(hasil);
  144.             reset();
  145.            
  146.         }
  147.         else if (flag!=1){
  148.             if(nilai2.charAt(0)=='0')
  149.                 nilai2="";
  150.             nilai2+=e.getActionCommand();
  151.             t.setText(nilai2);
  152.         }
  153.     }
  154.    
  155.     public static void main(String[] args) {
  156.         new JavaGUI();
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement