Belajar GUI hari pertama

Pada kesempatan kali ini kita akan membahas bagaimana cara membuat program GUI menggunkan net-beans.

Source code pada pembuatan program ini adalah sebagai berikut:

import javax.swing.JOptionPane;

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

/*
* Program1.java
*
* Created on Dec 5, 2011, 8:11:46 AM
*/
/**
*
* @author arima, sehat, dan Gantria
*/

public class Program1 extends javax.swing.JFrame {

/** Creates new form Program1 */
public Program1() {
super(“Program Pertama”);
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() {

jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jNama = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jHAlamat = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jNo = new javax.swing.JTextField();
jAlamat = new javax.swing.JTextField();
jHNama = new javax.swing.JTextField();
jHNo = new javax.swing.JTextField();
jToggleButton1 = new javax.swing.JToggleButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTextField1.setText(“jTextField1”);
jTextField1.setName(“jTextField1”);

jTextField2.setText(“jTextField2”);
jTextField2.setName(“jTextField2”);

jNama.setName(“jNama”);

jLabel1.setText(“Nama”);
jLabel1.setName(“jLabel1”);

jButton1.setText(“OK”);
jButton1.setName(“jButton1”);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jHAlamat.setName(“jHAlamat”);

jLabel2.setText(“No telepon”);
jLabel2.setName(“jLabel2”);

jLabel3.setText(“Alamat”);
jLabel3.setName(“jLabel3”);

jNo.setName(“jNo”);

jAlamat.setName(“jAlamat”);
jHNama.setName(“jHNama”);

jHNo.setName(“jHNo”);

jToggleButton1.setText(“jToggleButton1”);
jToggleButton1.setName(“jToggleButton1″);

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()
.addGap(107, 107, 107)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jHNama, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
.addComponent(jAlamat, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
.addComponent(jNo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
.addComponent(jNama, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
.addComponent(jHNo, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
.addComponent(jHAlamat, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
.addGap(56, 56, 56))
.addGroup(layout.createSequentialGroup()
.addGap(266, 266, 266)
.addComponent(jButton1)
.addGap(131, 131, 131)))
.addGap(278, 278, 278))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(65, 65, 65)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jNo, 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(jLabel3)
.addComponent(jAlamat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(17, 17, 17)
.addComponent(jHNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jHNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jHAlamat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(71, Short.MAX_VALUE))
);

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

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(jNama.getText().isEmpty()||jNo.getText().isEmpty()||jAlamat.getText().isEmpty())
{JOptionPane.showConfirmDialog(null,”Salah satu data tidak boleh kosong”);}
else{
jHNama.setText(jNama.getText());
jHNo.setText(jNo.getText());
jHAlamat.setText(jAlamat.getText());
jNama.setText(null);
jNo.setText(null);
jAlamat.setText(null);}
}

/**
* @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(Program1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Program1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Program1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Program1.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 Program1().setVisible(true);
}
});
}
// Variables declaration – do not modify
private javax.swing.JTextField jAlamat;
private javax.swing.JButton jButton1;
private javax.swing.JTextField jHAlamat;
private javax.swing.JTextField jHNama;
private javax.swing.JTextField jHNo;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jNama;
private javax.swing.JTextField jNo;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JToggleButton jToggleButton1;
// End of variables declaration
}

Algoritma :

pada listing program ini terdapat 3 label, 6 textfield, dan 1 button

3 label pertama dicetak sebagai label text berisikan :

1. jLabel1 = new javax.swing.JLabel(); // label ini bertuliskan Nama

2. jLabel2 = new javax.swing.JLabel(); // label ini bertuliskan No Telpon

3 . jLabel3 = new javax.swing.JLabel(); // label ini bertuliskan Alamat

6 textfield bervariabel kan :

1. jNama = new javax.swing.JTextField(); // TextField ini berfungsi sebagai input Nama

2. jNo = new javax.swing.JTextField(); // TextField ini berfungsi sebagai input No Telpon

3. jAlamat = new javax.swing.JTextField(); // TextField ini berfungsi sebagai input Alamat

4.  jHAlamat = new javax.swing.JTextField(); // TextField ini berfungsi sebagai output Alamat

5. jHNama = new javax.swing.JTextField(); // TextField ini berfungsi sebagai output Nama

6. jHNo = new javax.swing.JTextField(); // TextField ini berfungsi sebagai output No Telpon

1.  button berfungsi sebagai :

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(jNama.getText().isEmpty()||jNo.getText().isEmpty()||jAlamat.getText().isEmpty())
{JOptionPane.showConfirmDialog(null,”Salah satu data tidak boleh kosong”);}
else{
jHNama.setText(jNama.getText());
jHNo.setText(jNo.getText());
jHAlamat.setText(jAlamat.getText());
jNama.setText(null);
jNo.setText(null);
jAlamat.setText(null);}
}

pada listing button ini ketika di klik akan berfungsi untuk memindahkan nilai textfield nama, no telpon, dan alamat ke textfield hnama, hno telpon, dan halamat.

dan ketika nilai textfield nama, no telpon, alamat berpindah. Maka nilai textfield nama, no telpon, dan alamat di set null(kosong).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s