Selasa, 10 Januari 2012

Membuat Daftar Nilai Pada Java


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DaftaNilai.java
 *
 * Created on Nov 17, 2011, 5:37:14 PM
 */
package view;

import javax.swing.JOptionPane;

/**
 *
 * @author IscaKg
 */
public class DaftaNilai extends javax.swing.JFrame {

    public int nilai[][];
    public int datake;

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

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtNt = new javax.swing.JTextField();
        txtNa = new javax.swing.JTextField();
        txtNuts = new javax.swing.JTextField();
        txtNuas = new javax.swing.JTextField();
        txtNr = new javax.swing.JTextField();
        lblDatake = new javax.swing.JLabel();
        btnNew = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnShow = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        btnRelease = new javax.swing.JButton();

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

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("DAFTAR NILAI");

        jLabel2.setText("Nilai Tugas");

        jLabel3.setText("Nilai Normatif");

        jLabel4.setText("Nilai UTS");

        jLabel5.setText("Nilai UAS");

        jLabel6.setText("Nilai Rata-rata");

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

        lblDatake.setText("Data Ke :");

        btnNew.setText("New");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnShow.setText("Show");
        btnShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowActionPerformed(evt);
            }
        });

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnRelease.setText("Release Array");
        btnRelease.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReleaseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(30, 30, 30)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtNt, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                            .addComponent(txtNa)
                            .addComponent(txtNuts)
                            .addComponent(txtNuas)
                            .addComponent(txtNr)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnRelease))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(142, 142, 142)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblDatake, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnNew)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnSave)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnShow)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnClose)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(txtNt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtNuts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtNuas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtNr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addComponent(lblDatake)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNew)
                    .addComponent(btnSave)
                    .addComponent(btnShow)
                    .addComponent(btnClose))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRelease)
                .addContainerGap(19, 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(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

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

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

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        // TODO add your handling code here:
        this.setTitle("From data nilai");
        this.setSize(350, 360);
        this.setLocationRelativeTo(null);
        this.setVisible(true);

        txtNt.setEnabled(false);
        txtNa.setEnabled(false);
        txtNuas.setEnabled(false);
        txtNuts.setEnabled(false);
        txtNr.setEnabled(false);

        btnSave.setEnabled(false);
        btnNew.setEnabled(false);
        btnShow.setEnabled(false);

        btnNew.setMnemonic('N');
        btnSave.setMnemonic('S');
        btnShow.setMnemonic('C');
        btnRelease.setMnemonic('P');

        btnRelease.requestFocus();
    }                                

    private void btnReleaseActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        int jumlahdata = Integer.parseInt(JOptionPane.showInputDialog(this, "Masukan nilai"));
        nilai = new int[jumlahdata][4];
        datake = 0;
        btnSave.setEnabled(false);
        btnNew.setEnabled(true);
        btnShow.setEnabled(true);
    }                                         

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        datake = datake + 1;

        if (datake > nilai.length) {
            JOptionPane.showMessageDialog(this, "Array tidak cukup");
        } else {
            lblDatake.setText("Data ke :" + (datake));
            txtNt.setEnabled(true);
            txtNa.setEnabled(true);
            txtNuts.setEnabled(true);
            txtNuas.setEnabled(true);
            btnSave.setEnabled(true);
            txtNt.setText("");
            txtNa.setText("");
            txtNuts.setText("");
            txtNuas.setText("");
        }
    }                                     

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        String nt = txtNt.getText();
        String na = txtNa.getText();
        String nuts = txtNuts.getText();
        String nuas = txtNuas.getText();

        if ((nt.isEmpty()) | (na.isEmpty()) | (nuts.isEmpty())) {
            JOptionPane.showMessageDialog(this, "Masih ada nilai yang kosong");
            txtNt.requestFocus();
        } else {
            nilai[(datake - 1)][0] = Integer.parseInt(txtNt.getText());
            nilai[(datake - 1)][1] = Integer.parseInt(txtNa.getText());
            nilai[(datake - 1)][2] = Integer.parseInt(txtNuts.getText());
            nilai[(datake - 1)][3] = Integer.parseInt(txtNuas.getText());
            btnShow.setEnabled(true);
            txtNt.setText("");
            txtNa.setText("");
            txtNuts.setText("");
            txtNuas.setText("");
        }

    }                                      

    private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        int jumlahdata = Integer.parseInt(JOptionPane.showInputDialog(this, "Masukan no input"));
        if ((jumlahdata > nilai.length) | (jumlahdata <= 0)) {
            jumlahdata = 1;
        }
        lblDatake.setText("Data ke : " + (jumlahdata));
        txtNt.setText(new Integer(nilai[jumlahdata - 1][0]).toString());
        txtNa.setText(new Integer(nilai[jumlahdata - 1][1]).toString());
        txtNuts.setText(new Integer(nilai[jumlahdata - 1][2]).toString());
        txtNuas.setText(new Integer(nilai[jumlahdata - 1][3]).toString());
    }                                      

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        System.exit(0);
    }                                       
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DaftaNilai().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnRelease;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnShow;
    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.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblDatake;
    private javax.swing.JTextField txtNa;
    private javax.swing.JTextField txtNr;
    private javax.swing.JTextField txtNt;
    private javax.swing.JTextField txtNuas;
    private javax.swing.JTextField txtNuts;
    // End of variables declaration                  
}

0 komentar:

Poskan Komentar