SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The BasicsJeff Fox
 
Validation Controls in asp.net
Validation Controls in asp.netValidation Controls in asp.net
Validation Controls in asp.netDeep Patel
 
Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Peter R. Egli
 
Static and dynamic scoping
Static and dynamic scopingStatic and dynamic scoping
Static and dynamic scopingNusratShaikh16
 
mini project in c using data structure
mini project in c using data structure mini project in c using data structure
mini project in c using data structure SWETALEENA2
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAhsanul Karim
 
Introduction to java beans
Introduction to java beansIntroduction to java beans
Introduction to java beansHitesh Parmar
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPTkamal kotecha
 
Java: The Complete Reference, Eleventh Edition
Java: The Complete Reference, Eleventh EditionJava: The Complete Reference, Eleventh Edition
Java: The Complete Reference, Eleventh Editionmoxuji
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)slire
 

Was ist angesagt? (20)

JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Validation Controls in asp.net
Validation Controls in asp.netValidation Controls in asp.net
Validation Controls in asp.net
 
Wlan architecture
Wlan architectureWlan architecture
Wlan architecture
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
Java
JavaJava
Java
 
Static and dynamic scoping
Static and dynamic scopingStatic and dynamic scoping
Static and dynamic scoping
 
mini project in c using data structure
mini project in c using data structure mini project in c using data structure
mini project in c using data structure
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
 
Joins And Its Types
Joins And Its TypesJoins And Its Types
Joins And Its Types
 
JVM
JVMJVM
JVM
 
Introduction to java beans
Introduction to java beansIntroduction to java beans
Introduction to java beans
 
Active x control
Active x controlActive x control
Active x control
 
Java threads
Java threadsJava threads
Java threads
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Java: The Complete Reference, Eleventh Edition
Java: The Complete Reference, Eleventh EditionJava: The Complete Reference, Eleventh Edition
Java: The Complete Reference, Eleventh Edition
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 

Ähnlich wie 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
 

Ähnlich wie 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
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
 
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
 

Mehr von 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