Form & Event Handing

Form adalah tampilan user interface yang disediakan oleh bahasa pmrograman agar mudah untuk menjalankan sebuah program. Event handler adalah sebuah method yang dapat mendeteksi suatu kejadian yang terjadi pada sebuah form, contoh ketika sebuah button di Click maka program akan melakukan sebuah eksekusi  perintah yang ada pada event handlernya.

Contoh Form & Event Handler pada C# :


using System;
 using System.Windows.Forms;

 namespace form_t
 {
     /// <summary>
     /// Class with program entry point.
     /// </summary>
     internal sealed class Program
     {
         /// <summary>
         /// Program entry point.
         /// </summary>
         [STAThread]
         private static void Main(string[] args)
         {
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.Run(new MainForm());
         }

     }
 }

Hasil Running :

form(C#)

Contoh Form & Event Handler pada JAVA :


/**
 *
 * @author tari
 */
public class formt extends javax.swing.JFrame {

/** Creates new form formt */
 public formt() {
 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();
 jLabel1 = new javax.swing.JLabel();
 jTextField2 = new javax.swing.JTextField();
 jTextField3 = new javax.swing.JTextField();
 jTextField4 = new javax.swing.JTextField();
 jButton1 = new javax.swing.JButton();
 jLabel2 = new javax.swing.JLabel();
 jLabel3 = new javax.swing.JLabel();

jTextField1.setText("jTextField1");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Nilai A");

jTextField2.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent evt) {
 jTextField2ActionPerformed(evt);
 }
 });

jButton1.setText("Tambah");
 jButton1.addActionListener(new java.awt.event.ActionListener() {
 public void actionPerformed(java.awt.event.ActionEvent evt) {
 jButton1ActionPerformed(evt);
 }
 });

jLabel2.setText("Nilai B");

jLabel3.setText("Hasil");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
 getContentPane().setLayout(layout);
 layout.setHorizontalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
 .addGroup(layout.createSequentialGroup()
 .addContainerGap()
 .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE))
 .addGroup(layout.createSequentialGroup()
 .addContainerGap()
 .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
 .addGroup(layout.createSequentialGroup()
 .addContainerGap()
 .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addComponent(jButton1)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
 .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING)
 .addComponent(jTextField3, javax.swing.GroupLayout.Alignment.LEADING)
 .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)))
 .addGap(79, 79, 79))
 );
 layout.setVerticalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addGap(51, 51, 51)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addGap(23, 23, 23)
 .addComponent(jButton1))
 .addComponent(jLabel2))
 .addGap(18, 18, 18)
 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addComponent(jLabel3))
 .addContainerGap(30, Short.MAX_VALUE))
 );

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

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
 // TODO add your handling code here:
 int a,b,c;
 String Hasil;
 a=Short.valueOf(jTextField2.getText());
 b=Short.valueOf(jTextField3.getText());
 c=a+b;
 Hasil=String.valueOf(c);
 jTextField4.setText(Hasil);
 }

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

/**
 * @param args the command line arguments
 */
 public static void main(String args[]) {
 java.awt.EventQueue.invokeLater(new Runnable() {
 public void run() {
 new formt().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.JTextField jTextField1;
 private javax.swing.JTextField jTextField2;
 private javax.swing.JTextField jTextField3;
 private javax.swing.JTextField jTextField4;
 // End of variables declaration

}

Hasil Running :

form(java)

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