Pages

Friday, 2 May 2014

Cara membuat Aplikasi sederhana menggunakan jFrame java Netbeans

Aplikasi pada desain jFrame kali ini menggunakan control TextField, CheckBox, Button, MenuBar dan jCheckBox (jika tidak di klik, kita tidak bisa mengisi textfield) dengan membuat aplikasi perhitungan jumlah total menu BAKSO MAKENULL.

Untuk lebih mudah dipahami, langsung saja kita akan membuat sebuah program yang sederhana seperti screenshot dibawah ini :


Langkah-langkah yang harus dilakukan yaitu :
Pertama buatlah rancangan form di netbeans seperti gambar dibawah ini :


Komponen yang terdapat di form tersebut adalah sebagai berikut, ubah nama variable yang diberi kotak hijau itu seperti gambar dibawah ini, atau terserah keinginan kalian yg penting saat kita coding harus sama dengan nama itu :

 Untuk lebih jelasnya pertama-tama pada project klick kanan --> new --> jFrame


Kemudian klik label drak ke layer jFrame -->  klick kanan pada label --> diisi sesuai dengan yang diinginkan. Jika ingin mengedit Font klik font dilayer sebelah kiri --> kemudian edit fontny.. lebih jelasnya lihat gambar.

Di program ini, aturannya adalah jika pilihan menu tidak di centang, maka jTextFieldnya tidak bisa diisi dengan jumlah menu yang dibeli seperti tulisan di awal tadi. Jadi untuk itu jTextFieldnya di hilangkan editablenya.

Caranya sebagai berikut:
Klik kanan pada jTextField --Properties maka akan muncul tampilan seperti dibawah ini :

Hilangkan tanda centang pada editable, selanjutnya di Close.
Selanjutnya pada semua jCheckBox dan jButton yang ada di aplikasi, di klik kanan, pilih Event --> Action --> actionPerformed. Masuk ke bagian Source.

Ketikkan kode program yang tertera pada gambar :


 Lanjutkan untuk menu yang lainnya seperti gambar di atas tadi.

Untuk jButtonHitung, ketikkan kode seperti yang tertera pada gambar :




 pada jButtonHapus Ketikkan juga kode seperti yang tertera pada gambar :



Kalau sudah selesai langkah-langkah diatas, sekarang tinggal mencoba menjalankan programnya dengan menekan klik kanan pada Project --> Run File atau tekan tombol shift+F6.


Beri tanda centrang kemudian diisi sesuai dengan jumlah pesanan menu -- > klik hitung --> jumlah harga secara otomatis terisi seperti gambar :


Klik hapus --> jumlah nilai yang terterara secara otomatis terhapus seperti gambar :


Full Coding program sebagai berikut :


/*
 * 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.
 */

/**
 *
 * @author ARUL
 */
public class UAS_I extends javax.swing.JFrame {

    /**
     * Creates new form UAS_I
     */
    public UAS_I() {
        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() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        baksourat = new javax.swing.JTextField();
        jCheckBox1BAKSOURAT = new javax.swing.JCheckBox();
        jCheckBox1BAKSOTELUR = new javax.swing.JCheckBox();
        baksotelur = new javax.swing.JTextField();
        jCheckBox1MIEAYAM = new javax.swing.JCheckBox();
        mieayam = new javax.swing.JTextField();
        jCheckBox1MIEAYAMBAKSO = new javax.swing.JCheckBox();
        mieayambakso = new javax.swing.JTextField();
        HITUNG = new javax.swing.JButton();
        HAPUS = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        TOTAL = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        jTextField1.setText("jTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tekton Pro Cond", 1, 18)); // NOI18N
        jLabel1.setText("BAKSO MAKENULL");

        baksourat.setEditable(false);
        baksourat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                baksouratActionPerformed(evt);
            }
        });

        jCheckBox1BAKSOURAT.setText("BAKSO URAT @ Rp. 8000");
        jCheckBox1BAKSOURAT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1BAKSOURATActionPerformed(evt);
            }
        });

        jCheckBox1BAKSOTELUR.setText("BAKSO TELUR @ Rp. 8000");
        jCheckBox1BAKSOTELUR.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1BAKSOTELURActionPerformed(evt);
            }
        });

        baksotelur.setEditable(false);
        baksotelur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                baksotelurActionPerformed(evt);
            }
        });

        jCheckBox1MIEAYAM.setText("MIE AYAM @ Rp. 6000");
        jCheckBox1MIEAYAM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1MIEAYAMActionPerformed(evt);
            }
        });

        mieayam.setEditable(false);
        mieayam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mieayamActionPerformed(evt);
            }
        });

        jCheckBox1MIEAYAMBAKSO.setText("MIE AYAM bakso @ Rp. 8000");
        jCheckBox1MIEAYAMBAKSO.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1MIEAYAMBAKSOActionPerformed(evt);
            }
        });

        mieayambakso.setEditable(false);
        mieayambakso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mieayambaksoActionPerformed(evt);
            }
        });

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

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

        jLabel2.setText("JUMLAH HARGA");

        TOTAL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TOTALActionPerformed(evt);
            }
        });

        jLabel3.setText("http://belajrkom.blogspot.com");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(192, 192, 192)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCheckBox1MIEAYAMBAKSO)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(mieayambakso, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCheckBox1MIEAYAM)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(mieayam, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCheckBox1BAKSOTELUR)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(baksotelur, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCheckBox1BAKSOURAT)
                                .addGap(40, 40, 40)
                                .addComponent(baksourat, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(210, 210, 210)
                        .addComponent(HITUNG)
                        .addGap(18, 18, 18)
                        .addComponent(HAPUS))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(190, 190, 190)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(TOTAL, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jLabel3)))
                .addContainerGap(110, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(baksourat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox1BAKSOURAT))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox1BAKSOTELUR)
                    .addComponent(baksotelur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox1MIEAYAM)
                    .addComponent(mieayam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mieayambakso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox1MIEAYAMBAKSO))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(HITUNG)
                    .addComponent(HAPUS))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(TOTAL, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void baksouratActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
       
    }                                        

    private void jCheckBox1BAKSOURATActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        // TODO add your handling code here:
        if (jCheckBox1BAKSOURAT.isSelected() == true){
            baksourat.setEditable(true);
        } else {
            baksourat.setEditable(false);
            baksourat.setText("");
        }
    }                                                   

    private void jCheckBox1BAKSOTELURActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        // TODO add your handling code here:
        if (jCheckBox1BAKSOTELUR.isSelected() == true){
            baksotelur.setEditable(true);
        } else {
            baksotelur.setEditable(false);
            baksotelur.setText("");
        }
    }                                                   

    private void baksotelurActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
       
    }                                         

    private void jCheckBox1MIEAYAMActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:
        if (jCheckBox1MIEAYAM.isSelected() == true){
            mieayam.setEditable(true);
        } else {
            mieayam.setEditable(false);
            mieayam.setText("");
        }
    }                                                

    private void mieayamActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
    }                                      

    private void jCheckBox1MIEAYAMBAKSOActionPerformed(java.awt.event.ActionEvent evt) {                                                      
        // TODO add your handling code here:
        if (jCheckBox1MIEAYAMBAKSO.isSelected() == true){
            mieayambakso.setEditable(true);
        } else {
            mieayambakso.setEditable(false);
            mieayambakso.setText("");
        }
    }                                                      

    private void mieayambaksoActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void TOTALActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
       
    }                                    

    private void HITUNGActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
        int total = 0;
        if (jCheckBox1BAKSOURAT.isSelected()) {
            String BU = baksourat.getText();
            int jumlahBU = Integer.parseInt(BU);
            int hargaBU = jumlahBU * 8000;
            total = total + hargaBU;
        }
        if (jCheckBox1BAKSOTELUR.isSelected()) {
            String BT = baksotelur.getText();
            int jumlahBT = Integer.parseInt(BT);
            int hargaBT = jumlahBT * 8000;
            total = total + hargaBT;
        }
        if (jCheckBox1MIEAYAM.isSelected()) {
            String MA = mieayam.getText();
            int jumlahMA = Integer.parseInt(MA);
            int hargaMA = jumlahMA * 6000;
            total = total + hargaMA;
        }
        if (jCheckBox1MIEAYAMBAKSO.isSelected()) {
            String MAB = mieayambakso.getText();
            int jumlahMAB = Integer.parseInt(MAB);
            int hargaMAB = jumlahMAB * 8000;
            total = total + hargaMAB;
        }
        TOTAL.setText(""+total);
    }                                     

    private void HAPUSActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
        baksourat.setEditable(false);
        baksourat.setText("");
       
        baksotelur.setEditable(false);
        baksotelur.setText("");
       
        mieayam.setEditable(false);
        mieayam.setText("");
       
        mieayambakso.setEditable(false);
        mieayambakso.setText("");
       
        TOTAL.setText("");
    }                                    

    /**
     * @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(UAS_I.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UAS_I.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UAS_I.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UAS_I.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 UAS_I().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                    
    private javax.swing.JButton HAPUS;
    private javax.swing.JButton HITUNG;
    private javax.swing.JTextField TOTAL;
    private javax.swing.JTextField baksotelur;
    private javax.swing.JTextField baksourat;
    private javax.swing.JCheckBox jCheckBox1BAKSOTELUR;
    private javax.swing.JCheckBox jCheckBox1BAKSOURAT;
    private javax.swing.JCheckBox jCheckBox1MIEAYAM;
    private javax.swing.JCheckBox jCheckBox1MIEAYAMBAKSO;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField mieayam;
    private javax.swing.JTextField mieayambakso;
    // End of variables declaration                  
}



Demikian Tugas UAS SEMESTER PERTAMA ini selamat malam dan semoga bermanfaat :
Komando..!!!!!!!!!













.

12 comments:

  1. mkasih gan ,,, postingan cara-membuat-aplikasi-sederhana , yang bagus dan bermanfaat ini layaknya di share ajja ,, nih saya bantu ngeshare ,, ,, jgn lupa kunbal nya pulsagratisandroidku.blogspot.com terimakasih skali lagi gan , maju terus blog nya ,,, !

    ReplyDelete
  2. gan pas waktu di run formnya gak keluar itu kenapa ya, saya pake ide netbeans 8.0 pada windows 7

    ReplyDelete
    Replies
    1. Jangan menyerah gan.. belajar dan belajar terus..

      Delete
  3. Terima kasih admin sudah mau berbagi.. sngat membantu masukan ide aplikasi yg saya buat...

    ReplyDelete
  4. Replies
    1. Baca dr atas pelan pelan.. selamat mencoba..

      Delete