Form dan Event Handler

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.

</pre>
<b>using </b>System;

<b>using </b>System.Collections.Generic;

<b>using </b>System.ComponentModel;

<b>using </b>System.Data;

<b>using </b>System.Drawing;

<b>using </b>System.Linq;

<b>using </b>System.Text;

<b>using </b>System.Windows.Forms;

&nbsp;

<b>namespace </b>OOP2

{

<b>public </b>partial class Form1 : Form

{

<b>public </b><b>Form1</b>()

{

<b>InitializeComponent</b>();

}

&nbsp;

&nbsp;

<b>private </b>void <b>Form1_Load</b>(object sender, EventArgs e)

{

&nbsp;

}

&nbsp;

<b>private </b>void <b>label4_Click</b>(object sender, EventArgs e)

{

&nbsp;

}

&nbsp;

<b>private </b>void <b>textBox3_TextChanged</b>(object sender, EventArgs e)

{

&nbsp;

}

<b>int </b>bil1, bil2, jumlah, kurang;

<b>private </b>void <b>button3_Click</b>(object sender, EventArgs e)

{

bil1 = Convert.<b>ToInt32</b>(textBox1.Text);

&nbsp;

bil2= Convert.<b>ToInt32</b>(textBox2.Text);

&nbsp;

jumlah = bil1 + bil2;

textBox3.Text = jumlah.<b>ToString</b>();

}

&nbsp;

<b>private </b>void <b>button2_Click</b>(object sender, EventArgs e)

{

bil1 = Convert.<b>ToInt32</b>(textBox1.Text);

&nbsp;

bil2 = Convert.<b>ToInt32</b>(textBox2.Text);

&nbsp;

kurang = bil1 - bil2;

textBox3.Text = kurang.<b>ToString</b>();

}

}

}
<pre>

formc#1formc#2

</pre>
package oop2;

&nbsp;

/**

*

* @author indrawsp

*/

public class PerhitunganKalkulator extends javax.swing.JFrame {

&nbsp;

/** Creates new form Perhitungan_Kalkulator_Widy */

public PerhitunganKalkulator() {

initComponents();

}

&nbsp;

/** 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() {

&nbsp;

jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();

jLabel4 = new javax.swing.JLabel();

jTextField1 = new javax.swing.JTextField();

jTextField2 = new javax.swing.JTextField();

jTextField3 = new javax.swing.JTextField();

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

&nbsp;

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

&nbsp;

jLabel2.setText("Nilai1");

&nbsp;

jLabel3.setText("Nilai2");

&nbsp;

jLabel4.setText("Hasil");

&nbsp;

jTextField1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jTextField1ActionPerformed(evt);

}

});

&nbsp;

jTextField2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jTextField2ActionPerformed(evt);

}

});

&nbsp;

jTextField3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jTextField3ActionPerformed(evt);

}

});

&nbsp;

jButton1.setText("+");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

&nbsp;

jButton2.setText("--");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

}

});

&nbsp;

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(30, 30, 30)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel4)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)

.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jButton1)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel2)

.addComponent(jLabel3))

.addGap(34, 34, 34)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)

.addComponent(jTextField2)))

.addComponent(jButton2)))

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(40, 40, 40)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(30, 30, 30)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel3)

.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)

.addComponent(jButton1)

.addGap(3, 3, 3)

.addComponent(jButton2)

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel4)

.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

&nbsp;

pack();

}// </editor-fold>

&nbsp;

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}

&nbsp;

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}

&nbsp;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

int bil1,bil2,tambah,kurang;

String Hasil;

&nbsp;

bil1 = Short.valueOf(jTextField1.getText());

bil2 = Short.valueOf(jTextField2.getText());

tambah = bil1 + bil2;

Hasil = String.valueOf(tambah);

jTextField3.setText(Hasil);

}

&nbsp;

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

int bil1,bil2,tambah,kurang;

String Hasil;

&nbsp;

bil1 = Short.valueOf(jTextField1.getText());

bil2 = Short.valueOf(jTextField2.getText());

kurang = bil1 - bil2;

Hasil = String.valueOf(kurang);

jTextField3.setText(Hasil);

}

&nbsp;

private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}

&nbsp;

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Perhitungan_Kalkulator_Widy().setVisible(true);

}

});

}

&nbsp;

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JTextField jTextField1;

private javax.swing.JTextField jTextField2;

private javax.swing.JTextField jTextField3;

// End of variables declaration

&nbsp;

}

&nbsp;
<pre>

java1

java2

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