Kalkulator Sederhana Dengan Swing GUI Java

Diposting pada
tampilan kalkulator sederhana

nah anda pengen tahu sistem kerja sebuah kalkulator sederhana atau pun bingung bagaimana cara membuat program hitung sederhana pada pemrograman java. nah kali ini saya akan coba memberikan sedikit arahan bagaimana cara membuatnya.

– buka netbeans ada
– buat project baru ,kemudian pilih java application
– buat file baru kemudian jFrame Form
lihat script dibawah ini :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * kalkulatorANDHIKA.java
 *
 * Created on 04 Jul 11, 21:58:14
 */
package andhikaperc2;

/**
 *
 * @author putrapantura
 */
public class kalkulatorANDHIKA extends javax.swing.JFrame {

    /** Creates new form kalkulatorANDHIKA */
    public kalkulatorANDHIKA() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings(“unchecked”)
    // <editor-fold defaultstate=”collapsed” desc=”Generated Code”>                         
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        kolom1 = new javax.swing.JTextField();
        kolom2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        hasilhitung = new javax.swing.JTextField();
        penjumlahan = new javax.swing.JButton();
        pengurangan = new javax.swing.JButton();
        pembagian = new javax.swing.JButton();
        perkalian = new javax.swing.JButton();
        hitung = new javax.swing.JButton();
        hapus = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(“Program Hitung Sederhana”);

        jLabel1.setFont(new java.awt.Font(“Times New Roman”, 1, 12)); // NOI18N
        jLabel1.setText(“PROGRAM HITUNG SEDERHANA”);

        kolom1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        kolom2.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel2.setText(“HASIL =”);

        hasilhitung.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        penjumlahan.setText(“+”);
        penjumlahan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                penjumlahanActionPerformed(evt);
            }
        });

        pengurangan.setText(“-“);
        pengurangan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                penguranganActionPerformed(evt);
            }
        });

        pembagian.setText(“:”);
        pembagian.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pembagianActionPerformed(evt);
            }
        });

        perkalian.setText(“x”);
        perkalian.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                perkalianActionPerformed(evt);
            }
        });

        hitung.setText(“=”);
        hitung.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hitungActionPerformed(evt);
            }
        });

        hapus.setText(“DELETE ALL”);
        hapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hapusActionPerformed(evt);
            }
        });

        jButton1.setText(“OFF”);
    &n
bsp;   jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(hapus, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(hasilhitung))
                            .addComponent(kolom1, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addComponent(kolom2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(pembagian, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(penjumlahan, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(pengurangan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(perkalian, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addComponent(hitung, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton1))
                .addGap(18,
18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kolom1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(penjumlahan)
                    .addComponent(pengurangan))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kolom2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pembagian)
                    .addComponent(perkalian))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(hasilhitung, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hitung))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(hapus)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                       

    private void pembagianActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        int a , b;
        a = Integer.parseInt(kolom1.getText());
        b = Integer.parseInt(kolom2.getText());
        hasilhitung.setText(Integer.toString(a/b));
    }                                        

    private void hitungActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
        kolom1.setText(“”);
        kolom2.setText(“”);
        kolom1.setText(hasilhitung.getText());
        hasilhitung.setText(“”);
    }                                     

    private void penguranganActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        int a , b;
        a = Integer.parseInt(kolom1.getText());
        b = Integer.parseInt(kolom2.getText());
        hasilhitung.setText(Integer.toString(a-b));
    }                                          

    private void penjumlahanActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        int a , b;
        a = Integer.parseInt(kolom1.getText());
        b = Integer.parseInt(kolom2.getText());
        hasilhitung.setText(Integer.toString(a+b));
    }                                          

    private void perkalianActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        int a , b;
        a = Integer.parseInt(kolom1.getText());
        b = Integer.parseInt(kolom2.getText());
        hasilhitung.setText(Integer.toString(a*b));
    }                   &
nbsp;                    

    private void hapusActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
        kolom1.setText(“”);
        kolom2.setText(“”);
        hasilhitung.setText(“”);
    }                                    

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        System.exit(0);
    }                                       

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new kalkulatorANDHIKA().setVisible(true);
            }
        });
    }
    // Variables declaration – do not modify                    
    private javax.swing.JButton hapus;
    private javax.swing.JTextField hasilhitung;
    private javax.swing.JButton hitung;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField kolom1;
    private javax.swing.JTextField kolom2;
    private javax.swing.JButton pembagian;
    private javax.swing.JButton pengurangan;
    private javax.swing.JButton penjumlahan;
    private javax.swing.JButton perkalian;
    // End of variables declaration                  
}

nah sebetulnya yang terpenting adalah pada tampilan dan actionperformed nya
Gambar Gravatar
Blogger dan Programmer | andhika.na@gmail.com

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *