Download presentation
Presentation is loading. Please wait.
Published byClement Burke Modified over 9 years ago
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, it’s not just a variable, it’s 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 don’t 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
How is boxColor given the value “Color.blue” contained in the Jframe library? private Color boxColor; boxColor = new Color( ); boxColor = Color.blue; Color.pink Color.red Color.green Color.blue COMPOSITION: creates an Object… ready for Value ASSIGNMENT: Color.blue copied from Jframe library and assigned to boxColor Computer Memory Space JFrame library DEPENDENCY: reserves the name, sets public of private domain
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.