Saturday, 5 July 2014

GUI IN JAVA

FACE IT , you need to make GUIs ,if you are building applications that other people are going to use, you need a graphical interface. Command line apps are weak,inflexible and unfriendly  .In this post we will be learning about JFrame, Event Handling and Inner Classes.


IT ALL STARTS WITH A WINDOW!!

A JFrame is the object that represents a window on the screen. It's where you put all the interface things like buttons,chechkboxes,text fields and so on.You also have the options like minimizing,maximizing or closing the window.

  
MAKING A GUI IS EASY : SOME BASIC STEPS

(I) Make a frame (a JFrame)

        JFrame frame = new JFrame();

(II) Make a widget (button, textfield etc.)

        JButton button = new JButton ("click me") ;

(III) Add the widget to the frame 

       frame.getContentPane().add(button);

(IV) Display it (give it a size and make it visible)

      frame.setSize(300,300) ;
      frame.setVisible(true) ;


YOUR FIRST GUI :



WHEN YOU RUN IT :



    


 THAT'S A REALLY BIG BUTTON !!
     The button fills all the available space in the frame.
     Later we will see how to set button size.












NOW HOW TO GET THIS BUTTON DO SOMETHING ?

 We need

(I) A method to be called when the user clicks the button .
(II) A way to know when to trigger that method i.e. a way to know when the user clicks the button.

To know this click , is called getting a user event.. In Java the process of getting and handling a user event is called event handling.


If you want to know the user event then we need to IMPLEMENT A LISTENER ,so that you can LISTEN the event.A listener interface is the bridge between you (listener) and the event source(button).


->  The swing GUI components are the event sources . An event source can turn user actions like click a mouse , close a window into an event.

-> An event is represented as an object of a event class (contained in java.awt.event package).

-> An event source (a button) creates an event object whenever user does something like clicking and the listener gets to know about it through the object.



THE LISTENER :

-> If your class wants to know about the button action events then we implement the ActionListener interface.

-> We call its addActionListener(this) and pass a ActionListener reference , in this case you are the listener that is why we pass this.

-> The button needs a method to call you back , so we implement the actionPerformed() method of ActionListener.This is the only method that needs to be implemented.




WHEN WE RUN THIS PROGRAM :


WHEN WE CLICK BUTTON :




LISTENERS , SOURCES & EVENTS :

-> Your class is a listener so its job is to implement the interface,add itself as a listener for the button and provide event-handling.

-> Button as an event source ,its job is to accept listeners requests to add them, get events from the user and call the listeners event handling method .

-> Event object is the argument to the event call-back method and its job is to carry data about the event back to the listener.


No comments:

Post a Comment