GUI. Swing Class Hierarchy Swing Components Swing Conatiners JFrame – top-level window to store...

33
GUI
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    237
  • download

    2

Transcript of GUI. Swing Class Hierarchy Swing Components Swing Conatiners JFrame – top-level window to store...

Page 1: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

GUI

Page 2: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Swing Class Hierarchy

Page 3: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Swing Components

Page 4: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Swing Conatiners

JFrame – top-level window to store components

Page 5: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Swing Conatiners

JPanel – container; can be embedded in JFrame

Page 6: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Layouts

FlowLayout

arranges elements in a row

elements centered by default within container

GridLayout

subdivides container into cells of identical sizes

components take up all available space of a cell

BorderLayout

subdivides container into 5 areas: N, S, E, W, Center

Page 7: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Layouts

3x3 GridLayout 4x1 GridLayout BorderLayout

FlowLayout used to place the 3 panels in the Jframe.

Page 8: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Grid Layout

3x3 GridLayout 4x1 GridLayout BorderLayoutGridLayout layout = new GridLayout(3, 3);panel.setLayout(layout);

button1 = new JButton( "1" );panel.add( button1 );

button2 = new JButton( "2" );panel.add( button2 );

button3 = new JButton( "3" );panel.add( button3 );

button4 = new JButton( "4" );panel.add( button4 );

... ... ... ... ...

Components are stretched to occupy the whole panel area.

Page 9: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Border Layout

3x3 GridLayout 4x1 GridLayout BorderLayoutBorderLayout layout = new BorderLayout();panel.setLayout(layout);

button1 = new JButton( "North" );panel.add( button1, BorderLayout.NORTH );

button2 = new JButton( "South" );panel.add( button2, BorderLayout.SOUTH );

button3 = new JButton( "East" );panel.add( button3, BorderLayout.EAST );

button4 = new JButton( "West" );panel.add( button4, BorderLayout.WEST );

button5 = new JButton( "Center" );panel.add( button5, BorderLayout.CENTER );

Center area gets most of the space. The other areas are given only as much as they need. Not all areas need to be occupied.

Page 10: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Listeners Process events from components, containers

ActionListener (JButton, Timer, JComboBox)

ChangeListener (JSlider)

MouseListener, MouseMotionListener (JPanel, JFrame)

Listeners are interfaces; must implement ALL specified methods

ActionListener: void actionPerformed(ActionEvent e)

ChangeListener: void stateChanged(ChangeEvent e)

MouseListener: void mouseClicked(MouseEvent e)

void mousePressed(MouseEvent e)

void mouseReleased(MouseEvent e)

void mouseEntered(MouseEvent e)

void mouseExited(MouseEvent e)

MouseMotionListener: void mouseMoved(MouseEvent e) void mouseDragged(MouseEvent e)

Page 11: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Adapter classes Convenience classes

server as intermediaries between the available interfaces and the user-defined (listener) classes that implement the interfaces

make it possible to implement only the methods of interest

Page 12: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Adapter classes Convenience classes

server as intermediaries between the available interfaces and the user-defined (listener) classes that implement the interfaces

make it possible to implement only the methods of interest

abstract class MouseAdapter implements MouseListener, MouseMotionListener

{

void mousePressed(MouseEvent e) { // empty body }

void mouseReleased(MouseEvent e) { // empty body }

void mouseEntered(MouseEvent e) { // empty body }

void mouseExited(MouseEvent e) { // empty body }

void mouseMoved(MouseEvent e) { // empty body }

void mouseDragged(MouseEvent e) { // empty body }

}

MouseListener methods

MouseMotionListener methods

Page 13: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Adapter classes Convenience classes

server as intermediaries between the available interfaces and the user-defined (listener) classes that implement the interfaces

make it possible to implement only the methods of interest

abstract class MouseAdapter implements MouseListener, MouseMotionListener

{

abstract void mousePressed(MouseEvent e);

abstract void mouseReleased(MouseEvent e);

abstract void mouseEntered(MouseEvent e);

abstract void mouseExited(MouseEvent e);

abstract void mouseMoved(MouseEvent e);

asbtract void mouseDragged(MouseEvent e);

}

Page 14: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Adapter classes Convenience classes

server as intermediaries between the available interfaces and the user-defined (listener) classes that implement the interfaces

make it possible to implement only the methods of interest

abstract class MouseAdapter implements MouseListener, MouseMotionListener

{

… … … … … … … … … … … … …

}

class LineListener extends MouseAdapter

{

… implement only the methods of interest …

}

Page 15: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

File IO

Page 16: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Text File Output PrintWriter used for writing to file; same methods as in System.out

As if output is written to the screen with the extra steps of opening a file, closing the file, and catching exceptions

try { PrintWriter output = new PrintWriter("output-file.txt");

output.println("Hello");

output.printl(42);

output.println(3.1459);

output.close();

} catch (Exception e) {

// report error

}

Page 17: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Text File Input Scanner used for reading from file; same as in CS111

As if input is received from the keyboard with the extra steps of opening a file, closing the file, and catching exceptions

Reading stops as soon as appropriate token found; otherwise fails

try {

Scanner input = new Scanner(new File("input-file.txt"));

String word = input.next();

int answer = input.nextInt();

double pi = input.nextDouble();

input.close();

} catch (Exception e) {

// report error

}

Need to know the file format

Whitespaces are ignored

Page 18: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Serialization Mechanism for making exact copies of objects

For simple classes enough to declare implements Serializable

Application – saving / reading actual objects from a file

Page 19: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Saving with Serialization ObjectOutputStream and method writeObject(obj)

try {

FileOutputStream file = new FileOutputStream("pets.ser");

ObjectOutputStream output = new ObjectOutputStream(file);

output.write(petsList.size());

for (Pet pet : petsList) {

output.writeObject(pet);

}

output.close();

}catch (Exception e) {

System.out.println("Could not write to file. " + e);

}

Page 20: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

ObjectInputStream and method readObject()

try {

FileInputStream file = new FileInputStream("pets.ser");

ObjectInputStream output = new ObjectInputStream(file);

int count = input.readInt();

for (int i = 0; i < count; i++) {

Pet pet = input.readObject();

}

output.close();

}catch (Exception e) {

System.out.println("Could not read from file. " + e);

}

Reading with Serialization

Page 21: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Exceptions

Page 22: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Exceptions Mechanism for handling unexpected RUN-TIME conditions (errors)

Force the programmer to handle error conditions

Allow for separating the logic of the code from error-handling

Sometimes no other option to report the value:

constructor

minElement, maxElement

Example – see FileIO

Page 23: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Exceptions Can create our own type of exception (should inherit from Exception)

class EmptyArrayException extends Exception

{

public void EmptyArrayException()

{

super();

}

public void EmptyArrayException(String message)

{

super(message);

}

}

Page 24: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Exceptions Example of our own Exception --- throw/throws

int minElement(int[] numbers) throws EmptyArrayException

{

// empty array --- throw an exception

if (numbers.length == 0)

{

throw EmptyArrayException(“Empty array given”);

}

//

// ... compute smallest element ...

//

}

Page 25: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

extends vs. implements

class vs. interface

multiple inheritance

Page 26: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Interfaces An interface specifies a collection of methods

An interface does not have data members or code for methods

A class that implements the interface must provide code (implementation) for all methods listed in the interface

interface RemoteControllable{

public void play();public void stop();public void ffwd();

}

class VCR implements RemoteControllable{ // must provide code for all methods in RemoteControllable}class DVD implements RemoteControllable{ // must provide code for all methods in RemoteControllable}

Page 27: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Multiple Inheritance

String name; //speciesvoid setName(String n) { ... ... ... }

String name; // pet’s namevoid setName(String n) { ... ... ... }

Class Mammal Class Pet

Class Cat

Page 28: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Multiple Inheritance

String name; //speciesvoid setName(String n) { ... ... ... }

String name; // pet’s namevoid setName(String n) { ... ... ... }

Class Mammal Class Pet

Which name is inherited?

Which setName() is inherited?

Class Cat

Page 29: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Complex rules required to disambiguate in multiple inheritance

Java does not support multiple inheritance; C++ does

Multiple Inheritance

String name; //speciesvoid setName(String n) { ... ... ... }

String name; // pet’s namevoid setName(String n) { ... ... ... }

Class Mammal Class Pet

Which name is inherited?

Which setName() is inherited?

Class Cat

Page 30: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

What if we still want a Cat to behave like a Mammal and Pet

interface Mammal{ // all methods (behaviors) common to mammals // no code is specified, just the behavior names

(methods)}

class Pet{ // description of generic pet}

class Cat extends Pet implements Mammal{ // has all behaviors of a Pet – could override some

// must implement all behaviors of Mammal}

Multiple Inheritance

Page 31: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Can now use Cat objects anywhere Mammal behaviors required or where Pet objects are required

public void hunt(Mammal predator, Mammal prey){ // do something; could send a Cat as // either prey or predator}

public void doTricks(Pet pet){ // do something; could send a Cat for pet}

Multiple Inheritance

Page 32: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

A Java class can only extend from one other class (single inheritance)

A Java class can implement multiple interfaces – can ambiguity arise?

Multiple Interfaces Implementation

Page 33: GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

A Java class can only extend from one other class (single inheritance)

A Java class can implement multiple interfaces – no ambiguity since

an interface cannot have data members

an interface cannot have code (implementation) for methods

Multiple Interfaces Implementation