Definition List

Selasa, 21 Oktober 2014

Tugas Kedua Pemrograman Berbasis Objek

1. Buatlah design form seperti berikut ini :



2. Input kode berikut:

import java.awt.Color;
import javax.swing.JOptionPane;

/**
 *
 * @author ronald
 */
public class PhoneBook extends javax.swing.JFrame {

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

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtnama = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txttlp = new javax.swing.JTextField();
        txtalamat = new javax.swing.JTextField();
        btnbatal = new javax.swing.JButton();
        btnsave = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtarea = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        btnedit = new javax.swing.JButton();
        btndelete = new javax.swing.JButton();
        btnkeluar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input Identitas"));

        jLabel1.setText("Nama");

        jLabel2.setText("Alamat");

        jLabel3.setText("Telepon");

        btnbatal.setText("Batal");
        btnbatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnbatalActionPerformed(evt);
            }
        });

        btnsave.setText("Simpan");
        btnsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });

       
        txtarea.setColumns(20);
        txtarea.setRows(5);
        jScrollPane1.setViewportView(txtarea);

        jLabel4.setText(" Nama        Alamat         Telepon");

        btnedit.setText("Ubah");
        btnedit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditActionPerformed(evt);
            }
        });

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

        btnkeluar.setText("Keluar");
        btnkeluar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnkeluarActionPerformed(evt);
            }
        });

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

    private void btnbatalActionPerformed(java.awt.event.ActionEvent evt) {
        txtnama.setText("");
        txtalamat.setText("");
        txttlp.setText("");
        txtnama.requestFocus();       
    }

 private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {
        String aku = "", ronald = "";
        aku = txtnama.getText();
        ronald = txtalamat.getText();
        if (aku.isEmpty() && ronald.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "Anda Belum mengisi Nama atau Alamat...", "Pesan System", WIDTH);
        } else {
            txtarea.append(txtnama.getText() + " -- " + txtalamat.getText() + " -- " + txttlp.getText() + "\n");
        }
        btnbatalActionPerformed(null);      

    }

 private void btneditActionPerformed(java.awt.event.ActionEvent evt) {
        String ronald = txtarea.getText();
        if (ronald.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "List Kosong...!", "Pesan System", WIDTH);
        } else {
            txtarea.isEditable();
            txtarea.requestFocus();
        }       
    }

    private void btndeleteActionPerformed(java.awt.event.ActionEvent evt) {
        String ronald = txtarea.getText();
        if (ronald.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "List Kosong Atau Anda belum memilih TEKS..", "Pesan System", WIDTH);
        } else {
            txtarea.cut();
        }       
    }

    private void btnkeluarActionPerformed(java.awt.event.ActionEvent evt) {
        JOptionPane.showMessageDialog(rootPane, "Terima Kasih", "Pesan System", WIDTH);
        System.exit(WIDTH);       
    }
private void intComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtnama = new javax.swing.JTextField();
        txtalamat = new javax.swing.JTextField();
        txttlp = new javax.swing.JTextField();
        btnsave = new javax.swing.JButton();
        btnbatal = new javax.swing.JButton();
        btnedit = new javax.swing.JButton();
        btndelete = new javax.swing.JButton();
        btnkeluar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtarea = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Simple Phone Book");
        setAlwaysOnTop(true);
        setName("frame"); // NOI18N
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input Identitas"));
        jLabel1.setText("Nama");
        jLabel2.setText("Alamat");
        jLabel3.setText("Telepon");

        txtnama.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtnamaActionPerformed(evt);
            }
        });
        txtnama.addFocusListener(new java.awt.event.FocusAdapter() {

            public void focusGained(java.awt.event.FocusEvent evt) {
                txtnamaFocusGained(evt);
            }

            public void focusLost(java.awt.event.FocusEvent evt) {
                txtnamaFocusLost(evt);
            }
        });
        txtnama.addKeyListener(new java.awt.event.KeyAdapter() {

            public void keyPressed(java.awt.event.KeyEvent evt) {
                loncat(evt);
            }

            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtnamaKeyReleased(evt);
            }
        });
        txtalamat.addFocusListener(new java.awt.event.FocusAdapter() {

            public void focusGained(java.awt.event.FocusEvent evt) {
                txtalamatFocusGained(evt);
            }

            public void focusLost(java.awt.event.FocusEvent evt) {
                txtalamatFocusLost(evt);
            }
        });
        txttlp.addFocusListener(new java.awt.event.FocusAdapter() {

            public void focusGained(java.awt.event.FocusEvent evt) {
                txttlpFocusGained(evt);
            }

            public void focusLost(java.awt.event.FocusEvent evt) {
                txttlpFocusLost(evt);
            }
        });
        btnbatal.setText("Batal");
        btnbatal.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnbatalActionPerformed(evt);
            }
        });
        btnsave.setText("Simpan");
        btnsave.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });
        btnedit.setText("Ubah");
        btnedit.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditActionPerformed(evt);
            }
        });

        btndelete.setText("Hapus");
        btndelete.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndeleteActionPerformed(evt);
            }
        });

        btnkeluar.setText("Keluar");
        btnkeluar.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnkeluarActionPerformed(evt);
            }
        });

        txtarea.setColumns(20);
        txtarea.setRows(5);
        txtarea.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtareaMouseClicked(evt);
            }

            public void mouseReleased(java.awt.event.MouseEvent evt) {
                txtareaMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(txtarea);

        jLabel4.setText("Nama               Alamat                Telepon");



        pack();
    }// </editor-fold>
 private void txtnamaActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

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

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

    private void txtnamaFocusLost(java.awt.event.FocusEvent evt) {
        // TODO add your handling code here:
        txtnama.setBackground(Color.white);
    }

    private void txtnamaFocusGained(java.awt.event.FocusEvent evt) {
        // TODO add your handling code here:
        txtnama.setBackground(Color.LIGHT_GRAY);
    }

    private void txtalamatFocusGained(java.awt.event.FocusEvent evt) {
        // TODO add your handling code here:
        txtalamat.setBackground(Color.LIGHT_GRAY);
    }

    private void txttlpFocusGained(java.awt.event.FocusEvent evt) {
        // TODO add your handling code here:
        txttlp.setBackground(Color.LIGHT_GRAY);
    }

    private void txtalamatFocusLost(java.awt.event.FocusEvent evt) {
        // TODO add your handling code here:
        txtalamat.setBackground(Color.white);
    }

    private void txttlpFocusLost(java.awt.event.FocusEvent evt) {
        // TODO add your handling code here:
        txttlp.setBackground(Color.white);
    }

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

    private void txtareaMouseReleased(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        txtarea.isEnabled();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PhoneBook().setVisible(true);
            }
        });
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify
    private javax.swing.JButton btnbatal;
    private javax.swing.JButton btndelete;
    private javax.swing.JButton btnedit;
    private javax.swing.JButton btnkeluar;
    private javax.swing.JButton btnsave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtalamat;
    private javax.swing.JTextArea txtarea;
    private javax.swing.JTextField txtnama;
    private javax.swing.JTextField txttlp;
    // End of variables declaration
}

3. Jalankan programnya dan hasilnya seperti berikut :
  

Selamat mencoba......