Java |
//All event listeners are designed by interface.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class EventDemo extends JFrame
{
public EventDemo()
{
JButton clickMe = new JButton();
setTitle("Event Demo");
clickMe.setText("Click Me");
//add event listener
clickMe.addActionListener(new EventHandler());
Container con = getContentPane();
//use layout manager to position a button
con.setLayout(new FlowLayout());
con.add(clickMe);
setSize(500,400);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE );
JOptionPane.showMessageDialog(EventDemo.this,
"I Just Started!");
}
//inner class to deal with event listener
class EventHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(EventDemo.this,
"You Clicked My Button!");
}
}
public static void main(String[] args)
{
new EventDemo();
}
}
|
|
C# |
//Events and delegates work together
using System;
using System.Drawing;
using System.Windows.Forms;
// custom delegate
public delegate void StartDelegate();
class EventDemo : Form
{
// custom event
public event StartDelegate StartEvent;
public EventDemo()
{
Button clickMe = new Button();
clickMe.Parent = this;
clickMe.Text = "Click Me";
clickMe.Location = new Point(
(ClientSize.Width - clickMe.Width) /2,
(ClientSize.Height - clickMe.Height)/2);
// an EventHandler delegate is assigned
// to the button's Click event
clickMe.Click += new EventHandler(OnClickMeClicked);
// our custom "StartDelegate" delegate is assigned
// to our custom "StartEvent" event.
StartEvent += new StartDelegate(OnStartEvent);
// fire our custom event
StartEvent();
}
// this method is called when the "clickMe" button is pressed
public void OnClickMeClicked(object sender, EventArgs ea)
{
MessageBox.Show("You Clicked My Button!");
}
// this method is called when the "StartEvent" Event is fired
public void OnStartEvent()
{
MessageBox.Show("I Just Started!");
}
static void Main(string[] args)
{
Application.Run(new EventDemo());
}
}
|
|