Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
M.SUJITHA,
II-M.SC(CS&IT),
Nadar Saraswathi College Of Arts and Science, Theni
 Java includes libraries to provide multi-platform
support for Graphic User Interface objects.
 Java's GUI components include labels, text fields,
text areas, buttons.
 The Abstract Windowing Toolkit (AWT) also
includes containers which includes these
components.
 Containers include frames (windows), canvases
which are used to draw and panels which are used
to group components.
 Panels and canvases are contained in frames while
buttons and other components can be placed either
directly on frames or in panels inside the frames.
 These GUI components are automatically drawn
whenever the window is drawn.
 These GUI components are handled using Java's
event model.
 When a user interacts with a component, an event is
generated by the component that you interact with.
 For each component of your program, the
programmer is required to designate one or more
objects to "listen" for events from that component.
 If the program has a button labelled "start" you
must assign one or more objects which will be notified
when a user clicks on the button.
BUTTONS
 Button is a class in package java.awt which
represents buttons on the screen.
 The constructor is: public Button(String label)
which, when executed, creates a button with "label"
printed on it.
 The button is large enough to display the label.
 There is also a parameter less constructor that
creates an unlabeled button.
 Buttons respond to a variety of messages.
 "Action Listener" to a button with the method:
public void addActionListener(ActionListener
listener);
ADDING BUTTONS TO A FRAME OR PANEL
 A command to add a button to a frame or panel:
 my Frame .add(start Button);
 This code is used in the constructor for the frame or
panel. add(start Button) or simply add(start Button).
 The class extends Frame, which is part of java.awt.
 The constructor for Frame takes a String parameter and
creates a window with the string in the title bar.
 The constructor for Button Demo calls the super class
constructor, and then sets the size of the new Frame to be 400
x 200.
 The set Layout command tells the new Frame that new
components should be added from left to right across the panel.
EXAMPLE CODING:
import java.awt.*;
public class Button Demo extends Frame
{
protected Button start Button, stop Button;
// Constructor sets features of the frame, creates
buttons, //
public Button Demo()
{
super("Button demo");
// calls Frame constructor //
which adds title to window setSize(400,200);
// sets the size of the window //
Grid Layout. setLayout(new FlowLayout());
// create two new buttons
labels start and stop startButton = new Button("Start");
stopButton = new Button("Stop");
add(startButton); //
add buttons to frame add(stopButton);
// create an object to listen to both buttons:
ButtonListener myButtonListener = new
ButtonListener();
startButton.addActionListener(myButtonListener);
stopButton.addActionListener(myButtonListener);
setVisible(true);
// Show the window on the screen. //
Trivial main program associated with ButtonDemo //
public static void main(String args[])
{
// Create an instance of Buttons ButtonDemo app = new
ButtonDemo()
}
}
ACTION LISTENERS FOR BUTTONS
 Objects which implement the Action Listener
interface are required to implement a method action
Performed which takes a parameter of type Action
Event.
 When an action occurs to a button, all objects
which have been added as Action Listener's for that
button are notified by calling their action Performed
method with a parameter with information on the
exact event that occurred.
 The system automatically creates the ActionEvent
object and sends it along to the listener.
 It is need to manually create an ActionEvent
object in this course.The most useful methods of
ActionEvent are
INNER CLASSES
A bit heavy to have to create a completely
separate class in order to create a listener for the
two buttons in our Button Demo class.
 Two alternatives are possible.
 1.One is to let the frame itself be the
Action Listener for the button.
 2.public class Button Demo extends Frame
implements Action Listener .
 This is the style suggested in Core Java for
handling action events in simple cases.
 There is another style which is almost as simple,
but more general. It involves the use of what are
called "inner classes".
EXAMPLE SOURCE CODE:
import java.awt.*;
import java.awt.event.*;
public class ButtonDemo extends Frame
{
protected Button startButton, stopButton; public ButtonDemo()
{
startButton.addActionListener(myButtonListener);
stopButton.addActionListener(myButtonListener);
}
public static void main(String args[])
{
{
if (source == startButton) System.out.println("Start button");
else if (source == stopButton) System.out.println("Stop button");
} } }
 ThetButton Listener is declared to be protected, it
can only be used inside the containing class, Button
Demo. The method Performed is still public. If it is
declared as protected.
 These nested classes would now be contained within
a single file named ButtonDemo.java.
 Another advantage of using nested classes is that
all of the instance variables (and methods) of the
outer class are visible inside the inner class.
 This can be handy for picking up information from
other components of the outer class.
OTHER GUI COMPONENTS
LABELS
 A Label is a very simple component which contains a
string.
 The constructors are public Label()
 // creates label with no text public Label//
 The methods available are public String getText()
 The return label text public void setText(String s)
 It sets the label text the user can change the text
in Labels.
TEXT FIELDS
 A TextField is a single line area that the user can
type into.
 It is a good way of getting text input from the
user.
 The constructors are public Text Field () .
 When the user types into a text field and then
hits the return or enter key, it generates an event
which can be handled by the same kind of Action
Listener used with Buttons.
 If for some reason the user likes to be notified
every time any change is made to the Text Field one
can associate a Text Listener to the field.
TEXT AREAS
 Text Area is a class that provides an area to hold
multiple lines of text.
 It is fairly similar to Text Field except that no
special event is generated by hitting the return key.
 The constructors are
public Text Area(int rows, int columns)
// create text area with rows, columns, and displaying s
Methods public void set Editable(booleans)
// if false the Text Area is not user editable public
String get Text() //
return text in Text Area public void set Text(String s)
// sets the text

More Related Content

GUI components in Java

  • 2.  Java includes libraries to provide multi-platform support for Graphic User Interface objects.  Java's GUI components include labels, text fields, text areas, buttons.  The Abstract Windowing Toolkit (AWT) also includes containers which includes these components.  Containers include frames (windows), canvases which are used to draw and panels which are used to group components.  Panels and canvases are contained in frames while buttons and other components can be placed either directly on frames or in panels inside the frames.
  • 3.  These GUI components are automatically drawn whenever the window is drawn.  These GUI components are handled using Java's event model.  When a user interacts with a component, an event is generated by the component that you interact with.  For each component of your program, the programmer is required to designate one or more objects to "listen" for events from that component.  If the program has a button labelled "start" you must assign one or more objects which will be notified when a user clicks on the button.
  • 4. BUTTONS  Button is a class in package java.awt which represents buttons on the screen.  The constructor is: public Button(String label) which, when executed, creates a button with "label" printed on it.  The button is large enough to display the label.  There is also a parameter less constructor that creates an unlabeled button.  Buttons respond to a variety of messages.  "Action Listener" to a button with the method: public void addActionListener(ActionListener listener);
  • 5. ADDING BUTTONS TO A FRAME OR PANEL  A command to add a button to a frame or panel:  my Frame .add(start Button);  This code is used in the constructor for the frame or panel. add(start Button) or simply add(start Button).  The class extends Frame, which is part of java.awt.  The constructor for Frame takes a String parameter and creates a window with the string in the title bar.  The constructor for Button Demo calls the super class constructor, and then sets the size of the new Frame to be 400 x 200.  The set Layout command tells the new Frame that new components should be added from left to right across the panel.
  • 6. EXAMPLE CODING: import java.awt.*; public class Button Demo extends Frame { protected Button start Button, stop Button; // Constructor sets features of the frame, creates buttons, // public Button Demo() { super("Button demo"); // calls Frame constructor // which adds title to window setSize(400,200); // sets the size of the window //
  • 7. Grid Layout. setLayout(new FlowLayout()); // create two new buttons labels start and stop startButton = new Button("Start"); stopButton = new Button("Stop"); add(startButton); // add buttons to frame add(stopButton); // create an object to listen to both buttons: ButtonListener myButtonListener = new ButtonListener();
  • 8. startButton.addActionListener(myButtonListener); stopButton.addActionListener(myButtonListener); setVisible(true); // Show the window on the screen. // Trivial main program associated with ButtonDemo // public static void main(String args[]) { // Create an instance of Buttons ButtonDemo app = new ButtonDemo() } }
  • 9. ACTION LISTENERS FOR BUTTONS  Objects which implement the Action Listener interface are required to implement a method action Performed which takes a parameter of type Action Event.  When an action occurs to a button, all objects which have been added as Action Listener's for that button are notified by calling their action Performed method with a parameter with information on the exact event that occurred.  The system automatically creates the ActionEvent object and sends it along to the listener.  It is need to manually create an ActionEvent object in this course.The most useful methods of ActionEvent are
  • 10. INNER CLASSES A bit heavy to have to create a completely separate class in order to create a listener for the two buttons in our Button Demo class.  Two alternatives are possible.  1.One is to let the frame itself be the Action Listener for the button.  2.public class Button Demo extends Frame implements Action Listener .  This is the style suggested in Core Java for handling action events in simple cases.  There is another style which is almost as simple, but more general. It involves the use of what are called "inner classes".
  • 11. EXAMPLE SOURCE CODE: import java.awt.*; import java.awt.event.*; public class ButtonDemo extends Frame { protected Button startButton, stopButton; public ButtonDemo() { startButton.addActionListener(myButtonListener); stopButton.addActionListener(myButtonListener); } public static void main(String args[]) { { if (source == startButton) System.out.println("Start button"); else if (source == stopButton) System.out.println("Stop button"); } } }
  • 12.  ThetButton Listener is declared to be protected, it can only be used inside the containing class, Button Demo. The method Performed is still public. If it is declared as protected.  These nested classes would now be contained within a single file named ButtonDemo.java.  Another advantage of using nested classes is that all of the instance variables (and methods) of the outer class are visible inside the inner class.  This can be handy for picking up information from other components of the outer class.
  • 13. OTHER GUI COMPONENTS LABELS  A Label is a very simple component which contains a string.  The constructors are public Label()  // creates label with no text public Label//  The methods available are public String getText()  The return label text public void setText(String s)  It sets the label text the user can change the text in Labels.
  • 14. TEXT FIELDS  A TextField is a single line area that the user can type into.  It is a good way of getting text input from the user.  The constructors are public Text Field () .  When the user types into a text field and then hits the return or enter key, it generates an event which can be handled by the same kind of Action Listener used with Buttons.  If for some reason the user likes to be notified every time any change is made to the Text Field one can associate a Text Listener to the field.
  • 15. TEXT AREAS  Text Area is a class that provides an area to hold multiple lines of text.  It is fairly similar to Text Field except that no special event is generated by hitting the return key.  The constructors are public Text Area(int rows, int columns) // create text area with rows, columns, and displaying s Methods public void set Editable(booleans) // if false the Text Area is not user editable public String get Text() // return text in Text Area public void set Text(String s) // sets the text