java swing recitation – 11/(20,21)/2008 cs 180 department of computer science, purdue university

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

Post on 19-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

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

CS 180

Department of Computer Science,

Purdue University

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.

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.

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

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

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

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.

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.

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

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.

Adding Buttons

A button is created usingJButton Button_Name = new

JButton(“Button_Label”);

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

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.

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.)

Buttons and an Action Listener

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}

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.

.Example with buttons

Example with buttons

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.

Example with JPanel

Container 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

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.

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.

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

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);

Quiz

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