Advanced GUIs and Applets - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Advanced GUIs and Applets

Description:

Remove the method main ... Constructors and Methods of class JTextArea ... interface ItemListener and method itemStateChanged used to handle events ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 30
Provided by: manasibhat
Category:

less

Transcript and Presenter's Notes

Title: Advanced GUIs and Applets


1
  • Advanced GUIs and Applets

2
Inheritance Hierarchy of GUI Classes
3
Applets
  • Applet a Java program that is embedded within a
    Web page and executed by a Web browser
  • Create an applet by extending the class JApplet
  • class JApplet contained in package javax.swing

4
Members of class JApplet
5
Members of class JApplet
6
Applets
  • No main method
  • Methods init, start, and paint guaranteed to be
    invoked in sequence
  • To develop an applet
  • Override any/all of the methods above

7
Applet Methods
  • init Method
  • Initializes variables
  • Gets data from user
  • Places various GUI components
  • paint Method
  • Performs output

8
Skeleton of a Java Applet
import java.awt.Graphics import
javax.swing.JApplet public class WelcomeApplet
extends JApplet
9
Applet Displaying Welcome Message
//Welcome Applet import java.awt.Graphics impor
t javax.swing.JApplet public class
WelcomeApplet extends JApplet public
void paint(Graphics g)
super.paint(g)
//Line 1 g.drawString(Welcome to
Java Programming,
30, 30) //Line2

10
HTML to Run Applet
11
Differences Between Applets and GUI Applications
  • Applets
  • Derived from JApplet
  • No main method
  • Uses init method
  • Displayed by HTML
  • Sets title in HTML
  • Size set in HTML
  • Applet closes when HTML doc closes
  • GUI applications
  • class extends JFrame
  • Invokes main method
  • Uses constructors
  • Uses method setVisible
  • Uses setTitle method
  • Uses method setSize
  • Closes with Exit button

12
Converting a GUI Application to an Applet
  • Change JFrame to JApplet
  • Change constructor to method init
  • Remove method calls such as setVisible, setTitle,
    setSize
  • Remove the method main
  • If applicable, remove Exit button/all code
    associated with it (e.g. action listener)

13
Additional GUI Components
  • JTextArea
  • JCheckBox
  • JRadioButton
  • JComboBox
  • JList

14
JTextArea
  • Can collect multiple lines of input from user
  • Can display multiple lines of output
  • Pressing Enter key separates lines of text
  • Each line ends with newline character \n
  • Derived from class JTextComponent

15
Constructors and Methods of class JTextArea
16
Methods Inherited by class JTextArea from Parent
class JTextComponent
17
JCheckBox
  • User selects from predefined values
  • Example of a toggle button
  • Clicking JCheckBox generates item event
  • Use interface ItemListener and its abstract
    method itemStateChanged to handle event

18
Constructors and Methods of class JCheckBox
19
Constructors and Methods of class JCheckBox
20
Constructors and Methods of class JCheckBox
21
JRadioButton
  • Created same way as check boxes
  • Placed in content pane of applet
  • Forces user to select only one radion button at a
    time
  • You create a button group to group radio buttons
  • Generates an ItemEvent
  • interface ItemListener and method
    itemStateChanged used to handle events

22
Constructors and Methods of class JRadioButton
23
Constructors and Methods of class JRadioButton
24
Constructors and Methods of class JRadioButton
25
JComboBox
  • Commonly known as a drop-down list
  • Used to select an item from a list of
    possibilities
  • Generates an ItemEvent
  • Event monitored by ItemListener
  • ItemListener invokes method itemStateChanged

26
Constructors of class JComboBox
27
Applet with JCheckBox, JComboBox, and JRadioButton
28
Constructors of class JList
29
Methods of class JList
Write a Comment
User Comments (0)
About PowerShow.com