Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only...
-
Upload
adam-mason -
Category
Documents
-
view
215 -
download
0
Transcript of Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only...
![Page 1: Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only temporary carCount is a local variable good only while the.](https://reader035.fdocuments.in/reader035/viewer/2022081813/56649e915503460f94b968dd/html5/thumbnails/1.jpg)
Using ObjectsChapter 6
“CarCounter page 89 and 91
Local variables declared in a method only temporary
carCount is a local variable good only while the method exists. It is
an instance variable
![Page 2: Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only temporary carCount is a local variable good only while the.](https://reader035.fdocuments.in/reader035/viewer/2022081813/56649e915503460f94b968dd/html5/thumbnails/2.jpg)
Instantiation using random class
• Page 93 “RandomLines”
• Private Random randomPositions = new Random( );– xEnd = randomPositions.nextInt (100);– Using the method to generate a number from
the range 0 to 99
![Page 3: Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only temporary carCount is a local variable good only while the.](https://reader035.fdocuments.in/reader035/viewer/2022081813/56649e915503460f94b968dd/html5/thumbnails/3.jpg)
The Swing toolkit
• Imported as a Java library
• Components look the same on every platform (JButtons for example)
![Page 4: Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only temporary carCount is a local variable good only while the.](https://reader035.fdocuments.in/reader035/viewer/2022081813/56649e915503460f94b968dd/html5/thumbnails/4.jpg)
Events
• Action – like a button
• Change – adjust a slider
• “extends” current class (child) inherits from another class (parent)
![Page 5: Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only temporary carCount is a local variable good only while the.](https://reader035.fdocuments.in/reader035/viewer/2022081813/56649e915503460f94b968dd/html5/thumbnails/5.jpg)
Creating a JButton CarCounter extends JFame page 99
• Private JButton button;
• button = new JButton (“Car Enterng”);– Window.add(button);
• When button clicked actionListener called
![Page 6: Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only temporary carCount is a local variable good only while the.](https://reader035.fdocuments.in/reader035/viewer/2022081813/56649e915503460f94b968dd/html5/thumbnails/6.jpg)
General Guides for using objects
1. Determine what is to be imported
2. Select a constructor (with “new”) to be used
3. Add window, if needed
4. Use the instance via it’s methods
![Page 7: Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only temporary carCount is a local variable good only while the.](https://reader035.fdocuments.in/reader035/viewer/2022081813/56649e915503460f94b968dd/html5/thumbnails/7.jpg)
Page 102 SumTextFields uses JLabel class to display non-
changing text fields
• private JTextField number1Field, number2Field, sumField;
• JTextField class– A single line area to display text or input text– textField1 = new JTextField(15);– textField2 = new JTextField(“Hello!” ,15);– String s;– s = textField1.getText( );– textField1.setText(s);
![Page 8: Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only temporary carCount is a local variable good only while the.](https://reader035.fdocuments.in/reader035/viewer/2022081813/56649e915503460f94b968dd/html5/thumbnails/8.jpg)
JPanel class
• Used for drawing, or to hold objects such as buttons
• panel = new JPanel( );– Panel.setPreferedSize(new
Dimension(200,200));– window.add(panel);
![Page 9: Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only temporary carCount is a local variable good only while the.](https://reader035.fdocuments.in/reader035/viewer/2022081813/56649e915503460f94b968dd/html5/thumbnails/9.jpg)
Timer class
• Regularly spaced events
• timers, control speeds
• Not visible as Buttons are
![Page 10: Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only temporary carCount is a local variable good only while the.](https://reader035.fdocuments.in/reader035/viewer/2022081813/56649e915503460f94b968dd/html5/thumbnails/10.jpg)
TimerExamplepage 105
• Screenshot shows number of seconds
• Using timer.
![Page 11: Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only temporary carCount is a local variable good only while the.](https://reader035.fdocuments.in/reader035/viewer/2022081813/56649e915503460f94b968dd/html5/thumbnails/11.jpg)
JSlider“Raindrops” page 107
• private JSlider slider;
• slider = new JSlider(JSlider.HORIZONTAL, 200, 2000, 1000);
• window.add (slider);
• … gapField = new JTextField(10);
• gapField.setText(Integer.toString
• ( slider.getValue( ) ) );
![Page 12: Using Objects Chapter 6 “CarCounter page 89 and 91 Local variables declared in a method only temporary carCount is a local variable good only while the.](https://reader035.fdocuments.in/reader035/viewer/2022081813/56649e915503460f94b968dd/html5/thumbnails/12.jpg)
Summary
• The Java language has a vast number of classes which you can (and ought to) use.
• Do not write your own code without investigating the libraries first.
• You may check http://www.java.sun.com and search tutorials and references about the various classes