MASIGNASUKAv102
9032534155567241003

Membuat Aplikasi Aritmatika dalam Netbeans Menggunakan Bahasa Pemrograman Java

Membuat Aplikasi Aritmatika dalam Netbeans Menggunakan Bahasa Pemrograman Java
Add Comments
Saturday, 23 November 2019

Halo Coders!
Apa kabar? hehe..
Setelah sekian bulan aku ngga posting lagi karena sibuk *ehem* aku sempatkan lagi buat nulis lagi disini hehe..

Sekarang saya akan mengshare source code yang telah saya buat dalam tugas praktik PBO minggu lalu disekolah. Daripada nganggur mending dibagi-bagi. Meskipun sangat sederhana namun semoga bermanfaat ya teman-teman hehe ^^

Install Netbeans

Saya asumsikan bahwa disini kalian sudah menginstalnya di laptop kalian masing-masing. Kalo belum, silahkan download dulu ya..


Mulai membuat Project

Langkah kedua teman-teman langsung saja new project > java application 



Klik kanan di Source Packages lalu New - JPanelForm



Masukan Class Name : Aritmatika
Lalu Finish


Disini kita hanya akan menggunakan panel, text field, button, dan label



Silahkan samakan tampilannya, atau berkreasilah sesuai imajinasi kalian!
Jangan lupa untuk menggati setiap variabel name nya dengan cara klik kanan pada elemennya lalu change variabel name 


ganti dengan nama prosesKali

Begitu juga dengan proses-proses yang ada dibawahnya seperti prosesJumlah, prosesKurang, prosesBagi.


Ganti semua variabelnya dengan seperti ini yaa !!! INGAT !! bukan text tapi Variabel name nya, OK!

Masuk ke source


Masukan variabel a, b, dan hasil;


Lalu, masuk lagi ke Design , double klik dibagian button Proses yang pertama

Masukan kode dibawah ini :

private void prosesKaliActionPerformed(java.awt.event.ActionEvent evt) {                                           
        a = Integer.parseInt(kali1.getText());
        b = Integer.parseInt(kali2.getText());
        hasil = a*b; // ini proses perkalian
        hasilkali.setText(Integer.toString(hasil));
    } 

Ganti semua syntax dalem button dengan kode diatas. Namun jangan lupa mengganti sistem aritmatika dan nama variabelnya.

Ini adalah kode penuhnya :::

public class aritmatika extends javax.swing.JFrame {

   int a,b,hasil;
   
    public aritmatika() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTextField11 = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        kali1 = new javax.swing.JTextField();
        kali2 = new javax.swing.JTextField();
        hasilkali = new javax.swing.JTextField();
        prosesKali = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        bagi1 = new javax.swing.JTextField();
        bagi2 = new javax.swing.JTextField();
        hasilbagi = new javax.swing.JTextField();
        prosesBagi = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jumlah1 = new javax.swing.JTextField();
        jumlah2 = new javax.swing.JTextField();
        hasiljumlah = new javax.swing.JTextField();
        prosesJumlah = new javax.swing.JButton();
        kurang1 = new javax.swing.JTextField();
        kurang2 = new javax.swing.JTextField();
        hasilkurang = new javax.swing.JTextField();
        prosesKurang = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jTextField11.setText("jTextField11");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 204, 204));

        jPanel2.setBackground(new java.awt.Color(255, 204, 204));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Aplikasi Aritmatika Di JAVA");

        jLabel2.setText("Perkalian");

        kali1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        kali1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kali1ActionPerformed(evt);
            }
        });

        kali2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        kali2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kali2ActionPerformed(evt);
            }
        });

        hasilkali.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        hasilkali.setText("?");
        hasilkali.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hasilkaliActionPerformed(evt);
            }
        });

        prosesKali.setText("Proses");
        prosesKali.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prosesKaliActionPerformed(evt);
            }
        });

        jLabel3.setText("Pembagian");

        bagi1.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        bagi2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        bagi2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bagi2ActionPerformed(evt);
            }
        });

        hasilbagi.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        hasilbagi.setText("?");

        prosesBagi.setText("Proses");
        prosesBagi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prosesBagiActionPerformed(evt);
            }
        });

        jLabel4.setText("Penjumlahan");

        jLabel5.setText("Pengurangan");

        jumlah1.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jumlah2.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        hasiljumlah.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        hasiljumlah.setText("?");

        prosesJumlah.setText("Proses");
        prosesJumlah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prosesJumlahActionPerformed(evt);
            }
        });

        kurang1.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        kurang2.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        hasilkurang.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        hasilkurang.setText("?");

        prosesKurang.setText("Proses");
        prosesKurang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prosesKurangActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(kurang1, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addGap(22, 22, 22))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(13, 13, 13)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jumlah1, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
                            .addComponent(bagi1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(kali1, javax.swing.GroupLayout.Alignment.LEADING))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(kali2)
                    .addComponent(bagi2)
                    .addComponent(jumlah2)
                    .addComponent(kurang2, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(hasilbagi)
                    .addComponent(hasilkurang, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(hasilkali, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hasiljumlah))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(prosesKali, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(prosesBagi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                            .addComponent(prosesJumlah)
                            .addContainerGap()))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(prosesKurang)
                        .addContainerGap())))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addComponent(jLabel1)
                .addGap(0, 100, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addGap(26, 26, 26)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(kali1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(kali2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hasilkali, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(prosesKali))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(bagi1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bagi2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hasilbagi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(prosesBagi))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jumlah1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jumlah2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hasiljumlah, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(prosesJumlah))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(kurang1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(kurang2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(hasilkurang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(prosesKurang)))
                .addContainerGap(60, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

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

    private void kali2ActionPerformed(java.awt.event.ActionEvent evt) {                                      

    }                                     

    private void prosesBagiActionPerformed(java.awt.event.ActionEvent evt) {                                           
        a = Integer.parseInt(bagi1.getText());
        b = Integer.parseInt(bagi2.getText());
        hasil = a/b; //proses pembagian
        hasilbagi.setText(Integer.toString(hasil));
    }                                          

    private void bagi2ActionPerformed(java.awt.event.ActionEvent evt) {                                      
    }                                     

    private void prosesKaliActionPerformed(java.awt.event.ActionEvent evt) {                                           
        a = Integer.parseInt(kali1.getText());
        b = Integer.parseInt(kali2.getText());
        hasil = a*b; // ini proses perkalian
        hasilkali.setText(Integer.toString(hasil));
    }                                          

    private void hasilkaliActionPerformed(java.awt.event.ActionEvent evt) {                                          
    }                                         

    private void kali1ActionPerformed(java.awt.event.ActionEvent evt) {                                      
    }                                     

    private void prosesJumlahActionPerformed(java.awt.event.ActionEvent evt) {                                             
        a = Integer.parseInt(jumlah1.getText());
        b = Integer.parseInt(jumlah2.getText());
        hasil = a+b; // ini proses penjumlahan
        hasiljumlah.setText(Integer.toString(hasil));
    }                                            

    private void prosesKurangActionPerformed(java.awt.event.ActionEvent evt) {                                             
        a = Integer.parseInt(kurang1.getText());
        b = Integer.parseInt(kurang2.getText());
        hasil = a-b; // ini proses pengurangan
        hasilkurang.setText(Integer.toString(hasil));
    }                                            

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* 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(aritmatika.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(aritmatika.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(aritmatika.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(aritmatika.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

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

    // Variables declaration - do not modify                     
    private javax.swing.JTextField bagi1;
    private javax.swing.JTextField bagi2;
    private javax.swing.JTextField hasilbagi;
    private javax.swing.JTextField hasiljumlah;
    private javax.swing.JTextField hasilkali;
    private javax.swing.JTextField hasilkurang;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jumlah1;
    private javax.swing.JTextField jumlah2;
    private javax.swing.JTextField kali1;
    private javax.swing.JTextField kali2;
    private javax.swing.JTextField kurang1;
    private javax.swing.JTextField kurang2;
    private javax.swing.JButton prosesBagi;
    private javax.swing.JButton prosesJumlah;
    private javax.swing.JButton prosesKali;
    private javax.swing.JButton prosesKurang;
    // End of variables declaration                   
}


Terimakasih yaa.. Semoga bermanfaat ^^
Maaf jika kodenya acak acakan soalnya belum bisa menggunakan Plugin syntax Highlighter huhuhuh , Ajarin dong :(