Download presentation
Presentation is loading. Please wait.
1
CPSC150 JavaLynn Lambert CPSC150 Week 12 InheritanceInterfaces
2
CPSC150 Java Lynn Lambert Sections from Book Covered already: Covered already: 8.1-8.8 (covered in week 10), 9.2 8.1-8.8 (covered in week 10), 9.2 Covered now: Covered now: super call in methods 9.5 (covered?) super call in methods 9.5 (covered?) Covered in your assignment: Covered in your assignment: toString() 9.7 toString() 9.7 protected access 9.8 protected access 9.8
3
CPSC150 Java Lynn Lambert Sections week 11 To be covered: To be covered: overriding 9.3 overriding 9.3 dynamic method lookup 9.4 dynamic method lookup 9.4 Abstract Classes 10.3 Abstract Classes 10.3 Interfaces 10.5, 10.6 Interfaces 10.5, 10.6
4
CPSC150 Java Lynn Lambert Exercises For each of these, have at least 2 methods and 1 field or at least 2 fields and 1 method. You may use more. Write a parent Shape that is a parent of Square, Circle, and Triangle. Should it be a class, an abstract class or an Interface? What should be in it? Write a parent Shape that is a parent of Square, Circle, and Triangle. Should it be a class, an abstract class or an Interface? What should be in it? Write a class Person with three subtypes, teacher, student and worker. Write an Interface inSchool that teacher and student implement. What should be in there? Write a class Person with three subtypes, teacher, student and worker. Write an Interface inSchool that teacher and student implement. What should be in there?
5
CPSC150 JavaLynn Lambert Graphical User Interfaces Chapter 11 Week 13
6
CPSC150 Java Lynn Lambert GUI parts Components Components buttons, menus, textfields, etc. buttons, menus, textfields, etc. Layout Layout Where on the screen to put the components. use LayoutManager Where on the screen to put the components. use LayoutManager Event Handling Event Handling instead of executing methods when called (by BlueJ or in code), execute methods when an event occurs (e.g., mouse click on a button) instead of executing methods when called (by BlueJ or in code), execute methods when an event occurs (e.g., mouse click on a button)
7
CPSC150 Java Lynn Lambert Swing Java has AWT (old, Abstract Window Toolkit) and Swing (based on AWT) Java has AWT (old, Abstract Window Toolkit) and Swing (based on AWT) Swing prefixes components with J Swing prefixes components with J e.g., Button is from AWT, jButton is Swing e.g., Button is from AWT, jButton is Swing We’ll use Swing We’ll use Swing To use Swing: To use Swing: import java.awt.*; // * means all packages in import java.awt.event.*; import javax.swing.*; // not ‘x’
8
CPSC150 Java Lynn Lambert Building a Window Everything is in a JFrame Everything is in a JFrame JFrame has JFrame has Title bar Title bar Menu bar (optional) Menu bar (optional) Content Pane Content Pane title bar menu bar content pane Frame (whole window)
9
CPSC150 Java Lynn Lambert Code for a Frame To build a JFrame, do 4 things (From cpsc150Lab lab3 GuiInterface.java) 1. create new JFrame and put a title in private JFrame windowFrame = new JFrame(“Word”); 2. tie parts to JFrame (component, menubar, etc) Container cp = WindowFrame.getContentPane( ); cp.add(gameWordPanel); OR OR windowFrame.getContentPane( ).add(gameWordPanel); etc. content panes can hold any number of components etc. content panes can hold any number of components (4 components in windowFrame in lab3) (4 components in windowFrame in lab3) content Pane vs JPanel content Pane vs JPanel 3. pack frame. arrange components windowFrame.pack( ) 4. make the frame visible (can also make it invisible, as in quitWord) windowFrame.setVisible(true);
10
CPSC150 Java Lynn Lambert Some Components JButton JButton JPanel JPanel JLabel JLabel Lots, look at: Lots, look at: http://java.sun.com/j2se/1.4.2/docs/api/javax/s wing/package-summary.html
11
CPSC150 Java Lynn Lambert title bar minimize maximize close come for free component (JPanel) { Also in lab3, what to do on close (click on red X): windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12
CPSC150 Java Lynn Lambert Layout managers Manage limited space for competing components. Manage limited space for competing components. FlowLayout, BorderLayout, GridLayout, BoxLayout, GridBagLayout (most flexible, hardest) FlowLayout, BorderLayout, GridLayout, BoxLayout, GridBagLayout (most flexible, hardest) Manage Container objects, e.g. a content pane. Manage Container objects, e.g. a content pane. Each imposes its own style. Each imposes its own style.
13
CPSC150 Java Lynn Lambert FlowLayout
14
CPSC150 Java Lynn Lambert BorderLayout
15
CPSC150 Java Lynn Lambert GridLayout
16
CPSC150 Java Lynn Lambert BoxLayout Note: no component resizing.
17
CPSC150 Java Lynn Lambert Layout Manager Lab 3 4 content panes – gameWordPanel, gameGuessPanel, gameButtonPanel, gameStatusPanel 4 content panes – gameWordPanel, gameGuessPanel, gameButtonPanel, gameStatusPanel
18
CPSC150 Java Lynn Lambert Layout Managers – Lab 3 Container contentPane = windowFrame.getContentPane ( ); contentPane.setLayout(new GridLayout(4, 1)); OR windowFrame.getContentPane().setLayout(new GridLayout(4,1)); // add panels in order windowFrame.getContentPane().add(gameWordPanel); windowFrame.getContentPane().add(gameWordPanel); windowFrame.getContentPane().add(gameGuessPanel); windowFrame.getContentPane().add(gameGuessPanel); windowFrame.getContentPane().add(gameButtonPanel); windowFrame.getContentPane().add(gameButtonPanel); windowFrame.getContentPane().add(gameStatusPanel); windowFrame.getContentPane().add(gameStatusPanel);
19
CPSC150 Java Lynn Lambert Border Layout – Lab 3 windowFrame.getContentPane().setLayout(new BorderLayout()); windowFrame.getContentPane().setLayout(new BorderLayout()); windowFrame.getContentPane().add(gameWordPanel BorderLayout.NORTH); windowFrame.getContentPane().add(gameWordPanel BorderLayout.NORTH); windowFrame.getContentPane().add(gameGuessPanel, BorderLayout.EAST); windowFrame.getContentPane().add(gameGuessPanel, BorderLayout.EAST); windowFrame.getContentPane().add(gameButtonPanel, BorderLayout.WEST); windowFrame.getContentPane().add(gameButtonPanel, BorderLayout.WEST); windowFrame.getContentPane().add(gameStatusPanel, BorderLayout.SOUTH); windowFrame.getContentPane().add(gameStatusPanel, BorderLayout.SOUTH);
20
CPSC150 Java Lynn Lambert Border Layout – Lab 3
21
CPSC150 Java Lynn Lambert Events We can now put items on a JFrame and arrange them. We need to be able to do something with them. We can now put items on a JFrame and arrange them. We need to be able to do something with them. Action Events) Action Events) mouse click, mouse over, window action, menu choice, etc. mouse click, mouse over, window action, menu choice, etc. General, ActionListener General, ActionListener Something happened Something happened in java.awt.event in java.awt.event (http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/even t/package-summary.html) (http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/even t/package-summary.html)http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/even t/package-summary.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/javax/swing/even t/package-summary.html
22
CPSC150 Java Lynn Lambert ActionListener interface interface one method: one method: actionPerformed actionPerformed for each component that allows user response (e.g., JButton, NOT JLabel), make an ActionListener and implement actionPerformed for each component that allows user response (e.g., JButton, NOT JLabel), make an ActionListener and implement actionPerformed
23
CPSC150 Java Lynn Lambert “Check” button in Lab 3 b = new JButton("Check"); b = new JButton("Check"); b.addActionListener(new ActionListener() { b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); System.out.println("Check is " + e.getActionCommand()); checkWord(); checkWord(); } } ); ); What? there’s no class, no “implements ActionListener” What happened? What? there’s no class, no “implements ActionListener” What happened?
24
CPSC150 Java Lynn Lambert JButton listeners -- yuck class GuiInterface implements ActionListenter { JButton CheckButton; public void ActionPerformed(ActionEvent event) public void ActionPerformed(ActionEvent event) { if (event.getActionCommand( ).equals(“Check”) if (event.getActionCommand( ).equals(“Check”) checkWord( ); checkWord( );else … // check all other button possibilities }}
25
CPSC150 Java Lynn Lambert Yucky ActionListeners previous version works previous version works Bad because: Bad because: One place for EVERY button. better for each button to have its own method One place for EVERY button. better for each button to have its own method What if we change to “Inspect” What if we change to “Inspect” would have to rewrite “Check” in boolean would have to rewrite “Check” in boolean Lab 3 does not do that. How does it get away with that? Lab 3 does not do that. How does it get away with that?
26
CPSC150 Java Lynn Lambert Inner Classes A class within another class A class within another class class Outside {… class Inside class Inside { … // like methods, can use private fields in Outside … // like methods, can use private fields in Outside }}
27
CPSC150 Java Lynn Lambert Inner Class class GuiInterface { JButton CheckButton; CheckButton.addActionListener(new CheckActionListener( )); class CheckActionListener implements ActionListener class CheckActionListener implements ActionListener { public void ActionPerformed(ActionEvent event) public void ActionPerformed(ActionEvent event) { checkWord( ); checkWord( ); }}
28
CPSC150 Java Lynn Lambert Inner Classes Each button now has its own ActionListener. no giant if statement and no code that relies on the button having the same text forever. Each button now has its own ActionListener. no giant if statement and no code that relies on the button having the same text forever. This is closer to the actual code, but still bulkier than the original This is closer to the actual code, but still bulkier than the original Inner classes are good when exactly one instance/object will be created Inner classes are good when exactly one instance/object will be created CheckButton.addActionListener(new CheckActionListener( )); CheckButton.addActionListener(new CheckActionListener( ));
29
CPSC150 Java Lynn Lambert Anonymous Classes We don’t really ever use the inner class by name We don’t really ever use the inner class by name make it anonymous (unnamed) make it anonymous (unnamed) Instead of “implements ActionListener” use “new ActionListener()” for creating class Instead of “implements ActionListener” use “new ActionListener()” for creating class Looks look call to constructor, but is not Looks look call to constructor, but is not
30
CPSC150 Java Lynn Lambert Anonymous Classes – Part 1 class GuiInterface { JButton CheckButton; CheckButton.addActionListener(new CheckActionListener( )); class CheckActionListener implements ActionListener class CheckActionListener implements ActionListener { public void ActionPerformed(ActionEvent event) public void ActionPerformed(ActionEvent event) { checkWord( ); checkWord( ); } }…}
31
CPSC150 Java Lynn Lambert Anonymous Classes – Part 2 Delete Class Name class GuiInterface { JButton CheckButton; CheckButton.addActionListener(new ( )); implements ActionListener implements ActionListener { public void ActionPerformed(ActionEvent event) public void ActionPerformed(ActionEvent event) { checkWord( ); checkWord( ); } }…}
32
CPSC150 Java Lynn Lambert Anonymous Classes – Part 3 Delete ( )s for constructor class GuiInterface { JButton CheckButton; CheckButton.addActionListener(new ); implements ActionListener implements ActionListener { public void ActionPerformed(ActionEvent event) public void ActionPerformed(ActionEvent event) { checkWord( ); checkWord( ); } }…}
33
CPSC150 Java Lynn Lambert Anonymous Classes – Part 4 move class inside ( )s class GuiInterface { JButton CheckButton; CheckButton.addActionListener(new implements ActionListener implements ActionListener { public void ActionPerformed(ActionEvent event) public void ActionPerformed(ActionEvent event) { checkWord( ); checkWord( ); } } ); );…}
34
CPSC150 Java Lynn Lambert Part 5 - change “implements ActionListener” to “new ActionListener( )” // weird class GuiInterface { JButton CheckButton; CheckButton.addActionListener(new ActionListener( ) ActionListener( ) { public void ActionPerformed(ActionEvent event) public void ActionPerformed(ActionEvent event) { checkWord( ); checkWord( ); } } ); );…}
35
CPSC150 Java Lynn Lambert Anonymous Classes – Part 6 original (CheckButton changed to b) class GuiInterface { b = new JButton("Check"); b.addActionListener(new ActionListener() { b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { System.out.println("Check is " + e.getActionCommand()); System.out.println("Check is " + e.getActionCommand()); checkWord(); checkWord(); } } ); );…}
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.