Tuesday, April 17, 2012

Event handling in Java

programmerpro - Event handling is handling the concept of an action that occurred. So the program will run when something happens, for example, when the button is clicked, when the combo box is selected and so on. Java has some kind of Event Handling, one of which is the ActionListener class that handles the action of the button. Here is an example program:

display:





programm:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ClickMe extends JFrame implements ActionListener {
    private JButton tombol;

    public ClickMe() {
        super ("Event Handling");   

        Container container = getContentPane();
        container.setLayout(new FlowLayout());       

        tombol = new JButton ("Click Me!");
        tombol.addActionListener(this);
        container.add(tombol);       

        setSize (200,100);
        setVisible (true);
    }

    public static void main (String arg[]) {
        ClickMe test = new ClickMe();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed (ActionEvent e) {
        if (e.getSource() == tombol) {
            JOptionPane.showMessageDialog(null, "You click me, guys !!!");
        }
    }
}

Related Posts by Categories

No comments:

Post a Comment