













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Lecture from Object Oriented Programming and Data Structures course with following key points: Main Box, Boardbox, Infobox, Jbutton, Jlabel, Vertical Box, Tree
Typology: Slides
1 / 21
This page cannot be seen from the preview
Don't miss anything!














mainBox boardBox infoBox boardBox: vertical Box row: horizontal Box Square: Canvas or JPanel infoBox: vertical Box row (^) … row Square … Square Square … Square JButton JButton JButton JLabel JLabel JLabel pack(): Traverse the tree, determining the space required for each component
Listening to events: mouse click, mouse movement into or out of a window, a keystroke, etc.
/** Object has two buttons. Exactly one is enabled. / class ButtonDemo1 extends JFrame { /* Class inv: exactly one of eastB, westB is enabled */ JButton westB= new JButton("west"); JButton eastB= new JButton("east"); public ButtonDemo1(String t) { super (t); Container cp= getContentPane(); cp.add(westB, BLayout.WEST); cp.add(eastB, BLayout, EAST); westB.setEnabled( false ); eastB.setEnabled( true ); pack(); setVisible( true ); } public void actionPerformed (ActionEvent e) { boolean b= eastB.isEnabled(); eastB.setEnabled(!b); westB.setEnabled(b); } } red: listening blue: placing Listening to a Button implements ActionListener westB.addActionListener( this ); eastB.addActionListener( this );
/** Instance: JPanel of size (WIDTH, HEIGHT). Green or red: / public class Square extends JPanel { public static final int HEIGHT= 70; public static final int WIDTH= 70; private int x, y; // Panel is at (x, y) private boolean hasDisk= false ; /* Const: square at (x, y). Red/green? Parity of x+y. / public Square( int x, int y) { this .x= x; this .y= y; setPreferredSize( new Dimension(WIDTH,HEIGHT)); } /* Complement the "has pink disk" property */ public void complementDisk() { hasDisk=! hasDisk; repaint(); // Ask the system to repaint the square } Class Square continued on later
Class Graphics An object of abstract class Graphics has methods to draw on a component (e.g. on a JPanel, or canvas). Major methods: drawString(“abc”, 20, 30); drawLine(x1, y1, x2, y2); drawRect(x, y, width, height); fillRect(x, y, width, height); drawOval(x, y, width, height); fillOval(x, y, width, height); setColor(Color.red); getColor() getFont() setFont(Font f); More methods Graphics is in package java.awt You won’t create an object of Graphics; you will be given one to use when you want to paint a component
public interface MouseListener { void mouseClicked(MouseEvent e); void mouseEntered(MouseEvent e); void mouseExited(MouseEvent e); void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e); } In package java.awt.event Having to write all of these in a class that implements MouseListener, even though you don’t want to use all of them, can be a pain. So, a class is provided that implements them in a painless.
public class MouseInputAdaptor implements MouseListener, MouseInputListener { public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} … others … } In package java.swing.event So, just write a subclass of MouseInputAdaptor and override only the methods appropriate for the application
A class that listens to a mouseclick in a Square import javax.swing.; import javax.swing.event.; import java.awt.; import java.awt.event.; /** Contains a method that responds to a mouse click in a Square */ public class MouseEvents extends MouseInputAdapter { // Complement "has pink disk" property public void mouseClicked(MouseEvent e) { Object ob= e.getSource(); if (ob instanceof Square) { ((Square)ob).complementDisk(); } } } This class has several methods (that do nothing) that process mouse events: mouse click mouse press mouse release mouse enters component mouse leaves component mouse dragged beginning in component Our class overrides only the method that processes mouse clicks red: listening blue: placing
Class MouseDemo public class MD2 extends JFrame Box b= new Box(…X_AXIS); Box leftC= new Box(…Y_AXIS); Square b00, b01= new squares; Box riteC= new Box(..Y_AXIS); Square b10, b01= new squares; JButton jb= new JButton("reset"); /** Constructor: … */ public MouseDemo2() { super (t); place components on content pane; pack, make unresizeable, visible; jb.addActionListener( this ); b00.addMouseListener(me); b01.addMouseListener(me); b10.addMouseListener(me); b11.addMouseListener(me); } red: listening blue: placing implements ActionListener { public void actionPerformed ( ActionEvent e) { call clearDisk() for b00, b01, b10, b } MouseEvents me= new MouseEvents();
public class BDemo3 extends JFrame implements ActionListener { private JButton wButt, eButt …; public ButtonDemo3() { Add buttons to content pane, enable ne, disable the other wButt.addActionListener( this ); eButt.addActionListener( new BeListener()); } public void actionPerformed(ActionEvent e) { boolean b= eButt.isEnabled(); eButt.setEnabled(!b); wButt.setEnabled(b); } } class BeListener implements ActionListener { public void actionPerformed(ActionEvent e) { boolean b= eButt.isEnabled(); eButt.setEnabled(!b); wButt.setEnabled(b); } A listener for eastButt Doesn’t work! Can’t reference eButt, wButt Have a different listener for each button
eButt (^) … BD aPerf(… eButt … wButt ...} wButt (^) … BeLis@ BeLis aPerf(… eButt … wButt ...} Make BeListener an inner class. Inside-out rule then gives access to wButt, eButt
eButt (^) … BD aPerf…(… eButt … wButt..} wButt (^) … BeLis@ BeLis aPerf(… eButt … wButt ...} listens to wButt listens to eButt but can’t reference fields
Problem: can’t give a function as a parameter: public void m() { … eButt.addActionListener(aP); } public void aP(ActionEvent e) { body } Why not just give eButt the function to call? Can’t do it in Java! Can in some other languages public void m() { … eButt.addActionListener( new C()); } public class C implements IN { public void aP(ActionEvent e) { body } } Java says: provide class C that wraps method; give eButt an object of class C C must implement interface IN that has abstract method aP
Have a class for which only one object is created? Use an anonymous class. Use sparingly, and only when the anonymous class has 1 or 2 methods in it, because the syntax is ugly, complex, hard to understand. public class BDemo3 extends JFrame implements ActionListener { private JButton wButt, eButt …; public ButtonDemo3() { … eButt.addActionListener( new BeListener()); } public void actionPerformed(ActionEvent e) { … } private class BeListener implements ActionListener { public void actionPerformed(ActionEvent e) { body } } } 1 object of BeListener created. Ripe for making anonymous