Rabu, 04 Juli 2012

Event Headling

Event handler adalah satu atau lebih method yang dijalankan ketika sebuah event terjadi pada pemrograman menggunakan Windows Form Aplication. Misalnya, ketika kita mengklik sebuah tombol maka akan terjadi sebuah event, event itu bisa mengakibatkan suatu window atau jendela tertutup, membuka window atau jendela baru dan masih banyak lagi. Apabila kita menggunakan Microsoft Visual Studio, jenis-jenis event bisa kita lihat pada tab propertis. .

Berikut adalah contoh program menurut versi :

Versi C# :


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;



namespace EventhandlingManual

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

            this.button1.Click += new EventHandler(EvenHnd);

            this.button2.Click += new EventHandler(EvenHnd);

        }



        private void EvenHnd(object sender, EventArgs e)

        {

            Button button = sender as Button;

            if (button.Name == "button1")

            {

                MessageBox.Show("Tombol ! Success!!");

            }

            else if (button.Name == "button2")

            {

                MessageBox.Show("Tombol 2 Success!!");

            }
        }
    }
}
C# image

Versi Java :

package createhandling;

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class testButton {
    public void test() {
	//mendeklarasikan beberapa buton
	JButton btn1=new JButton("button1");
        btn1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                JOptionPane.showMessageDialog(null, "Tombol 1 Success!!");
            }
        });
	JButton btn2=new JButton("button2");
        btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                JOptionPane.showMessageDialog(null, "Tombol 2 Success!!");
            }
        });
        JPanel butonPanel=new JPanel();
	butonPanel.add(btn1);
	butonPanel.add(btn2);
	//coba//membuat pesan dengan label
	JLabel msg=new JLabel("contoh tombol",JLabel.CENTER);
	//membuat frame untuk menampilkan buton
	JFrame frame=new JFrame("form 1");
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	JRootPane rootPane=frame.getRootPane();
	rootPane.setDefaultButton(btn1);
	//membuat layout dan dispalay
	Container c=frame.getContentPane();
	c.add(msg,BorderLayout.CENTER);
	c.add(butonPanel,BorderLayout.SOUTH);
	frame.setSize(200, 200);
	frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
        testButton oye = new testButton();
        oye.test();
    }

}
Java image Link Download : Here !!!

Twitter Delicious Facebook Digg Stumbleupon Favorites More