Senin, 01 Desember 2014

Aplikasi menu makanan

Pada kali ini saya membuat aplikasi menu makanan dengan menggunakan jradiobutton dan jcheckbox, seperti biasa pertama kita buat design dahulu sebelum masuk ke codingnya. Berikut langkah-langkahnya :

1.Ini adalah design yang saya buat :


2. Setelah membuat design kita masuk ke codingnya :

 




3. Setelah selesai coding run :




Coding :

/*
 * 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 Dewiex
 */
public class NewJFrame extends javax.swing.JFrame {

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

        jCheckBox1 = new javax.swing.JCheckBox();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jmiayam = new javax.swing.JCheckBox();
        jsoto = new javax.swing.JCheckBox();
        jmigoreng = new javax.swing.JCheckBox();
        jbakso = new javax.swing.JCheckBox();
        vbakso = new javax.swing.JTextField();
        vsoto = new javax.swing.JTextField();
        vmiayam = new javax.swing.JTextField();
        vtotal = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        vmigoreng = new javax.swing.JTextField();

        jCheckBox1.setText("jCheckBox1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tiranti Solid LET", 1, 18)); // NOI18N
        jLabel1.setText("Menu Makanan");

        jmiayam.setText("Mi Ayam @10000");
        jmiayam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiayamActionPerformed(evt);
            }
        });

        jsoto.setText("Soto @8000");
        jsoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jsotoActionPerformed(evt);
            }
        });

        jmigoreng.setText("Mi Goreng @8000");
        jmigoreng.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmigorengActionPerformed(evt);
            }
        });

        jbakso.setText("Bakso @5000");
        jbakso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbaksoActionPerformed(evt);
            }
        });

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

        jButton2.setText("Hitung");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel2.setText("Total");

        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(131, 131, 131)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jsoto)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jbakso)
                                            .addComponent(jmiayam)
                                            .addComponent(jmigoreng))
                                        .addGap(30, 30, 30))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jButton2)
                                        .addGap(18, 18, 18)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(vsoto, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                                    .addComponent(vbakso)
                                    .addComponent(vmiayam, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                                    .addComponent(jButton1)
                                    .addComponent(vmigoreng, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                                    .addComponent(vtotal)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(123, 123, 123)
                        .addComponent(jLabel2)))
                .addContainerGap(57, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbakso)
                    .addComponent(vbakso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jmiayam)
                    .addComponent(vmiayam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jsoto)
                    .addComponent(vsoto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jmigoreng)
                    .addComponent(vmigoreng, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(vtotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(75, Short.MAX_VALUE))
        );

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

    private void jmiayamActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        if (jmiayam.isSelected() == true) {
            vmiayam.setEnabled(true);
        } else {
            vmiayam.setEnabled(false);
            vmiayam.setText("0");
        }

    }                                      

    private void jbaksoActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
        if (jbakso.isSelected() == true) {
            vbakso.setEnabled(true);
        } else {
            vbakso.setEnabled(false);
            vbakso.setText("0");
        }
    }                                     

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int baso = 5000 * Integer.parseInt(vbakso.getText());
        int miayam = 10000 * Integer.parseInt(vmiayam.getText());
        int soto = 8000 * Integer.parseInt(vsoto.getText());
        int migoreng = 8000 * Integer.parseInt(vmigoreng.getText());
       
        int hasil = baso + miayam + soto + migoreng;
        vtotal.setText("" + hasil);
    }                                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        vbakso.setText("0");
        vmiayam.setText("0");
        vsoto.setText("0");
        vmigoreng.setText("0");
        vtotal.setText("0");
        jbakso.setSelected(false);
        jmiayam.setSelected(false);
        jsoto.setSelected(false);
        jmigoreng.setSelected(false);
        vbakso.setEnabled(false);
        vmiayam.setEnabled(false);
        vsoto.setEnabled(false);
        vmigoreng.setEnabled(false);
       
    }                                       

    private void formWindowActivated(java.awt.event.WindowEvent evt) {                                    
        // TODO add your handling code here:
        vbakso.setText("0");
        vsoto.setText("0");
        vmiayam.setText("0");
        vmigoreng.setText("0");
        vbakso.setEnabled(false);
        vsoto.setEnabled(false);
        vmiayam.setEnabled(false);
        vmigoreng.setEnabled(false);

    }                                   

    private void jsotoActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
                if (jsoto.isSelected() == true) {
            vsoto.setEnabled(true);
        } else {
            vsoto.setEnabled(false);
            vsoto.setText("0");
        }

    }                                    

    private void jmigorengActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
                if (jmigoreng.isSelected() == true) {
            vmigoreng.setEnabled(true);
        } else {
            vmigoreng.setEnabled(false);
            vmigoreng.setText("0");
        }

    }                                        

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

    // Variables declaration - do not modify                    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JCheckBox jbakso;
    private javax.swing.JCheckBox jmiayam;
    private javax.swing.JCheckBox jmigoreng;
    private javax.swing.JCheckBox jsoto;
    private javax.swing.JTextField vbakso;
    private javax.swing.JTextField vmiayam;
    private javax.swing.JTextField vmigoreng;
    private javax.swing.JTextField vsoto;
    private javax.swing.JTextField vtotal;
    // End of variables declaration                  
}
 
Itulah program menu makan yg saya buat menggunakan jradiobutton dan jcheckbox maklum juka masih sangat sederhana dan banyak kekurangan. Mohon bimbingannya :)