SlideShare a Scribd company logo
1 of 19
Download to read offline
Using a Simple GUI
public static void initialize() { mySSNGUI=new JFrame(); mySSNGUI.setSize(400, 200); mySSNGUI.setLocation( 100, 100 ); mySSNGUI.setTitle(" Social Security Numbers "); mySSNGUI.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); mySSNGUI.setVisible(true); } This area is the ContentPane Creating a JFrame mySSNGUI
public static void printSSNtoJFrame (JFrame jf, String[] list, int size) { Container myContentPane = jf.getContentPane(); TextArea myTextArea = new TextArea(); myContentPane.add(myTextArea); for (int i=0;i<size;i++) if (!isValidSSN(list[i])) myTextArea.append(&quot;Invalid SSN: &quot;+list[i]+&quot;&quot;); else myTextArea.append(list[i]+&quot;&quot;); jf.setVisible(true); }  Putting data in a JFrame
Container myContentPane = jf.getContentPane(); myContentPane
Container myContentPane = jf.getContentPane(); myContentPane TextArea myTextArea = new TextArea(); myTextArea
myContentPane.add(myTextArea); myContentPane myTextArea myTextArea.append(“Hello”); myContentPane myTextArea Hello
It is more common for the &quot;main&quot; application and the GUI to be separate classes. Main Instantiates GUI Has object reference to GUI Manipulates GUI GUI Object Title Content Pane Window Close Etc. mySSNGUI public class SSN {… mySSNGUI = new Jframe(); mySSNGUI = new SSNGUI(); public class SSNGUI {…
public class SSNGUI  extends JFrame {… } NO! We can take advantage of the most powerful  Object-Oriented feature… Inheritance public class SSNGUI {… But do we have to write code  here for all things a GUI  (JFrame) can do? }
public class SSNGUI  extends JFrame {… } The class SSNGUI can now do whatever a JFrame can do, as well as whatever we add to it.  public static void initialize() { mySSNGUI=new SSNGUI(); mySSNGUI.setSize(400, 200); mySSNGUI.setLocation(100, 100); mySSNGUI.setTitle(&quot;Social Security Numbers&quot;); mySSNGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mySSNGUI.setVisible(true); }
The program SSNFrame3 produces this SSNGUI: The entire ContentPane is one TextArea
To &quot;divide&quot; the ContentPane into different areas, we can use a  LayoutManager ,[object Object],[object Object],[object Object]
BorderLayout East North West South Center
public static void printSSNtoSSNGUI  (SSNGUI ssnGUI,  String[] list, int size) { Container myContentPane = ssnGUI.getContentPane(); TextArea myTextArea = new TextArea(); TextArea mySubscripts = new TextArea(); myContentPane.add(myTextArea, BorderLayout.EAST); myContentPane.add(mySubscripts, BorderLayout.WEST); for (int i=0;i<size;i++) { mySubscripts.append(Integer.toString(i)+&quot;&quot;); if (!isValidSSN(list[i])) myTextArea.append(&quot;Invalid SSN: &quot;+list[i]+&quot;&quot;); else myTextArea.append(list[i]+&quot;&quot;); } ssnGUI.setVisible(true);
 
mySSNGUI.setLayout(new GridLayout(1,2));
myContentPane.add(myTextArea); myContentPane.add(mySubscripts); for (int i=0;i<size;i++) { mySubscripts.append(Integer.toString(i)+&quot;&quot;); if (!isValidSSN(list[i])) myTextArea.append(&quot;Invalid SSN: &quot;+list[i]+&quot;&quot;); else myTextArea.append(list[i]+&quot;&quot;); }
Non-application Classes (no &quot;main&quot; method) ,[object Object],[object Object],[object Object],[object Object],[object Object]
import javax.swing.*; public class SSNGUI extends JFrame { public SSNGUI () { } } ,[object Object],[object Object],[object Object],[object Object]
import javax.swing.*; public class SSNGUI extends JFrame { public SSNGUI( String title ) { setTitle( title ); } import javax.swing.*; public class SSNGUI extends JFrame { public SSNGUI( String title, int height, int width ) { setTitle( title ); setSize( height, width ); }

More Related Content

Viewers also liked

Rock Candy | Beauty &amp; The Beast
Rock Candy | Beauty &amp; The BeastRock Candy | Beauty &amp; The Beast
Rock Candy | Beauty &amp; The BeastMathew C.P Hayward
 
10.3 Android Video
10.3 Android Video10.3 Android Video
10.3 Android VideoOum Saokosal
 
JAVA GUI PART I
JAVA GUI PART IJAVA GUI PART I
JAVA GUI PART IOXUS 20
 
Java OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - SwingJava OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - SwingOUM SAOKOSAL
 

Viewers also liked (8)

Rock Candy | Beauty &amp; The Beast
Rock Candy | Beauty &amp; The BeastRock Candy | Beauty &amp; The Beast
Rock Candy | Beauty &amp; The Beast
 
UCM 6
UCM 6UCM 6
UCM 6
 
10.3 Android Video
10.3 Android Video10.3 Android Video
10.3 Android Video
 
Swing
SwingSwing
Swing
 
JAVA GUI PART I
JAVA GUI PART IJAVA GUI PART I
JAVA GUI PART I
 
Java OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - SwingJava OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - Swing
 
Java swing
Java swingJava swing
Java swing
 
Centripetal Force
Centripetal ForceCentripetal Force
Centripetal Force
 

Similar to 4 gu is-andinheritance

Creating an Uber Clone - Part XXXIX.pdf
Creating an Uber Clone - Part XXXIX.pdfCreating an Uber Clone - Part XXXIX.pdf
Creating an Uber Clone - Part XXXIX.pdfShaiAlmog1
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfShaiAlmog1
 
Html and i_phone_mobile-2
Html and i_phone_mobile-2Html and i_phone_mobile-2
Html and i_phone_mobile-2tonvanbart
 
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdfCreating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdfShaiAlmog1
 
Creating a Facebook Clone - Part XXVIII.pdf
Creating a Facebook Clone - Part XXVIII.pdfCreating a Facebook Clone - Part XXVIII.pdf
Creating a Facebook Clone - Part XXVIII.pdfShaiAlmog1
 
Gauss in java
Gauss in javaGauss in java
Gauss in javabaxter89
 
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdfUsing standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdffashiongallery1
 
COA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdfCOA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdfJavedAnsari236392
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semesterDOSONKA Group
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Skills Matter
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mark Needham
 
Insert latest articles on blogger
Insert latest articles on bloggerInsert latest articles on blogger
Insert latest articles on bloggerStefano Vinci
 
Making it fit - DroidCon Paris 18 june 2013
Making it fit - DroidCon Paris 18 june 2013Making it fit - DroidCon Paris 18 june 2013
Making it fit - DroidCon Paris 18 june 2013Paris Android User Group
 
Stabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance EvaluationStabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance EvaluationEmery Berger
 
2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrayskinan keshkeh
 

Similar to 4 gu is-andinheritance (20)

Creating an Uber Clone - Part XXXIX.pdf
Creating an Uber Clone - Part XXXIX.pdfCreating an Uber Clone - Part XXXIX.pdf
Creating an Uber Clone - Part XXXIX.pdf
 
Sencha Touch Intro
Sencha Touch IntroSencha Touch Intro
Sencha Touch Intro
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdf
 
Html and i_phone_mobile-2
Html and i_phone_mobile-2Html and i_phone_mobile-2
Html and i_phone_mobile-2
 
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdfCreating a Facebook Clone - Part XXVIII - Transcript.pdf
Creating a Facebook Clone - Part XXVIII - Transcript.pdf
 
Creating a Facebook Clone - Part XXVIII.pdf
Creating a Facebook Clone - Part XXVIII.pdfCreating a Facebook Clone - Part XXVIII.pdf
Creating a Facebook Clone - Part XXVIII.pdf
 
Gauss in java
Gauss in javaGauss in java
Gauss in java
 
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdfUsing standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
 
COA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdfCOA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdf
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semester
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 
Insert latest articles on blogger
Insert latest articles on bloggerInsert latest articles on blogger
Insert latest articles on blogger
 
Making it fit - DroidCon Paris 18 june 2013
Making it fit - DroidCon Paris 18 june 2013Making it fit - DroidCon Paris 18 june 2013
Making it fit - DroidCon Paris 18 june 2013
 
Stabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance EvaluationStabilizer: Statistically Sound Performance Evaluation
Stabilizer: Statistically Sound Performance Evaluation
 
DSA.pdf
DSA.pdfDSA.pdf
DSA.pdf
 
Structure
StructureStructure
Structure
 
Structure
StructureStructure
Structure
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatch
 
2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays
 

4 gu is-andinheritance

  • 2. public static void initialize() { mySSNGUI=new JFrame(); mySSNGUI.setSize(400, 200); mySSNGUI.setLocation( 100, 100 ); mySSNGUI.setTitle(&quot; Social Security Numbers &quot;); mySSNGUI.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); mySSNGUI.setVisible(true); } This area is the ContentPane Creating a JFrame mySSNGUI
  • 3. public static void printSSNtoJFrame (JFrame jf, String[] list, int size) { Container myContentPane = jf.getContentPane(); TextArea myTextArea = new TextArea(); myContentPane.add(myTextArea); for (int i=0;i<size;i++) if (!isValidSSN(list[i])) myTextArea.append(&quot;Invalid SSN: &quot;+list[i]+&quot;&quot;); else myTextArea.append(list[i]+&quot;&quot;); jf.setVisible(true); } Putting data in a JFrame
  • 4. Container myContentPane = jf.getContentPane(); myContentPane
  • 5. Container myContentPane = jf.getContentPane(); myContentPane TextArea myTextArea = new TextArea(); myTextArea
  • 6. myContentPane.add(myTextArea); myContentPane myTextArea myTextArea.append(“Hello”); myContentPane myTextArea Hello
  • 7. It is more common for the &quot;main&quot; application and the GUI to be separate classes. Main Instantiates GUI Has object reference to GUI Manipulates GUI GUI Object Title Content Pane Window Close Etc. mySSNGUI public class SSN {… mySSNGUI = new Jframe(); mySSNGUI = new SSNGUI(); public class SSNGUI {…
  • 8. public class SSNGUI extends JFrame {… } NO! We can take advantage of the most powerful Object-Oriented feature… Inheritance public class SSNGUI {… But do we have to write code here for all things a GUI (JFrame) can do? }
  • 9. public class SSNGUI extends JFrame {… } The class SSNGUI can now do whatever a JFrame can do, as well as whatever we add to it. public static void initialize() { mySSNGUI=new SSNGUI(); mySSNGUI.setSize(400, 200); mySSNGUI.setLocation(100, 100); mySSNGUI.setTitle(&quot;Social Security Numbers&quot;); mySSNGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mySSNGUI.setVisible(true); }
  • 10. The program SSNFrame3 produces this SSNGUI: The entire ContentPane is one TextArea
  • 11.
  • 12. BorderLayout East North West South Center
  • 13. public static void printSSNtoSSNGUI (SSNGUI ssnGUI, String[] list, int size) { Container myContentPane = ssnGUI.getContentPane(); TextArea myTextArea = new TextArea(); TextArea mySubscripts = new TextArea(); myContentPane.add(myTextArea, BorderLayout.EAST); myContentPane.add(mySubscripts, BorderLayout.WEST); for (int i=0;i<size;i++) { mySubscripts.append(Integer.toString(i)+&quot;&quot;); if (!isValidSSN(list[i])) myTextArea.append(&quot;Invalid SSN: &quot;+list[i]+&quot;&quot;); else myTextArea.append(list[i]+&quot;&quot;); } ssnGUI.setVisible(true);
  • 14.  
  • 16. myContentPane.add(myTextArea); myContentPane.add(mySubscripts); for (int i=0;i<size;i++) { mySubscripts.append(Integer.toString(i)+&quot;&quot;); if (!isValidSSN(list[i])) myTextArea.append(&quot;Invalid SSN: &quot;+list[i]+&quot;&quot;); else myTextArea.append(list[i]+&quot;&quot;); }
  • 17.
  • 18.
  • 19. import javax.swing.*; public class SSNGUI extends JFrame { public SSNGUI( String title ) { setTitle( title ); } import javax.swing.*; public class SSNGUI extends JFrame { public SSNGUI( String title, int height, int width ) { setTitle( title ); setSize( height, width ); }