1. Untuk membuat sebuah form, pertama-tama buatlah design form seperti gambar berikut:
2. Kemudian input kode berikut untuk setiap object:
package Form;
import java.awt.Color;
import javax.swing.JOptionPane;
/**
*
* @author Natan
*/
public class PhoneBook extends javax.swing.JFrame {
/** Creates new form PhoneBook */
public PhoneBook() {
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();
txtnama = new javax.swing.JTextField();
txtalamat = new javax.swing.JTextField();
txttlp = new javax.swing.JTextField();
btnsimpan = new javax.swing.JButton();
btnbatal = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
txtarea = new javax.swing.JTextArea();
btnubah = new javax.swing.JButton();
btnhapus = 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");
btnsimpan.setText("Simpan");
btnsimpan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsimpanActionPerformed(evt);
}
});
btnbatal.setText("Batal");
btnbatal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnbatalActionPerformed(evt);
}
});
jLabel4.setText("Nama Alamat Telepon");
txtarea.setColumns(20);
txtarea.setRows(5);
jScrollPane1.setViewportView(txtarea);
btnubah.setText("Ubah");
btnubah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnubahActionPerformed(evt);
}
});
btnhapus.setText("Hapus");
btnhapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnhapusActionPerformed(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 btnkeluarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(rootPane, "Terima Kasih, Sampai jumpa...", "Pesan System", WIDTH);
System.exit(WIDTH);
}
private void btnhapusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String niel=txtarea.getText();
if (niel.isEmpty()){JOptionPane.showMessageDialog(rootPane, "List Kosong Atau Anda belum memilih TEKS..", "Pesan System", WIDTH);}
else{txtarea.cut();}
}
private void btnbatalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtnama.setText("");
txtalamat.setText("");
txttlp.setText("");
txtnama.requestFocus();
}
private void btnubahActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String dani=txtarea.getText();
if (dani.isEmpty()){JOptionPane.showMessageDialog(rootPane, "List Kosong...!", "Pesan System", WIDTH);}
else {
txtarea.isEditable();
txtarea.requestFocus();}
}
private void btnsimpanActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String aku="",natan="";
aku=txtnama.getText();
natan=txtalamat.getText();
if (aku.isEmpty()&&natan.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 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();
btnsimpan = new javax.swing.JButton();
btnbatal = new javax.swing.JButton();
btnubah = new javax.swing.JButton();
btnhapus = 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() {
@Override
public void focusGained(java.awt.event.FocusEvent evt) {
txtnamaFocusGained(evt);
}
@Override
public void focusLost(java.awt.event.FocusEvent evt) {
txtnamaFocusLost(evt);
}
});
txtnama.addKeyListener(new java.awt.event.KeyAdapter() {
@Override
public void keyPressed(java.awt.event.KeyEvent evt) {
loncat(evt);
}
@Override
public void keyReleased(java.awt.event.KeyEvent evt) {
txtnamaKeyReleased(evt);
}
});
txtalamat.addFocusListener(new java.awt.event.FocusAdapter() {
@Override
public void focusGained(java.awt.event.FocusEvent evt) {
txtalamatFocusGained(evt);
}
@Override
public void focusLost(java.awt.event.FocusEvent evt) {
txtalamatFocusLost(evt);
}
});
txttlp.addFocusListener(new java.awt.event.FocusAdapter() {
@Override
public void focusGained(java.awt.event.FocusEvent evt) {
txttlpFocusGained(evt);
}
@Override
public void focusLost(java.awt.event.FocusEvent evt) {
txttlpFocusLost(evt);
}
});
btnsimpan.setText("Simpan");
btnsimpan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnsimpanActionPerformed(evt);
}
});
btnbatal.setText("Batal");
btnbatal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnbatalActionPerformed(evt);
}
});
btnubah.setText("Ubah");
btnubah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnubahActionPerformed(evt);
}
});
btnhapus.setText("Hapus");
btnhapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnhapusActionPerformed(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() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
txtareaMouseClicked(evt);
}
@Override
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();
}
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 btnhapus;
private javax.swing.JButton btnkeluar;
private javax.swing.JButton btnsimpan;
private javax.swing.JButton btnubah;
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.....!!!!!
Selasa, 21 Oktober 2014
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar