Tutorial Programming

TUTORIAL JAVA SWING Kalkulator



Hallo sahabat saya akan menjelaskan bagaimana step by step membuat aplikasi java dengan mengunakan swing pada NETBEANS, mungkin bagi sahabat yang sudah tau bagaimana membuat aplikasi java dengan tools yang bermacam ragam, tidak apa-apa, karena setiap tools memiliki kelebihan masing-masing, kali ini saya mengunakan NETBEANS IDE 8.0.2, di tutorial sebelumnya saya sudah memberi tau link download netbeans dan JDK untuk keperluan pembuatan aplikasi tersebut. Baiklah sebelum kita melanjutkan pembuatan , kita bismillah terlebih dahulu. 

Pertama buat new project -> pilih java -> Java Aplication -> Beri nama project dan posisi DIR project

Kedua buka project -> Source Packeges -> Nama Packeges sahabat

Ketiga pada packeges sahabat klik kanan -> pilih new -> pilih JFrame Form Misalkan kalau saya adalah Kalkulator

Keempat pada project sahabat klik kanan, lalu pilih properties -> RUN -> pada label field MAIN CLASS pilih nama packeges sabahat misalkan kalau saya Views.Kalkulator lalu klik oke, fungsi tersebut saat kita running program, akan menampilkan aplikasi kalkulator di form awal.

Kelima copy coding tersebut :

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Views;

/**
 *
 * @author Ary
 */
public class Kalkulator extends javax.swing.JFrame {

    /**
     * Creates new form Kalkulator
     */
    public Kalkulator() {
        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")
    //                           
    private void initComponents() {

        NilaiAkhir = new javax.swing.JTextField();
        Tambah = new javax.swing.JButton();
        Kurang = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        Hasil = new javax.swing.JTextField();
        Bagi = new javax.swing.JButton();
        Kali = new javax.swing.JButton();
        BtnHapus = new javax.swing.JButton();
        Keluar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        NIlaiAwal = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Tambah.setText("Tambah");
        Tambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TambahActionPerformed(evt);
            }
        });

        Kurang.setText("Kurang");
        Kurang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KurangActionPerformed(evt);
            }
        });

        jLabel3.setText("Hasil");

        Hasil.setEditable(false);

        Bagi.setText("Bagi");
        Bagi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BagiActionPerformed(evt);
            }
        });

        Kali.setText("Kali");
        Kali.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KaliActionPerformed(evt);
            }
        });

        BtnHapus.setText("Hapus");
        BtnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnHapusActionPerformed(evt);
            }
        });

        Keluar.setText("Keluar");
        Keluar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KeluarActionPerformed(evt);
            }
        });

        jLabel1.setText("Nilai Awal");

        jLabel2.setText("Nilai Akhir");

        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(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(BtnHapus)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(NilaiAkhir, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                                .addComponent(NIlaiAwal)
                                .addComponent(Hasil)))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(Tambah, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(Kurang, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(Bagi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(Kali, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(Keluar))
                .addContainerGap(98, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(NIlaiAwal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Tambah))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(NilaiAkhir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Kurang)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Bagi)))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(Hasil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Kali))
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnHapus)
                    .addComponent(Keluar))
                .addContainerGap(63, Short.MAX_VALUE))
        );

        pack();
    }//                         

    private void TambahActionPerformed(java.awt.event.ActionEvent evt) {                                       
        int nil1=Integer.parseInt(NIlaiAwal.getText());
        int nil2=Integer.parseInt(NilaiAkhir.getText());
        int hsl=nil1+nil2;
        Hasil.setText(Double.toString(hsl));
        // TODO add your handling code here:
    }                                      

    private void KurangActionPerformed(java.awt.event.ActionEvent evt) {                                       
        int nil1=Integer.parseInt(NIlaiAwal.getText());
        int nil2=Integer.parseInt(NilaiAkhir.getText());
        int hsl=nil1-nil2;
        Hasil.setText(Double.toString(hsl));
        // TODO add your handling code here:
    }                                      

    private void BagiActionPerformed(java.awt.event.ActionEvent evt) {                                     
        int nil1=Integer.parseInt(NIlaiAwal.getText());
        int nil2=Integer.parseInt(NilaiAkhir.getText());
        int hsl=nil1/nil2;
        Hasil.setText(Double.toString(hsl));
        // TODO add your handling code here:
    }                                    

    private void KaliActionPerformed(java.awt.event.ActionEvent evt) {                                     
        int nil1=Integer.parseInt(NIlaiAwal.getText());
        int nil2=Integer.parseInt(NilaiAkhir.getText());
        int hsl=nil1*nil2;
        Hasil.setText(Double.toString(hsl));
        // TODO add your handling code here:
    }                                    

    private void BtnHapusActionPerformed(java.awt.event.ActionEvent evt) {                                         
        NIlaiAwal.setText("");
        NilaiAkhir.setText("");
        Hasil.setText("");
        // TODO add your handling code here:
    }                                        

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

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Kalkulator().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton Bagi;
    private javax.swing.JButton BtnHapus;
    private javax.swing.JTextField Hasil;
    private javax.swing.JButton Kali;
    private javax.swing.JButton Keluar;
    private javax.swing.JButton Kurang;
    private javax.swing.JTextField NIlaiAwal;
    private javax.swing.JTextField NilaiAkhir;
    private javax.swing.JButton Tambah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration                   
}


Setelah itu run aplikasi F6, seperti gambar dibawah:




Baiklah sekian tutorial saya semoga bermanfaat yah, semoga sukses, terimakasih.

No comments:

Post a Comment

Tutorial Programming Designed by Templateism | Ary EJFA Copyright © 2015

Theme images by richcano. Powered by Blogger.