SlideShare una empresa de Scribd logo
1 de 22
Labels and buttons




http://improvejava.blogspot.in/
                                  1
Objectives

On completion of this period, you would be
able to know

• Control fundamentals
• Adding and removing controls
• Labels
• Buttons



              http://improvejava.blogspot.in/
                                                2
Recap

In the previous class, you have leant

• Displaying information within a window
   • Working with color and font




                  http://improvejava.blogspot.in/
                                                    3
Control Fundamentals
• The AWT supports the following types of
  controls:
  •   Labels
  •   Push buttons
  •   Check boxes
  •   Choice lists
  •   Lists
  •   Scroll bars
  •   Text editing
• These controls are subclasses of Component

                     http://improvejava.blogspot.in/   4
Adding and Removing Controls

• To include a control in a window, you must add it
  to the window
• To do this, you must first create an instance of
  the desired control
• Then add it to a window by calling add()
• The syntax is
  – Component add(Component compObj);
  – Here, compObj is an instance of the control that you
    want to add
  – A reference to compObj is returned

                   http://improvejava.blogspot.in/         5
Adding and Removing Controls                       contd..


• Sometimes you want to remove a control from a
  window when the control is no longer needed
• To do this, call remove( )
• It has this general form:
  – void remove(Component obj)
  – Here, obj is a reference to the control you want to
    remove
• You can remove all controls by calling
  removeAll( )

                    http://improvejava.blogspot.in/             6
Labels

• The easiest control to use is a label
• A label is an object of type Label, and it contains
  a string, which it displays
• Labels are passive controls that do not support
  any interaction
• with the user
• Label defines the following constructors:
   – Label( )
   – Label(String str)
   – Label(String str, int how)

                     http://improvejava.blogspot.in/   7
Labels                       contd..


• The first version creates a blank label
• The second version creates a label that contains
  the string specified by str
• This string is left-justified
• The third version creates a label that contains
  the string specified by str using the alignment
  specified by ‘how’



                 http://improvejava.blogspot.in/     8
Labels                    contd..


• The alignment is specified by one of these three
  constants
  – Label.LEFT
  – Label.RIGHT
  – Label.CENTER
• You can set or change the text in a label by
  using the setText( ) method
  – void setText(String str)



                    http://improvejava.blogspot.in/   9
Labels                    contd..


• You can obtain the current label by calling
  getText( )
  – String getText( )
• You can set the alignment of the string within the
  label by calling setAlignment()
  – void setAlignment(int how)
• To obtain the current alignment, call
  getAlignment( )
  – int getAlignment( )

                    http://improvejava.blogspot.in/   10
Labels               contd..
// Demonstrate Labels
import java.awt.*;
import java.applet.*;
/*                                                      The sample output is shown here
<applet code="LabelDemo" width=300
   height=200>
</applet>
*/
public class LabelDemo extends Applet {
    public void init() {
          Label one = new Label("One");
          Label two = new Label("Two");                       Fig. 70.1 LabelDemo
          Label three = new Label("Three");
          // add labels to applet window
          add(one);
          add(two);
          add(three);
    }
}                             http://improvejava.blogspot.in/                       11
Buttons

• The most widely used control is the push button

• A push button is a component that contains a
  label and that generates an event when it is
  pressed

• Push buttons are objects of type Button



                 http://improvejava.blogspot.in/   12
Buttons                 contd..


• Button defines these two constructors:
  –   Button( )
  –   Button(String str)
  –   The first version creates an empty button
  –   The second creates a button that contains str as a label
• After a button has been created, you can set its
  label by calling setLabel( )
  – void setLabel(String str)
  – Here, str becomes the new label for the button



                        http://improvejava.blogspot.in/          13
Handling Buttons
• Each time a button is pressed, an action event is
  generated
• This is sent to any listeners that previously registered
• Each listener implements the ActionListener interface
• That interface defines the actionPerformed() method,
  which is called when an event occurs
• We have to write our code in this method to handle the
  button’s event




                     http://improvejava.blogspot.in/         14
Handling Buttons                            contd..

// Demonstrate Buttons
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ButtonDemo" width=250 height=150>
</applet>
*/
public class ButtonDemo extends Applet implements ActionListener {
    String msg = "";
    Button yes, no, maybe;
    public void init() {
    yes = new Button("Yes");
    no = new Button("No");
    maybe = new Button("Undecided");
                        http://improvejava.blogspot.in/              15
Handling Buttons                           contd..

    add(yes);
    add(no);
    add(maybe);
    yes.addActionListener(this);
    no.addActionListener(this);
    maybe.addActionListener(this);
}




                    http://improvejava.blogspot.in/             16
Handling Buttons                           contd..

public void actionPerformed(ActionEvent ae) {
     String str = ae.getActionCommand();
     if(str.equals("Yes")) {
               msg = "You pressed Yes.";
     } else if(str.equals("No")) {
               msg = "You pressed No.";
     } else {
               msg = "You pressed Undecided.";
     }
     repaint();
}



                     http://improvejava.blogspot.in/             17
Handling Buttons                             contd..

    public void paint(Graphics g) {
         g.drawString(msg, 6, 100);
    }
}                                                  The sample output is shown here




                                                           Fig. 70.2 Handling Buttons



                         http://improvejava.blogspot.in/                            18
Summary

• AWT supports several GUI components
• In this class we have seen how to use
  – Labels
  – Buttons




              http://improvejava.blogspot.in/   19
Quiz

1. The event generated by button is
   – FocusEvent
   – ActionEvent
   – ContainerEvent
   – KeyEvent




                 http://improvejava.blogspot.in/   20
Quiz                      contd..

2. Which method is used get the caption of a label
   – getCaption()
   – getLabel()
   – getText()
   – None




                  http://improvejava.blogspot.in/             21
Frequently Asked Questions

1. List the constructors and methods of Label
2. List the constructors and methods of Button




                 http://improvejava.blogspot.in/   22

Más contenido relacionado

La actualidad más candente (20)

String in java
String in javaString in java
String in java
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
DOM and Events
DOM and EventsDOM and Events
DOM and Events
 
Java swing
Java swingJava swing
Java swing
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
JavaScript - Chapter 14 - Form Handling
 JavaScript - Chapter 14 - Form Handling   JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling
 
Interface
InterfaceInterface
Interface
 
Awt controls ppt
Awt controls pptAwt controls ppt
Awt controls ppt
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Java script errors &amp; exceptions handling
Java script  errors &amp; exceptions handlingJava script  errors &amp; exceptions handling
Java script errors &amp; exceptions handling
 
Java applet - java
Java applet - javaJava applet - java
Java applet - java
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 

Similar a Labels and buttons

Debugger & Profiler in NetBeans
Debugger & Profiler in NetBeansDebugger & Profiler in NetBeans
Debugger & Profiler in NetBeansHuu Bang Le Phan
 
Gui builder
Gui builderGui builder
Gui builderlearnt
 
awt controls .pptx
awt controls .pptxawt controls .pptx
awt controls .pptxAnsarTp1
 
Advance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTAdvance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTPayal Dungarwal
 
intro_gui
intro_guiintro_gui
intro_guifilipb2
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examplesAbdii Rashid
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Javasuraj pandey
 
Lists and scrollbars
Lists and scrollbarsLists and scrollbars
Lists and scrollbarsmyrajendra
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAhsanul Karim
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDrRajeshreeKhande
 
Unit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxUnit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxssuser10ef65
 
Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...
Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...
Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...Nagios
 
Checkbox and checkbox group
Checkbox and checkbox groupCheckbox and checkbox group
Checkbox and checkbox groupmyrajendra
 

Similar a Labels and buttons (20)

13457272.ppt
13457272.ppt13457272.ppt
13457272.ppt
 
Debugger & Profiler in NetBeans
Debugger & Profiler in NetBeansDebugger & Profiler in NetBeans
Debugger & Profiler in NetBeans
 
Gui builder
Gui builderGui builder
Gui builder
 
awt controls .pptx
awt controls .pptxawt controls .pptx
awt controls .pptx
 
17625-1.pptx
17625-1.pptx17625-1.pptx
17625-1.pptx
 
Gui
GuiGui
Gui
 
Advance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTAdvance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWT
 
intro_gui
intro_guiintro_gui
intro_gui
 
28 awt
28 awt28 awt
28 awt
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
 
Visualbasic tutorial
Visualbasic tutorialVisualbasic tutorial
Visualbasic tutorial
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
 
tL19 awt
tL19 awttL19 awt
tL19 awt
 
Lists and scrollbars
Lists and scrollbarsLists and scrollbars
Lists and scrollbars
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form Widgets
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
 
Unit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxUnit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptx
 
Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...
Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...
Nagios Conference 2013 - Jake Omann - Developing Nagios XI Components and Wiz...
 
Checkbox and checkbox group
Checkbox and checkbox groupCheckbox and checkbox group
Checkbox and checkbox group
 
Vb6.0 intro
Vb6.0 introVb6.0 intro
Vb6.0 intro
 

Más de myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Labels and buttons

  • 2. Objectives On completion of this period, you would be able to know • Control fundamentals • Adding and removing controls • Labels • Buttons http://improvejava.blogspot.in/ 2
  • 3. Recap In the previous class, you have leant • Displaying information within a window • Working with color and font http://improvejava.blogspot.in/ 3
  • 4. Control Fundamentals • The AWT supports the following types of controls: • Labels • Push buttons • Check boxes • Choice lists • Lists • Scroll bars • Text editing • These controls are subclasses of Component http://improvejava.blogspot.in/ 4
  • 5. Adding and Removing Controls • To include a control in a window, you must add it to the window • To do this, you must first create an instance of the desired control • Then add it to a window by calling add() • The syntax is – Component add(Component compObj); – Here, compObj is an instance of the control that you want to add – A reference to compObj is returned http://improvejava.blogspot.in/ 5
  • 6. Adding and Removing Controls contd.. • Sometimes you want to remove a control from a window when the control is no longer needed • To do this, call remove( ) • It has this general form: – void remove(Component obj) – Here, obj is a reference to the control you want to remove • You can remove all controls by calling removeAll( ) http://improvejava.blogspot.in/ 6
  • 7. Labels • The easiest control to use is a label • A label is an object of type Label, and it contains a string, which it displays • Labels are passive controls that do not support any interaction • with the user • Label defines the following constructors: – Label( ) – Label(String str) – Label(String str, int how) http://improvejava.blogspot.in/ 7
  • 8. Labels contd.. • The first version creates a blank label • The second version creates a label that contains the string specified by str • This string is left-justified • The third version creates a label that contains the string specified by str using the alignment specified by ‘how’ http://improvejava.blogspot.in/ 8
  • 9. Labels contd.. • The alignment is specified by one of these three constants – Label.LEFT – Label.RIGHT – Label.CENTER • You can set or change the text in a label by using the setText( ) method – void setText(String str) http://improvejava.blogspot.in/ 9
  • 10. Labels contd.. • You can obtain the current label by calling getText( ) – String getText( ) • You can set the alignment of the string within the label by calling setAlignment() – void setAlignment(int how) • To obtain the current alignment, call getAlignment( ) – int getAlignment( ) http://improvejava.blogspot.in/ 10
  • 11. Labels contd.. // Demonstrate Labels import java.awt.*; import java.applet.*; /* The sample output is shown here <applet code="LabelDemo" width=300 height=200> </applet> */ public class LabelDemo extends Applet { public void init() { Label one = new Label("One"); Label two = new Label("Two"); Fig. 70.1 LabelDemo Label three = new Label("Three"); // add labels to applet window add(one); add(two); add(three); } } http://improvejava.blogspot.in/ 11
  • 12. Buttons • The most widely used control is the push button • A push button is a component that contains a label and that generates an event when it is pressed • Push buttons are objects of type Button http://improvejava.blogspot.in/ 12
  • 13. Buttons contd.. • Button defines these two constructors: – Button( ) – Button(String str) – The first version creates an empty button – The second creates a button that contains str as a label • After a button has been created, you can set its label by calling setLabel( ) – void setLabel(String str) – Here, str becomes the new label for the button http://improvejava.blogspot.in/ 13
  • 14. Handling Buttons • Each time a button is pressed, an action event is generated • This is sent to any listeners that previously registered • Each listener implements the ActionListener interface • That interface defines the actionPerformed() method, which is called when an event occurs • We have to write our code in this method to handle the button’s event http://improvejava.blogspot.in/ 14
  • 15. Handling Buttons contd.. // Demonstrate Buttons import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="ButtonDemo" width=250 height=150> </applet> */ public class ButtonDemo extends Applet implements ActionListener { String msg = ""; Button yes, no, maybe; public void init() { yes = new Button("Yes"); no = new Button("No"); maybe = new Button("Undecided"); http://improvejava.blogspot.in/ 15
  • 16. Handling Buttons contd.. add(yes); add(no); add(maybe); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); } http://improvejava.blogspot.in/ 16
  • 17. Handling Buttons contd.. public void actionPerformed(ActionEvent ae) { String str = ae.getActionCommand(); if(str.equals("Yes")) { msg = "You pressed Yes."; } else if(str.equals("No")) { msg = "You pressed No."; } else { msg = "You pressed Undecided."; } repaint(); } http://improvejava.blogspot.in/ 17
  • 18. Handling Buttons contd.. public void paint(Graphics g) { g.drawString(msg, 6, 100); } } The sample output is shown here Fig. 70.2 Handling Buttons http://improvejava.blogspot.in/ 18
  • 19. Summary • AWT supports several GUI components • In this class we have seen how to use – Labels – Buttons http://improvejava.blogspot.in/ 19
  • 20. Quiz 1. The event generated by button is – FocusEvent – ActionEvent – ContainerEvent – KeyEvent http://improvejava.blogspot.in/ 20
  • 21. Quiz contd.. 2. Which method is used get the caption of a label – getCaption() – getLabel() – getText() – None http://improvejava.blogspot.in/ 21
  • 22. Frequently Asked Questions 1. List the constructors and methods of Label 2. List the constructors and methods of Button http://improvejava.blogspot.in/ 22