Introducing, the JFrame - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Introducing, the JFrame

Description:

Three ways to make use of classes. One step instantiation: JFrame myWindow = new JFrame ... the Main class causes the constructor (if there is one) to be run. ... – PowerPoint PPT presentation

Number of Views:152
Avg rating:3.0/5.0
Slides: 43
Provided by: appliedsci3
Category:

less

Transcript and Presenter's Notes

Title: Introducing, the JFrame


1
Introducing, the JFrame
  • Gives us a work area beside System.out.println

2
Three ways to make use of classes
  • One step instantiation
  • JFrame myWindow new JFrame( )
  • Two step Instantiation
  • public JFrame myWindow
  • public static void method( int a)
  • myWindow new JFrame( )

3
third way
  • public class myClass extends JFrame
  • // your class inherits all of JFrames methods

4
Extension, Hierarchy, and Inheritance
5
Extension, Hierarchy, and Inheritance
  • Extends in the class statement, tells which
    larger class serves as the parent.
  • Inheritance the child class inherits methods
    and variables from the parent class
  • Hierarchy - parent to child relationship

6
Extend keyword
  • public class myClass extends someOtherClass
  • // I can use all the methods and
  • // variables from someOtherClass in myClass.

7
Other terms
  • Parent / Child
  • Base class / Sub class
  • Super Class / Sub class
  • Any class (not just an imported library class)
    can be a parent or base class.
  • Write classes then write new inherited classes
    for targeted capabilities.

8
Java common inherited classes
  • public class myClass extends JFrame
  • - myClass gets a whole bunch of methods for
    displaying windows on-screen
  • including the ability to launch a paint( )
    method.
  • public class myClass extends Applet
  • - applet and browser graphics
  • public class myClass extends Object
  • - the parent of ALL classes, usually omitted

9
  • import javax.swing.
  • public class MainClass extends JFrame
  • // stuff here
  • public static void main (String args)
  • MainClass application new MainClass( )

10
What is this?
  • Main method instantiates the class that its in?
  • Because main is not considered a member of the
    class that contains it.
  • The Main class serves a place-holding function
    for the computer, by keeping main in a usable
    place.
  • Instantiating the Main class causes the
    constructor (if there is one) to be run.
  • What other reasons?

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

12
Use these helper libraries
  • import javax.swing.JFrame // for JFrame
  • import java.awt. // for painting and animation
  • import java.awt.event. // for event
    handling, more later
  • import java.util.Random // always useful

13
some JFrame methods
  • setLocation(100,200)
  • setSize(250,250)
  • setVisible(true)
  • getContentPane( ) // gives us a context
  • // more later
  • setDefaultCloseOperation( EXIT_ON_CLOSE)

14
  • public class MainClass extends JFrame
  • public MainClass ( )
  • setLocation(100,200)
  • setSize(250,250)
  • setVisible(true)
  • // end MainClass constructor
  • public static void main (String args)
  • MainClass application new MainClass( )
  • // end main method
  • // end class

15
  • public class MainClass extends JFrame
  • public MainClass ( int x, int y )
  • setLocation( x, y )
  • setSize(250,250)
  • setVisible(true)
  • // end MainClass constructor
  • public static void main (String args)
  • MainClass application new MainClass(100,
    200)
  • // end main method
  • // end class

16
  • public class MainClass extends JFrame
  • public MainClass ( int x, int y )
  • setLocation( x, y )
  • setSize(250,250)
  • setVisible(true)
  • // end MainClass constructor
  • public static void main (String args)
  • MainClass appl1 new MainClass(100, 200)
  • MainClass appl2 new MainClass(200, 300)
  • // end main method
  • // end class

17
Demo
18
If you dont extend JFrame.
  • public class MainClass
  • public MainClass ( )
  • JFrame myFrame new JFrame( )
  • myFrame.setLocation(100,200)
  • myFrame.setSize(250,250)
  • myFrame.setVisible(true)
  • // end MainClass constructor
  • public static void main (String args)
  • MainClass application new MainClass( )
  • // end main method
  • // end class

19
Objects As Containers of Information
20
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

21
Color class
  • Like the String class, and Integer class, does
    not use the new keyword.
  • holds a color value (nothing more)
  • e.g Color boxColor new Color( )
  • boxColor Color.blue
  • or
  • Color boxColor Color.blue
  • then boxColor can be used to set System
    properties in Classes/Objects that need color
    (more later).

22
JColorChooser returns a Color object to the
caller
23
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 )

24
a clarification
  • Color boxColor Color.blue
  • or
  • Color boxColor new Color( )
  • boxColor Color.blueobject t to box Color.

25
Computer Memory Space
  • Color boxColor Color.blue

JFrame library
Color.blue
Color.green
Color.red
Color.pink
26
Computer Memory Space
  • Color boxColor new Color( )
  • boxColor Color.blue

Color.blue
Color.green
Color.red
Color.pink
ready for Color
Color.blue
27
the this qualifier
  • Means this object, the one that were in.
  • Used when a call to a method, especially in a
    child, needs to specify which object the method
    should act upon.
  • this always refers to an object of some type.

28
the super qualifier
  • refers to the parent class
  • the command super calls the parents constructor
  • super.MethodName can call any method in the
    parent.

29
A Random Number generator
  • import java.util.Random
  • .
  • .
  • .
  • Random myGen new Random( )
  • X myGen.nextInt( 50 )

30
Meaning
  • myGen is an Object of the Random class.
  • It has methods.
  • The nextInt( num ) method returns an int
    between 1 and num.

31
graphics on your computer
  • Since there are many computers (types,
    instances)....
  • and since the programs that you write should run
    anywhere....
  • then the programs that you write, should retrieve
    and use information native to the computer that
    it is running on.
  • This is called context awareness

32
The paint Method
  • Computer runs it once (you dont), but then you
    can run it by calling repaint().
  • The computer passes it an object of the Graphics
    Class, of its own making.

33
  • public void paint (Graphics g)
  • // the computer calls the paint program
  • // automatically whenever an event requires
  • // the screen to be redrawn

34
An event
  • The user does anything moves the mouse, moves
    the window, hits a key, etc.

35
Graphics!
  • g.setColor
  • g.fillRect
  • g.drawString
  • g.drawPolygon
  • hundreds of methods that use your computers
    graphics capability

36
demo
  • A Frank Lloyd Wright style generator

37
the Container class
  • Holds a whole window context frames, scroll
    bars, buttons, menus, pop-ups, and operator
    actions mouse clicks, dragging dropping.
  • Operator actions are called events.
  • JFrame objects must be placed in a container, to
    be of any use.
  • ContentPane is a special container that
    contains the current state of the computer.

38
Creating a ContentPane container
  • Container frameContainer new Container()
  • frameContainer getContentPane( )
  • frameContainer is now an event environment,
    which is an object that holds the current details
    of all screen, user, keyboard, mouse events and
    conditions, and further... is a place where we
    can build a GUI, compatible with the computers
    configuration (called a layered glass pane).

39
what does this do?
  • FlowLayout layout new FlowLayout( )
  • Container frameContainer new Container()
  • frameContainer getContentPane( )
  • frameContainer.setLayout( layout )

JFrame
Container
40
create a Container to hold local computer
contents e.g. a ContentPane
41
the Container controls everything
42
more JFrame and Container methods
  • setSize( w, h )
  • setLocation( x, y )
  • setDefaultCloseOperation( EXIT_ON_CLOSE )
  • Container myPC new Container()
  • myPC getContentPane()
  • FlowLayout layout new FlowLayout()
  • myPC.setLayout( layout )
  • JButton helloButton new JButton( "Hello" )
  • myPC.add( helloButton )
  • setVisible( true )
Write a Comment
User Comments (0)
About PowerShow.com