kali ini saya membuat program Berbasis Objek (Netbeans).
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* PhoneBook.java
*
* Created on 18 Okt 14, 7:50:36
*/
package pbo;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
/**
*
* @author Aris
*/
public class PhoneBook extends javax.swing.JFrame {
/** Creates new form PhoneBook */
public PhoneBook() {
initComponents();
}
/** Creates new form PhoneBook */
/** 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();
txtnama = new javax.swing.JTextField();
txtalamat = new javax.swing.JTextField();
txttlp = new javax.swing.JTextField();
btnbatal = new javax.swing.JButton();
btnsave = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
scrollPane1 = new java.awt.ScrollPane();
jScrollPane1 = new javax.swing.JScrollPane();
txtarea = new javax.swing.JTextArea();
jPanel3 = new javax.swing.JPanel();
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("Save");
btnsave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsaveActionPerformed(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()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtalamat)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(txttlp, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnbatal)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnsave))
.addComponent(txtnama))
.addContainerGap(18, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtnama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtalamat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txttlp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnbatal)
.addComponent(btnsave))
.addContainerGap(31, Short.MAX_VALUE))
);
jLabel4.setText("Nama Alamat Telepon");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap(21, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
.addContainerGap())
);
txtarea.setColumns(20);
txtarea.setRows(5);
jScrollPane1.setViewportView(txtarea);
scrollPane1.add(jScrollPane1);
btnedit.setText("Edit");
btnedit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btneditActionPerformed(evt);
}
});
btndelete.setText("Delete");
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);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnedit)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btndelete)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
.addComponent(btnkeluar)
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnedit)
.addComponent(btnkeluar)
.addComponent(btndelete))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(scrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(46, 46, 46))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(scrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(88, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnbatalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtnama.setText("");
txtalamat.setText("");
txttlp.setText("");
txtnama.requestFocus();
}
private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//pesan = JOptionPane.showInputDialog("Please enter your name");}
String saya="",aris="";
saya=txtnama.getText();
aris=txtalamat.getText();
if (saya.isEmpty()&&aris.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 btnkeluarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(rootPane, "Terima Kasih", "Pesan System", WIDTH);
System.exit(WIDTH);
}
private void btneditActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String Aris=txtarea.getText();
if (Aris.isEmpty()){JOptionPane.showMessageDialog(rootPane, "List Kosong...!", "Pesan System", WIDTH);}
else {
txtarea.isEditable();
txtarea.requestFocus();}
}
private void btndeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String Aris=txtarea.getText();
if (Aris.isEmpty()){JOptionPane.showMessageDialog(rootPane, "List Kosong Atau Anda belum memilih TEKS..", "Pesan System", WIDTH);}
else{txtarea.cut();}
}
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);
}
private void txtnamaFocusGained(FocusEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void txtnamaFocusLost(FocusEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
});
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);
}
private void loncat(KeyEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void txtnamaFocusLost(java.awt.event.FocusEvent evt) {
// TODO add your handling code here:
txtnama.setBackground(Color.white);
}
private void txtnamaKeyReleased(KeyEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
});
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);
}
private void txtalamatFocusGained(FocusEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void txtalamatFocusLost(FocusEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
});
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);
}
private void txttlpFocusGained(FocusEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void txttlpFocusLost(FocusEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
});
btnsave.setText("Save");
btnsave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsaveActionPerformed(evt);
}
private void btnsaveActionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
});
btnbatal.setText("Batal");
btnbatal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnbatalActionPerformed(evt);
}
private void btnbatalActionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel1).addComponent(jLabel2).addComponent(jLabel3)).addGap(18, 18, 18).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addGroup(jPanel1Layout.createSequentialGroup().addComponent(txttlp, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE).addGap(18, 18, 18).addComponent(btnbatal, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(btnsave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addComponent(txtnama, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE).addComponent(txtalamat, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)).addContainerGap()));
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1).addComponent(txtnama, 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(jLabel2).addComponent(txtalamat, 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(jLabel3).addComponent(btnsave).addComponent(txttlp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(btnbatal)).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
btnedit.setText("Edit");
btnedit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btneditActionPerformed(evt);
}
private void btneditActionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
});
btndelete.setText("Delete");
btndelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btndeleteActionPerformed(evt);
}
private void btndeleteActionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
});
btnkeluar.setText("Keluar");
btnkeluar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnkeluarActionPerformed(evt);
}
private void btnkeluarActionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
});
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);
}
private void txtareaMouseClicked(MouseEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void txtareaMouseReleased(MouseEvent evt) {
throw new UnsupportedOperationException("Not yet implemented");
}
});
}
private void txtnamaActionPerformed(java.awt.event.ActionEvent 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 txtalamatActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtalamat.setBackground(Color.LIGHT_GRAY);
}
private void txtnamaFocusGained(java.awt.event.FocusEvent evt) {
// TODO add your handling code here:
txtnama.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);
}
});
}
// 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.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private java.awt.ScrollPane scrollPane1;
private javax.swing.JTextField txtalamat;
private javax.swing.JTextArea txtarea;
private javax.swing.JTextField txtnama;
private javax.swing.JTextField txttlp;
// End of variables declaration
}
Hasilnya: