Java GUI – Program Luas Persegi

Membuat program luas persegi dengan java GUI

Langkah – langkah perancangan pembuatan program luas persegi dengan java GUI yaitu :

  1. Buka Aplikasi Netbeans
  2. Klik File  à New Project à Java  Desktop Aplications à Next à Finish
  3. Selanjutnya pilih menu properti untuk merancang form aplikasi sesuai dengan kebutuhan program

Adapun form program luas persegi adalah sebagai berikut :

Properti yang dipakai :

  • Label  (Untuk Menulis Label atau nama dari komponen form)
  • Text Field (Untuk Menampilkan data atau tempat untuk menginputkan data)
  • JBotton (Tombol yang di gunakan untuk mengeksekusi program, atau untuk menginputkan koding program yang akan dibuat dengan cara klik kanan pada button dan pilih event > action kemudian pilih action performed )

LISTING PROGRAM


/*
 * Persegi.java
 *
 * Created on Dec 5, 2011, 1:38:53 PM
 */
/**
 *
 * @author Ade
 */
public class Persegi extends javax.swing.JFrame {

private static class str {

public str() {
 }
 }

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

jLabel1 = new javax.swing.JLabel();
 jLabel2 = new javax.swing.JLabel();
 jLabel3 = new javax.swing.JLabel();
 jLabel4 = new javax.swing.JLabel();
 txtpj = new javax.swing.JTextField();
 txtlbr = new javax.swing.JTextField();
 txtluas = new javax.swing.JTextField();
 jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
 jLabel1.setText("MENGHITUNG LUAS PERSEGI");
 jLabel1.setName("jLabel1"); // NOI18N

jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
 jLabel2.setText("PANJANG");
 jLabel2.setName("jLabel2"); // NOI18N

jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
 jLabel3.setText("LEBAR");
 jLabel3.setName("jLabel3"); // NOI18N

jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
 jLabel4.setText("LUAS PERSEGI");
 jLabel4.setName("jLabel4"); // NOI18N

txtpj.setName("txtpj"); // NOI18N

txtlbr.setName("txtlbr"); // NOI18N

txtluas.setName("txtluas"); // NOI18N

jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
 jButton1.setText("HITUNG");
 jButton1.setName("jButton1"); // NOI18N
 jButton1.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent evt) {
 jButton1ActionPerformed(evt);
 }
 });

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
 getContentPane().setLayout(layout);
 layout.setHorizontalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addGap(19, 19, 19)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addComponent(jLabel2)
 .addComponent(jLabel3)
 .addComponent(jLabel4))
 .addGap(18, 18, 18)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addComponent(jLabel1)
 .addContainerGap())
 .addGroup(layout.createSequentialGroup()
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addComponent(txtluas, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
 .addComponent(txtlbr, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
 .addComponent(txtpj, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
 .addComponent(jButton1))
 .addGap(125, 125, 125))))
 );
 layout.setVerticalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addGap(11, 11, 11)
 .addComponent(jLabel1)
 .addGap(18, 18, 18)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 .addComponent(jLabel2)
 .addComponent(txtpj, 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.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addComponent(txtlbr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
 .addComponent(txtluas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
 .addGroup(layout.createSequentialGroup()
 .addComponent(jLabel3)
 .addGap(18, 18, 18)
 .addComponent(jLabel4)))
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
 .addComponent(jButton1))
 );

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

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
 int panjang=Integer.parseInt(txtpj.getText());
 int lebar=Integer.parseInt(txtlbr.getText());

 int hasil= (panjang*lebar);
 txtluas.setText(String.valueOf(hasil));

}

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(Persegi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
 } catch (InstantiationException ex) {
 java.util.logging.Logger.getLogger(Persegi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
 } catch (IllegalAccessException ex) {
 java.util.logging.Logger.getLogger(Persegi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
 java.util.logging.Logger.getLogger(Persegi.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 Persegi().setVisible(true);
 }
 });
 }
 // Variables declaration - do not modify
 private javax.swing.JButton jButton1;
 private javax.swing.JLabel jLabel1;
 private javax.swing.JLabel jLabel2;
 private javax.swing.JLabel jLabel3;
 private javax.swing.JLabel jLabel4;
 private javax.swing.JTextField txtlbr;
 private javax.swing.JTextField txtluas;
 private javax.swing.JTextField txtpj;
 // End of variables declaration
}

Tampilan Output Program :

About Ade Akbar

Hidup, Berarti dan Mati

Posted on Desember 5, 2011, in Program Java. Bookmark the permalink. Tinggalkan komentar.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: