Source Code Program Java Menghitung Panjang Dan Lebar dengan netbeans


walah gan ternyata cewe kalo lagi Merah sensitifnya sama kaya program error di java malesin. dahh Kali ini Ane share Membuat netbeans  pakai Jframe.
1.buat class di Jframe @P_111100042
2.mulailah mendisain di Jframe
3.masukan :4 label,4 textfield dan 2 tombol butten
Kayagini neee jadinya




 /* @author al dkidrebel
 */
import javax.swing.JOptionPane;
import java.awt.event.*;
import java.awt.*;
public class P_111100042 extends javax.swing.JFrame {

 
    public P_111100042() {
        initComponents();
    }

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtPanjang = new javax.swing.JTextField();
        txtLebar = new javax.swing.JTextField();
        jBtnHItung = new javax.swing.JButton();
        jBtnHapus = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtLuas = new javax.swing.JTextField();
        txtKeliling = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("aldkidrebel");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Panjang");

        jLabel2.setText("Lebar");

        txtPanjang.setBackground(new java.awt.Color(51, 255, 255));
        txtPanjang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPanjangActionPerformed(evt);
            }
        });
        txtPanjang.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPanjangKeyPressed(evt);
            }
        });

        txtLebar.setBackground(new java.awt.Color(51, 255, 255));
        txtLebar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtLebarActionPerformed(evt);
            }
        });
        txtLebar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtLebarKeyPressed(evt);
            }
        });

        jBtnHItung.setBackground(new java.awt.Color(255, 0, 0));
        jBtnHItung.setForeground(new java.awt.Color(0, 204, 0));
        jBtnHItung.setText("Hitung");
        jBtnHItung.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnHItungActionPerformed(evt);
            }
        });

        jBtnHapus.setBackground(new java.awt.Color(255, 0, 0));
        jBtnHapus.setForeground(new java.awt.Color(0, 204, 0));
        jBtnHapus.setText("Hapus");
        jBtnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnHapusActionPerformed(evt);
            }
        });

        jLabel3.setText("Luas");

        jLabel4.setText("Keliling");

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

        txtKeliling.setBackground(new java.awt.Color(0, 255, 204));
        txtKeliling.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtKelilingActionPerformed(evt);
            }
        });

        jButton1.setBackground(new java.awt.Color(204, 0, 0));
        jButton1.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\al dkidrebel\\My Documents\\New Folder\\sr1.jpeg")); // NOI18N
        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(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtLebar, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtPanjang, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jBtnHItung)
                                .addGap(29, 29, 29)
                                .addComponent(jBtnHapus))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel3))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtLuas, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                                    .addComponent(txtKeliling))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(txtPanjang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtLebar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnHItung)
                    .addComponent(jBtnHapus))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtLuas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtKeliling, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(53, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(25, 25, 25))
        );

        pack();
    }                    

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

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

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

    private void jBtnHapusActionPerformed(java.awt.event.ActionEvent evt) {                                         
 int exitApp= JOptionPane.showConfirmDialog(null, "Apakah Anda Yakin akan menghapus?",
                   "Konfirmasi", JOptionPane.YES_NO_OPTION);
                    if (exitApp==JOptionPane.YES_OPTION){
                        txtPanjang.setText ("");
                        txtLebar.setText("");
                        txtLuas.setText("");
                        txtKeliling.setText ("");

                    }
    }                                        

    private void jBtnHItungActionPerformed(java.awt.event.ActionEvent evt) {                                          
 String panjang = txtPanjang.getText();
        String lebar = txtLebar.getText();
        Double pjg = Double.parseDouble(panjang);
        Double lbr = Double.parseDouble(lebar);

        txtLuas.setText (""+pjg * lbr);
        txtKeliling.setText (""+2*(pjg + lbr)); 
      
    }                                         



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

    private void formWindowClosing(java.awt.event.WindowEvent evt) {                                  
        int exitApp= JOptionPane.showConfirmDialog(null,
            "Apakah Ingin Menutup Aplikasi?", "Konfirmasi", JOptionPane.YES_NO_OPTION);
            if (exitApp==JOptionPane.YES_OPTION){
            System.exit(0);

            }
    }                                 

    private void txtPanjangKeyPressed(java.awt.event.KeyEvent evt) {                                     
if(evt.getKeyCode() == KeyEvent.VK_0){
        } else if (evt.getKeyCode() == KeyEvent.VK_1){
           } else if (evt.getKeyCode() == KeyEvent.VK_2){
                } else if (evt.getKeyCode() == KeyEvent.VK_3){
                    } else if (evt.getKeyCode() == KeyEvent.VK_4){
                        } else if (evt.getKeyCode() == KeyEvent.VK_5){
                            } else if (evt.getKeyCode() == KeyEvent.VK_6){
                                } else if (evt.getKeyCode() == KeyEvent.VK_7){
                                    } else if (evt.getKeyCode() == KeyEvent.VK_8){
                                        } else if (evt.getKeyCode() == KeyEvent.VK_9){
                                            } else if (evt.getKeyCode() == KeyEvent.VK_PERIOD){
                                                } else if (evt.getKeyCode() != KeyEvent.VK_BACK_SPACE)
        JOptionPane.showMessageDialog(null,  "Harus memasukkan Angka !  "
                + "SEGERA HAPUS !",
"Warning",  JOptionPane.WARNING_MESSAGE);     

   
    }                                    

    private void txtLebarKeyPressed(java.awt.event.KeyEvent evt) {                                   
if(evt.getKeyCode() == KeyEvent.VK_0){
        } else if (evt.getKeyCode() == KeyEvent.VK_1){
           } else if (evt.getKeyCode() == KeyEvent.VK_2){
                } else if (evt.getKeyCode() == KeyEvent.VK_3){
                    } else if (evt.getKeyCode() == KeyEvent.VK_4){
                        } else if (evt.getKeyCode() == KeyEvent.VK_5){
                            } else if (evt.getKeyCode() == KeyEvent.VK_6){
                                } else if (evt.getKeyCode() == KeyEvent.VK_7){
                                    } else if (evt.getKeyCode() == KeyEvent.VK_8){
                                        } else if (evt.getKeyCode() == KeyEvent.VK_9){
                                            } else if (evt.getKeyCode() == KeyEvent.VK_PERIOD){                                            
                                                } else if (evt.getKeyCode() != KeyEvent.VK_BACK_SPACE)
                                                
        JOptionPane.showMessageDialog(null,  "arus memasukkan Angka !  "
                + "SEGERA HAPUS !",
"Warning",  JOptionPane.WARNING_MESSAGE);     
       
         }                                  

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
JOptionPane.showMessageDialog(null,  "anda telah menklik tombol saringgan!  "
                + "Klik OK!",
"Warning",  JOptionPane.WARNING_MESSAGE);     
       
    }                                       
    /**
     * @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(P_111100042.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(P_111100042.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(P_111100042.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(P_111100042.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 P_111100042().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton jBtnHItung;
    private javax.swing.JButton jBtnHapus;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtKeliling;
    private javax.swing.JTextField txtLebar;
    private javax.swing.JTextField txtLuas;
    private javax.swing.JTextField txtPanjang;
    // End of variables declaration                  
}
CATATAN
  • Perhatikan tilisan berwarna merah  DO_NOTHING_ON_CLOSE buat di disain klik kanan propertis dan cari tombol defaultCloseOperation    ganti dengan DO_NOTHING_ON_CLOSE 
  • Membuat action keypressed klik kanan di textfield event _action_Keypressend lakukan pada setiap textfield.
OUTPUT


CATATAN
Baca Juga : Source Code Java Animasi Bitmap Frame
BUDAYAKAN BERBAGI UNTUK MENYEMPURNAKAN BELAJAR NO RASIS AND NO SARA GAN mohon maaf kalo ada salah dan kurang jelas maklum masih newbe

0 Comments


EmoticonEmoticon