java swing recitation – 11/(20,21)/2008

25
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University

Upload: ace

Post on 14-Feb-2016

42 views

Category:

Documents


4 download

DESCRIPTION

CS 180 Department of Computer Science, Purdue University. Java Swing Recitation – 11/(20,21)/2008 . Announcements. Project 8 is out Milestone due on Dec 3 rd, 10:00 pm Final due on Dec 10 th, 10:00 pm No classes, recitations and labs next week. No study group meeting next Tuesday. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Java Swing  Recitation – 11/(20,21)/2008

Java Swing Recitation – 11/(20,21)/2008

CS 180Department of Computer Science, Purdue University

Page 2: Java Swing  Recitation – 11/(20,21)/2008

Announcements Project 8 is out

Milestone due on Dec 3rd, 10:00 pm Final due on Dec 10th, 10:00 pm

No classes, recitations and labs next week. No study group meeting next Tuesday. Consulting hours will be held only on

Monday 7-10 pm.

Page 3: Java Swing  Recitation – 11/(20,21)/2008

Event Driven Programming Most GUI programs involve events and event

handlers.

A GUI event is an object that represents some action such as clicking the mouse, dragging the mouse, pressing a keyboard key, clicking the close-window button on a window, etc.

When an object generates an event, it is said to fire the event.

Page 4: Java Swing  Recitation – 11/(20,21)/2008

4

Event Driven Programming

An event is an object that represents an action

Event handling is similar to exception handling Difference –

Exceptions are created by user code or java interpreter. Events are created by external actions, such as user

interactions through a GUI

Page 5: Java Swing  Recitation – 11/(20,21)/2008

Programming Example: A Simple Window

This simple program produces a window and displays some text.

JFrame : to create a window JLabel : to create a label getContentPane().add() : add a component such as a label to the

content pane of the window setTitle() : set the title of the window setSize() : set the size of the window setVisible() : Method setVisible permits the programmer to specify

when GUI objects should be displayed and when they should not

Page 6: Java Swing  Recitation – 11/(20,21)/2008

public class makeWindow { public static void main(String args[]) { JFrame myWindow=new JFrame(); //create the window myWindow.setSize(300, 200); //set the title of the window myWindow.setTitle("this is a window"); //create the label JLabel myLabel=new JLabel("this is a label"); //add the label to the content pane of the window myWindow.getContentPane().add(myLabel); //set color of the content pane myWindow.getContentPane().setBackground(Color.CYAN); //make the window visible myWindow.setVisible(true); }}

A Frame

This area is the content pane

ExampleProgramming Example: A Simple Window

Page 7: Java Swing  Recitation – 11/(20,21)/2008

7

Layout Managers A layout manager arranges objects within a

container After a container has been created, you can set

its layout manager using the setLayout method.

For example: Container contentPane = frame.getContentPane();

contentPane.setLayout(new FlowLayout());

FlowLayout : It simply lays out components in a single row, starting a new row if its container is not sufficiently wide.

Page 8: Java Swing  Recitation – 11/(20,21)/2008

8

Layout Managers BorderLayout : It places components in up to five areas:

top, bottom, left, right, and center. Every content pane is initialized to use a BorderLayout.

GridLayout : It simply arranges a bunch of components in a grid of rows and columns. Components are

ordered in a row-major fashion.

Page 9: Java Swing  Recitation – 11/(20,21)/2008

Example - Border Layout A BorderLayout manager can place a component

into any of the five regions. Regions which are unused give up their space to

BorderLayout.CENTER. This layout limits the GUI to five objects, these are

almost always five (or fewer) JPanels.

Equivalent forms for center:content.add(label3, BorderLayout.CENTER);

andcontent.add(label3, “Center”);

and (for center ONLY)content.add(label3);

Even though BorderLayout is default,it’s better to set the layout explicitly

Page 10: Java Swing  Recitation – 11/(20,21)/2008

Buttons A button is a GUI component that looks like a

button and fires an event when it is clicked using a mouse.

Like a label, a button is created and added to a container.

Unlike a label, a button can fire an event and the event can cause a GUI to perform some action.

Buttons are instances of the JButton class.

Page 11: Java Swing  Recitation – 11/(20,21)/2008

Adding Buttons A button is created using

JButton Button_Name = new JButton(“Button_Label”);

A button is added to a container usingContainer_Name.add(Button_Name);

Page 12: Java Swing  Recitation – 11/(20,21)/2008

Action Listeners and Action Events

For each button, the GUI needs to register (specify) the listener object(s). define the methods to be invoked when an

event is fired. Buttons fire action events which are handled by

action listeners. An action listener is an object of type

ActionListener, and ActionListener is an interface

Note: A user defined GUI class can itself be its own listener if it implements the ActionListener interface.

Page 13: Java Swing  Recitation – 11/(20,21)/2008

Action Listeners and Action Events, cont.

To make a class into an ActionListener, add implements ActionListener to the

heading of the class definition

Define a method named actionPerformed.

register the ActionListener object with the component that will fire the event using the method addActionListener(..) The actionPerformedMethod of the ActionListener class will

be called every time the object fires an event. This is an important step that must not be forgotten.

(A component may register with more than one listener.)

Page 14: Java Swing  Recitation – 11/(20,21)/2008

Buttons and an Action Listener

Page 15: Java Swing  Recitation – 11/(20,21)/2008

The actionPerformed Method

An actionListener class must have a method named actionPerformed that has one parameter of type ActionEvent.

syntaxpublic void actionPerformed(ActionEvent e){

Code_for_Actions_Performed}

Page 16: Java Swing  Recitation – 11/(20,21)/2008

Method setActionCommand

Every object that fires an action event has an associated string known as the action command for that component.

e.getActionCommand() returns the action command for the component that fired the event e.

The default action command for a button is its name.

Method setActionCommand(String) can be used to change the action command for the object.

Page 17: Java Swing  Recitation – 11/(20,21)/2008

.Example with buttons

Page 18: Java Swing  Recitation – 11/(20,21)/2008

Example with buttons

Page 19: Java Swing  Recitation – 11/(20,21)/2008

The JPanel Class

A GUI can be organized hierarchically, with window-like containers inside other containers.

Components can be placed in a JPanel which can be placed in another JPanel, … which can be placed in a JFrame.

E.g, to place two components in BorderLayout.SOUTH for example, simply place the two components in a panel and place the panel in the BorderLayout.SOUTH position.

The panel has its own layout manager.

Page 20: Java Swing  Recitation – 11/(20,21)/2008

Example with JPanelContainer contentPane = getContentPane ();contentPane.setBackground (Color.BLUE); contentPane.setLayout (new BorderLayout ()); JPanel buttonPanel = new JPanel (); buttonPanel.setBackground (Color.WHITE); buttonPanel.setLayout (new FlowLayout ());

JButton stopButton = new JButton ("Red");stopButton.setBackground (Color.RED);stopButton.addActionListener (this); buttonPanel.add (stopButton);

JButton goButton = new JButton ("Green");goButton.setBackground (Color.GREEN);goButton.addActionListener (this); buttonPanel.add (goButton);

contentPane.add (buttonPanel, BorderLayout.SOUTH); Adding panel to the frame

Adding a green button to the panel

Adding a red button to the panel

Creating a panel with flow layout

Page 21: Java Swing  Recitation – 11/(20,21)/2008

The JPanel Class, cont.

Panel with two buttons

since BorderLayout limits the GUI to five objects, there are usually JPanels (five or fewer) in the pane.

Page 22: Java Swing  Recitation – 11/(20,21)/2008

The Container Class

An object of a class which descends from class Container is called a container class and can have components added to it.

Examples – JFrame is a descendent of class Container, permitting

any JFrame object to hold labels, buttons, panels, and other components.

JPanel is a descendent of class Container, permitting any JPanel object to hold labels, buttons, other panels, and other components.

Page 23: Java Swing  Recitation – 11/(20,21)/2008

Text I/O using JTextField

Create a text field with some initial text JTextField textfield = new JTextField("Initial Text");

Create a text field with some initial text and a default number of columns.

The number of columns controls the preferred width of the component

textfield = new JTextField("Initial Text", cols);

Use textfield.getText() to return the written text

Page 24: Java Swing  Recitation – 11/(20,21)/2008

Text I/O using JTextArea

Create a text area with some initial text

JTextArea textarea = new JTextArea("Initial Text");

Create a text area with some initial text and a default number of rows and columns. This number of rows and columns controls the preferred width and height of the component;

textarea = new JTextArea(“InitialText", rows, cols);

Page 25: Java Swing  Recitation – 11/(20,21)/2008

Quiz

GUI components can be added to an object ofany class that descends from the _______class.