HFTW Simplified Calculator Version 0.6.7 BETA

Last Updated on 14 years by Mas Herdi

Source Coud MatheGUI.java

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.Container;
import java.awt.CardLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.JLabel;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JOptionPane;

public class MatheGUI extends JFrame
{
    private static JTextField angka, hasil;
    private static JButton tambah;
    private static JButton kurang;
    private static JButton bagi;
    private static JButton kali;
    private static JLabel label1;
    static JPanel box;
    double a;
    double b;
    double c;
    double o;
    boolean h = false,j = false,k = false,i = false;
    String s = null; 

    public MatheGUI()
    {
        super("HFTW-SC");
    }

    public void addComponent(Container pane)
    {
        //setLayout(new FlowLayout());
        tambah = new JButton("+");
        kurang = new JButton("-");
        bagi = new JButton("/");
        kali = new JButton("*");
        tambah.addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent evt)
                {
                    tambahDiklik(evt);
                }
            });
        kurang.addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent evt)
                {
                    kurangDiklik(evt);
                }
            });
        bagi.addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent evt)
                {
                    bagiDiklik(evt);
                }
            });
        kali.addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent evt)
                {
                    kaliDiklik(evt);
                }
            });

        angka = new JTextField(10);
        angka.setHorizontalAlignment(JTextField.RIGHT);
        angka.addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent evt)
                {
                    angkaDiklik(evt);
                }
            });
        angka.setSize(75, 60);

        hasil = new JTextField(5);
        hasil.setEditable(false);
        hasil.setBackground(new java.awt.Color(200, 102, 102));
        hasil.setHorizontalAlignment(JTextField.RIGHT);

        label1 = new JLabel("HFTW SimplifiedCalculator");

        JPanel comboBox = new JPanel();
        comboBox.add(angka);
        comboBox.add(hasil);
        //JPanel iso = new JPanel();
        //iso.add(hasil);

        JPanel bawahan = new JPanel();
        bawahan.add(tambah);
        bawahan.add(kurang);
        bawahan.add(kali);
        bawahan.add(bagi);

        JPanel buottom = new JPanel();
        buottom.add(label1);

        //box = new JPanel(new CardLayout());
        //box.add(comboBox);
        //box.add(bawahan);
        pane.add(comboBox, BorderLayout.PAGE_START);
        pane.add(bawahan, BorderLayout.PAGE_END);
        pane.add(buottom, BorderLayout.CENTER);
        //pane.add(iso, BorderLayout.LINE_START);
    }

    private void kurangDiklik(MouseEvent evt)
    {
        a = Double.parseDouble(angka.getText().replaceFirst(",","."));
        angka.setText(null);
        hasil.setText(null);
        h = true;
        i = false;
        j = false;
        k = false;
    }

    private void tambahDiklik(MouseEvent evt)
    {
        a = Double.parseDouble(angka.getText().replaceFirst(",","."));
        angka.setText(null);
        hasil.setText(null);
        i = true;
        h = false;
        j = false;
        k = false;
    }

    private void bagiDiklik(MouseEvent evr)
    {
        a = Double.parseDouble(angka.getText().replaceFirst(",","."));
        angka.setText(null);
        hasil.setText(null);
        j = true;
        i = false;
        h = false;
        k = false;
    }

    private void kaliDiklik(MouseEvent evt)
    {
        a = Double.parseDouble(angka.getText().replaceFirst(",","."));
        angka.setText(null);
        hasil.setText(null);
        k = true;
        i = false;
        j = false;
        h = false;
    }

    private void angkaDiklik(MouseEvent evt)
    {
        try
        {
            final java.awt.event.KeyAdapter xs = new java.awt.event.KeyAdapter()
            {
                @Override
                public void keyTyped(java.awt.event.KeyEvent kevt)throws ArithmeticException
                {
                    if((kevt.getKeyChar() == java.awt.event.KeyEvent.VK_ENTER))
                    {
                        c = 0;
                        b = Double.parseDouble(angka.getText().replaceFirst(",","."));
                        if(h == true)
                        {
                            c = a - b;
                            a = c;
                            s = String.format("%,.2f",c);
                            hasil.setText(s);
                            //h = false;
                        }
                        else if( i == true)
                        {
                            c = a + b;
                            a = c;
                            s = String.format("%,.2f",c);
                            hasil.setText(s);
                            //i = false;
                        }
                        else if(j == true)
                        {
                            if( b == 0)
                            {
                                throw new ArithmeticException();
                            }
                            else if (b != 0)
                            {
                                c = a / b;
                                a = c;
                                //s = String.format("%,.2f %,.2f %,.2f",a,b,c);
                                s = String.format("%,.2f",c);
                                hasil.setText(s);
                            }
                            //j = false;
                        }
                        else if(k == true)
                        {
                            c = a * b;;
                            a = c;
                            s = String.format("%,.2f",c);
                            hasil.setText(s);
                            //k = false;
                        }
                    }
                }
            };
            angka.addKeyListener(xs);
        }catch(ArithmeticException ex) {
            label1.setText("Error:Pembagian dengan nol");
        }
    }

    public static void createGUI()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MatheGUI mg = new MatheGUI();
        mg.addComponent(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
        frame.setSize(250,130);
        frame.setResizable(false);
    }

    public static void main(String... args)
    {
        /*MatheGUI mg = new MatheGUI();
mg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mg.setSize(300, 200);
mg.setVisible(true);*/
        try
        {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        }
        catch(UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (NumberFormatException ex){

        } catch (Exception ex){

        }

        javax.swing.SwingUtilities.invokeLater(new Runnable(){

            public void run()
            {
                createGUI();
            }
        });
    }
}

Screenshot

Deskripsi Singkat :

  1. FUNGSIONALITAS
    • Empat buah tombol, masing-masing untuk operasi tambah, kurang, kali dan bagi
    • Satu bidang teks untuk menginputkan kata, dan satunya sebagai output hasil
    • Format angka dalam double
    • Tidak ada warning_exception yang dioutputkan
    • Tetapi beberapa exception diimplementasikan
    • Eksekusi perintah dengan menekan ENTER
    • Aliran Data : Input angka pertama > klik tombol operator > input angka kedua > ENTER > hasil keluar
    • Jika ENTER ditekan berkali-kali maka operasi sebelumnya akan dieksekusi dengan mengganti angka pertama dengan hasil operasi sebelumnya
    • Aliran data masih belum sempurna, masih belum bisa berpindah-pindah operator dengan bebas
    • Terkadang hasilnya akan mengagetkan 🙂
    • Masih terdapat bug
  2. LAYOUT
    • Menggunakan tampilan CardLayout siap pakai
    • Menggunakan FlowControl
    • Tidak Menggunakan netBeans
    • Ditulis menggunakan jEdit




Download aplikasi kami di Google Play Store


Tutorial Menarik Lainnya :

Leave a Reply

Your email address will not be published. Required fields are marked *

TWOH&Co.