cursosr

Blogger Widgets

Senin, 06 Juli 2015

Perhitungan sederhana menggunakan Java



public class penjumlahan extends javax.swing.JFrame {
 int nilai1;
int nilai2;
int nilai3;
String Hasil;

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

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfnilai2 = new javax.swing.JTextField();
        btntmbah = new javax.swing.JButton();
        btnkurang = new javax.swing.JButton();
        btnkali = new javax.swing.JButton();
        lbltambah = new javax.swing.JLabel();
        tfnilai1 = new javax.swing.JTextField();
        lblkurang = new javax.swing.JLabel();
        lblkali = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Nilai 1 : ");

        jLabel2.setText("Nilai 2 : ");

        btntmbah.setText("+");
        btntmbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btntmbahActionPerformed(evt);
            }
        });

        btnkurang.setText("-");
        btnkurang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnkurangActionPerformed(evt);
            }
        });

        btnkali.setText("*");
        btnkali.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnkaliActionPerformed(evt);
            }
        });

        lbltambah.setText("Hasil Penjumlahan");

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

        lblkurang.setText("Hasil Pengurangan");

        lblkali.setText("Hasil Perkalian");

        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(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btntmbah)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbltambah))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfnilai2, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfnilai1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnkali)
                            .addComponent(btnkurang))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblkali)
                            .addComponent(lblkurang))))
                .addContainerGap(152, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfnilai1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfnilai2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btntmbah)
                    .addComponent(lbltambah))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnkurang)
                    .addComponent(lblkurang))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnkali)
                    .addComponent(lblkali))
                .addContainerGap(123, Short.MAX_VALUE))
        );

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

private void btntmbahActionPerformed(java.awt.event.ActionEvent evt) {                                        
// TODO add your handling code here:
    nilai1=Integer.valueOf(tfnilai1.getText());
nilai2=Integer.valueOf(tfnilai2.getText());
nilai3=nilai1 + nilai2;
        Hasil=String.valueOf(nilai3);
lbltambah.setText("Hasil Penjumlahan: \t\t\t" + Hasil);  
}                                       

private void btnkurangActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
    nilai1=Integer.valueOf(tfnilai1.getText());
nilai2=Integer.valueOf(tfnilai2.getText());
nilai3=nilai1 - nilai2;
        Hasil=String.valueOf(nilai3);
lblkurang.setText("Hasil Pengurangan: \t\t\t" + Hasil);
}                                        

private void btnkaliActionPerformed(java.awt.event.ActionEvent evt) {                                       
// TODO add your handling code here:
    nilai1=Integer.valueOf(tfnilai1.getText());
nilai2=Integer.valueOf(tfnilai2.getText());
nilai3=nilai1 * nilai2;
        Hasil=String.valueOf(nilai3);
lblkali.setText("Hasil Perkalian: \t\t\t" + Hasil);
}                                      

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

    public static void main(String args[]) {
       
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new penjumlahan().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton btnkali;
    private javax.swing.JButton btnkurang;
    private javax.swing.JButton btntmbah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lblkali;
    private javax.swing.JLabel lblkurang;
    private javax.swing.JLabel lbltambah;
    private javax.swing.JTextField tfnilai1;
    private javax.swing.JTextField tfnilai2;
    // End of variables declaration                  
}

Biodata Mahasiswa Menggunakan Java




import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
public class biodata extends javax.swing.JFrame {
private ButtonGroup gabung;
   
    public biodata() {
        super("Biodata Mahasiswa");
        initComponents();
         gabung=new ButtonGroup();
         gabung.add(RbLakilaki);
           gabung.add(RbPerempuan);

        
    }

    /** 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() {

        jLabel1 = new javax.swing.JLabel();
        lblNPM = new javax.swing.JLabel();
        tfNPM = new javax.swing.JTextField();
        RbLakilaki = new javax.swing.JRadioButton();
        lbljeniskelamin = new javax.swing.JLabel();
        RbPerempuan = new javax.swing.JRadioButton();
        lbljurusan = new javax.swing.JLabel();
        btnSimpan = new javax.swing.JButton();
        btbBatal = new javax.swing.JButton();
        Cbjurusan = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        output = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(0, 255, 102));
        setFont(new java.awt.Font("Times New Roman", 0, 14));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel1.setText("Biodata Mahasiswa");

        lblNPM.setFont(new java.awt.Font("Times New Roman", 0, 14));
        lblNPM.setText("NPM:");

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

        RbLakilaki.setFont(new java.awt.Font("Times New Roman", 0, 14));
        RbLakilaki.setText("Laki-laki");
        RbLakilaki.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RbLakilakiActionPerformed(evt);
            }
        });

        lbljeniskelamin.setFont(new java.awt.Font("Times New Roman", 0, 14));
        lbljeniskelamin.setText("Jenis Kelamin:");

        RbPerempuan.setFont(new java.awt.Font("Times New Roman", 0, 14));
        RbPerempuan.setText("Perempuan");

        lbljurusan.setFont(new java.awt.Font("Times New Roman", 0, 14));
        lbljurusan.setText("Jurusan:");

        btnSimpan.setFont(new java.awt.Font("Times New Roman", 0, 14));
        btnSimpan.setText("Simpan");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });

        btbBatal.setFont(new java.awt.Font("Times New Roman", 0, 14));
        btbBatal.setText("Batal");
        btbBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btbBatalActionPerformed(evt);
            }
        });

        Cbjurusan.setFont(new java.awt.Font("Times New Roman", 0, 14));
        Cbjurusan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Manajemen Informatika", "Akuntansi", "Teknik Jaringan" }));

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel5.setText("Terima kasih telah mengisi form ini");

        output.setColumns(20);
        output.setFont(new java.awt.Font("Times New Roman", 0, 14));
        output.setRows(5);
        jScrollPane1.setViewportView(output);

        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()
                        .addContainerGap()
                        .addComponent(lbljurusan))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbljeniskelamin)
                            .addComponent(lblNPM))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tfNPM, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(Cbjurusan, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(RbLakilaki)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(RbPerempuan)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(btnSimpan)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btbBatal))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNPM)
                    .addComponent(tfNPM, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbljeniskelamin)
                    .addComponent(RbLakilaki)
                    .addComponent(RbPerempuan))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbljurusan)
                    .addComponent(Cbjurusan, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSimpan)
                    .addComponent(btbBatal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

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

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

}                                    

private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
    String JK, jurusan;
     if (RbLakilaki.isSelected())
     {
     JK = "Laki-laki";
     }
     else
     {
     JK="Perempuan";
   
     }
     jurusan = Cbjurusan.getSelectedItem().toString();
   
    output.setText("NPM : " + tfNPM.getText() + "\n" + "Jenis Kelamin :"+ JK + "\n" +"Jurusan :"+ jurusan +"\n" );

}                                        

private void btbBatalActionPerformed(java.awt.event.ActionEvent evt) {                                        
// TODO add your handling code here:
         int mesage=JOptionPane. showConfirmDialog(null, "Apakah Anda Yakin Keluar");
     if (mesage==JOptionPane.OK_OPTION)
     System.exit(1);

}                                       

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

    /**
     * @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(biodata.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(biodata.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(biodata.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(biodata.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 biodata().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JComboBox Cbjurusan;
    private javax.swing.JRadioButton RbLakilaki;
    private javax.swing.JRadioButton RbPerempuan;
    private javax.swing.JButton btbBatal;
    private javax.swing.JButton btnSimpan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNPM;
    private javax.swing.JLabel lbljeniskelamin;
    private javax.swing.JLabel lbljurusan;
    private javax.swing.JTextArea output;
    private javax.swing.JTextField tfNPM;
    // End of variables declaration                  
}