Download presentation
Presentation is loading. Please wait.
Published byJanae Bursell Modified over 9 years ago
1
Introduction to Java Classes, events, GUI’s
2
Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven programming works How classes inherit methods Overall program structure
3
Major Example
4
Red colorMe GreenBlue darkerreSize
5
RedGreenBlue Each color is mixture of amounts of Red, Green, and Blue ( 200, 50 125 ) values in range: 0 … 255
6
Illustrate with Rectangle object Class definition of rectangle including constructor, properties, methods Test program that creates instance of rectangle Classes or Objects in Java
7
Class Rectangle int height, width ; { } public Rectangle (int h, int w ) { } height = h; width = w; int findArea ( ) { } return height * width ; instance variables or properties constructor Class blueprint int getHght ( ) { return height } methods
8
public class TestRectangle { } public static void main ( String [ ] args ) { } Rectangle r ; r = new Rectangle (2, 4) ; int area = r.findArea ( ); System.out.println ( "Area is: " + area ) Test program instance r of rectangle saved in file TestRectangle say: r’s findArea constructor
9
public class Rectangle { } public static void main ( String [ ] args ) { } Rectangle r ; r = new Rectangle (2, 4) ; int area = r.findArea ( ); System.out.println ( "Area is: " + area ) ; another instance System.out.println ( "Area is: " + s.findArea ( ) ); Rectangle s = new Rectangle (5, 10) ;
10
Major Example
11
Red colorMe GreenBlue darkerreSize object Program Design
12
Design object GUI components Recognize & handle events Make Test program to create instances
13
GUI design Build object on top of frame class Add panel to frame Put GUI elements in frame: Add labels, textfields, & buttons Make object listen for clicks on buttons inherit properties of frames
14
Red colorMe GreenBlue darkerreSize object labelsbuttons text fields panel
15
Event-handler design Recognize source of event Take appropriate action - grab data from textfield - convert data to integer - use value to make RGB color - RGB: ( r, g, b ) - change color of panel Asynchronously called
16
Program Structure for Demo
17
import java.awt.*; public class TestColorizer { } public static void main (String[ ] args) { … } class Colorizer { }{ } public void actionPerformed(ActionEvent e) { … } public Colorizer( ) { … } private JTextField Num1 … ; implements ActionListener extends JFrame Program Structure
18
class Colorizer { }{ } implements ActionListener extends JFrame private JTextField Num1 … ; Instance variables public Colorizer( ) { … } constructor public void actionPerformed(ActionEvent e) { … } Event-handler Class
19
Content Pane X title bar JFrame
20
X Red colorMe GreenBlue darkerreSize Colorizer object extends Jframe
21
JFrame Title Bar Content PanePanel JTextFieldJLabelJButton
22
public Colorizer ( ) { } p1 = new JPanel ( ); this.getContentPane().add(p1); p1.add(new JLabel(“Red")); p1.add(Num1 = new JTextField(3 )); p1.add(colorMe = new JButton("ColorMe")); colorMe.addActionListener(this ); objects Constructor
23
public Colorizer ( ) { } p1 = new JPanel ( ); this.getContentPane().add(p1); p1.add(new JLabel(“Red")); p1.add(Num1 = new JTextField(3 )); p1.add(colorMe = new JButton("ColorMe")); colorMe.addActionListener(this ); named so can refer to in event-handler Constructor un-named since not referred to in event- handler system listens for button click
24
class Colorizer { }{ } public Colorizer( ) { … } private JTextField Num1 ; implements ActionListener extends JFrame private JButton colorMe ; private JPanel p1 ; public void actionPerformed(ActionEvent e) { … } instance variables Class…again
25
if (e.getSource() == colorMe ) { } public void actionPerformed (ActionEvent e) { } p1.setBackground ( c ); Integer.parseInt ( );int R =Num1.getText( ) recognize event source Get & convert number c = new Color (R, 100, 150); Create color Event-handler method signature
26
import java.awt.*; public class TestColorizer { } public static void main (String[ ] args) { } Colorizer f = new Colorizer( ); f.setSize (400,400); f.setVisible(true); import javax.swing.*; import java.awt.event.*; create instance of object use inherited methods Test program
27
public class TestColorizer { } public static void main (String[ ] args) { } Colorizer f = new Colorizer( ); f.setSize (400,400); f.setVisible(true); import java.awt.*; import javax.swing.*; import java.awt.event.*; { }{ } class Colorizer implements ActionListener extends JFrame if (e.getSource() == colorMe ) { } c = new Color (R, 100, 150); p1.setBackground ( c ); Integer.parseInt ( );int R =Num1.getText( ) public void actionPerformed (ActionEvent e) { } private JTextField Num1 ; private JButton colorMe; private JPanel p1 ; private Color c ; p1 = new JPanel ( ); this.getContentPane().add(p1); p1.add(new JLabel(“Red")); p1.add(Num1 = new JTextField(3 )); p1.add(colorMe = new JButton("ColorMe")); colorMe.addActionListener(this ); public Colorizer ( ) { } prototype program
28
Outline Classes in Java Rectangle example propertiesor instance variables instances of class constructor for class Demo Colorizer Example compilationexecution RGB representationpanel color buttonsdarken-buttonresize-button Colorizer class main program TextPad
29
Colorizer Class Design program structure constructor event-handler GUI setup constructor recognize event-source grab text make Color reset background color reset window size event-handler instance variables declarations labels textFields buttons panel actionListeners Outline
30
TextPad Multiple instances Terms and Concepts GUI components JTextFields JButtons JLabels Event-driven programming Asynchronous Inheriting properties & methods Instance variables Constructors Method signature Classes Objects Methods Instances of objects JFrames import statements extend class JFrame’s setSize method JFrame’s setVisible method JPanels JPanel’s add method this notation ActionEvent ActionEvent’e getSource() method Color objects RGB representation Test program Program structure JPanel’s setBackground ( c) method
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.