Using classes - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Using classes

Description:

One step instantiation, Composition I. JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II. private JFrame myWindow; // this is called DEPENDENCY ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 17
Provided by: mik97
Category:

less

Transcript and Presenter's Notes

Title: Using classes


1
Using classes
2
  • One step instantiation, Composition I
  • JFrame myWindow new JFrame( )
  • Two step Instantiation, Composition II
  • private JFrame myWindow // this is called
    DEPENDENCY
  • public static void method( int a)
  • myWindow new JFrame( ) // CREATES

  • // COMPOSITION

3
JFrame
  • Puts graphics-capable windows on screen.
  • JOptionPane uses JFrame
  • Colors
  • Fonts
  • Drawings
  • A frame for intuitive GUI
  • Adds a paint method

4
JFrame
  • import javax.swing.JFrame
  • Methods
  • setLocation( x, y )
  • setSize( height, width )
  • setTitle(string)
  • setDefaultCloseOperation( EXIT_ON_CLOSE )
  • add( anotherObject )

5
A proper program
  • import javax.swing.
  • public class WindowDemo
  • private JFrame myWindow //
    DEPENDENCY
  • public WindowDemo() // all the work done
    in constructor JFrame myWindow new
    JFrame( ) // COMPOSITION
    myWindow.setLocation( 400, 200 )
    myWindow.setSize(200, 100)
    myWindow.setTitle("HELLO")
    myWindow.setDefaultCloseOperation(
    myWindow.EXIT_ON_CLOSE ) myWindow.setVisible(
    true ) public static void main(String
    args ) // not considered part of the class
    WindowDemo app new WindowDemo()
    // end main
  • //end class

6
JButton
  • import javax.swing.JButton
  • can be added to JFrame

7
String
  • yes, its not just a variable, its a class with
    methods.
  • String testStr new String( )
  • testStr JOptionPane.showInputDialog("enter a
    string")
  • System.out.println( testStr.concat("ing") )
  • System.out.println( testStr.toLowerCase() )
  • System.out.println( testStr.toUpperCase() )
  • System.out.println( testStr.indexOf( "s" ) )
  • System.out.println( testStr.replace('s', 'q'))
  • if ( testStr.equalsIgnoreCase("exit") true )
    System.out.println("exiting")
    System.exit(0)

8
Math class
  • look it up

9
Random class
  • look it up

10
Date class
  • look it up
  • Date today new Date()
  • System.out.println( today )

11
Objects As Containers of Information
12
Objects dont always DO things
  • Sometimes they just HOLD things (information)
  • The Color class can hold color information
  • The Point class can hold cartesian coordinate
    information
  • The Container class contains 10,000 variables
    that describe your computer

13
Color class
  • holds a color value (nothing more)
  • e.g Color boxColor // DEPENDENCY
  • boxColor new Color( ) //
    COMPOSITION
  • boxColor Color.blue // ASSIGNMENT
  • then boxColor can be used to set System
    properties in Classes/Objects that need color
    (more later).

14
JColorChooser returns a Color object to the
caller
15
Returns an Object?
  • JColorChooser fills in all of the information in
    a blank object of the Color class, and copies it
    to the Color object in the calling statement
  • boxColor JColorChooser.showDialog(
  • null, Greeting, default color )

16
Computer Memory Space
  • How is boxColor given the value Color.blue
  • contained in the Jframe library?
  • private Color boxColor
  • boxColor new Color( )
  • boxColor Color.blue

JFrame library
Color.blue
Color.green
Color.red
Color.pink
DEPENDENCY reserves the name, sets public of
private domain
COMPOSITION creates an Object ready for Value
ASSIGNMENT Color.blue copied from Jframe
library and assigned to boxColor
Write a Comment
User Comments (0)
About PowerShow.com