+ All Categories
Home > Documents > Graphical User Interfaces

Graphical User Interfaces

Date post: 09-Feb-2016
Category:
Upload: carlo
View: 32 times
Download: 0 times
Share this document with a friend
Description:
Graphical User Interfaces. Allow for interaction with Buttons Menus Text Fields Two Java Libraries to assist in GUI Programming AWT Swing. Text Fields. provide a line of user-updatable text need to - import javax.swing.*; - declare a variable private JTextField input; - PowerPoint PPT Presentation
Popular Tags:
29
Graphical User Interfaces • Allow for interaction with – Buttons – Menus – Text Fields • Two Java Libraries to assist in GUI Programming – AWT – Swing
Transcript
Page 1: Graphical User Interfaces

Graphical User Interfaces

• Allow for interaction with– Buttons– Menus– Text Fields

• Two Java Libraries to assist in GUI Programming– AWT– Swing

Page 2: Graphical User Interfaces

Text Fields• provide a line of user-updatable text• need to

- import javax.swing.*; - declare a variable

private JTextField input;- construct

input = new JTextField( "Enter text here " );- place in a window

- use. For example,String userInput = input.getText();

Page 3: Graphical User Interfaces

• User enters desired text in the text field at the north of the window

• With each click on the canvas, the text is displayed

Page 4: Graphical User Interfaces

import objectdraw.*;import java.awt.*;import javax.swing.*;

public class TextController extends WindowController {private JTextField input;

public void begin() {input = new JTextField( "Enter text here" );Container contentPane = getContentPane();contentPane.add( input, BorderLayout.NORTH );contentPane.validate();

}

public void onMouseClick( Location point ){new Text( input.getText(), point, canvas );

}}

Page 5: Graphical User Interfaces

The Container Class• Graphical components must be put in a

Container to be displayedimport java.awt.*;

Container contentPane = getContentPane();

contentPane.add( aComponent, position );

• Validate to be sure components displayed properly

Page 6: Graphical User Interfaces

Layout of Components in a Container

• Many layout options

• One example: BorderLayout– BorderLayout.NORTH– BorderLayout.EAST– BorderLayout.SOUTH– BorderLayout.WEST– BorderLayout.CENTER

Page 7: Graphical User Interfaces

Buttons

Let’s add a button to our previous program.Clicking on the button clears the canvas.

• Want clicks on the button to trigger a program action

• Requires slightly more programming effort than text fields

Page 8: Graphical User Interfaces

import objectdraw.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;

public class TextButtonController extends WindowController implements ActionListener {

private JTextField input;

public void begin() {input = new JTextField( "Enter text here" );JButton clearButton = new JButton(" Clear canvas ");

Container contentPane = getContentPane();contentPane.add( input, BorderLayout.NORTH );contentPane.add( clearButton, BorderLayout.SOUTH );contentPane.validate();

clearButton.addActionListener(this);}

Page 9: Graphical User Interfaces

public void onMouseClick( Location point ){new Text( input.getText(), point, canvas );

}

public void actionPerformed( ActionEvent evt ) {canvas.clear();

}}

Page 10: Graphical User Interfaces

Action Events

• When a JButton is clicked

– an ActionEvent is generated– actionPerformed is executed

• Just as onMouseClick is executed whenever a user clicks on the canvas

– actionPerformed is provided with a parameter• Contains information about the object that

triggered the event

Page 11: Graphical User Interfaces

Checklist for using JButtons

• Construct the JButton• Add it to the content pane of the

WindowController extension and validate• So that the WindowController extension

responds to events generated by the JButton– Add this as a listener– Make sure WindowController extension implements

ActionListener– Add actionPerformed method

Page 12: Graphical User Interfaces

Combo Boxes• The Swing package’s name for pop-up

menus: JComboBox

• Let’s use one to control the speed of a simple falling ball animation– Three speed options: slow, medium, fast

FallingBall.java

Page 13: Graphical User Interfaces

Using a JComboBox

MenuBallController.java

Page 14: Graphical User Interfaces

Checklist for JComboBoxes• Construct the JComboBox and add selection

options• Add it to the content pane of the

WindowController extension and validate• So that the WindowController extension

responds to events generated– Add this as a listener– Make sure WindowController extension implements

ActionListener– Add actionPerformed method

Page 15: Graphical User Interfaces

Better GUI design: basic tools

• Panels• Layout Managers

Page 16: Graphical User Interfaces

The JPanel

• Provided by Swing• Organize subcomponents

– Add subcomponents to JPanel– Add JPanel to Container

Page 17: Graphical User Interfaces

Organizing JButtons in a JPanel

• Construct the JPanelJPanel southPanel = new JPanel();

• After each JButton is constructed, add it to the JPanel southPanel.add( fastButton );southPanel.add( mediumButton );southPanel.add( slowButton );

• Add the JPanel to the containercontentPane.add(southPanel, BorderLayout.SOUTH);

Page 18: Graphical User Interfaces

FlowLayout

• JPanels use a different layout manager:FlowLayout

• Lays out its components left to right

Page 19: Graphical User Interfaces

Which button?If we make this a listener for each of the three buttons, as we havebefore…actionPerformed method must determine which one clicked

public void actionPerformed(ActionEvent evt) {if (evt.getSource() == slowButton) {speed = SLOW_SPEED;} else if (evt.getSource() == mediumButton) {speed = MEDIUM_SPEED;} else {speed = FAST_SPEED;}if (droppedBall != null) {droppedBall.setSpeed(speed);}

}

Page 20: Graphical User Interfaces

More on Layout Managers

• Determine how GUI components should be displayed

• Do their best to accommodate when user changes window size

• Some useful layout managers:– BorderLayout: adds to sides or center– FlowLayout: adds left to right– GridLayout: divides a container into equally sized

parts• southPanel.setLayout( new GridLayout(1, 3) );

Page 21: Graphical User Interfaces

Sliders

• Used to graphically select values in a range

• Can be vertical or horizontal

• stateChanged method replaces actionPerformed

• Swing’s name: JSlider

Page 22: Graphical User Interfaces

A slider for controlling speedimport objectdraw.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;

public class SliderBallController extends WindowController implements ChangeListener {

private JSlider speedSlider;

public void begin() {speedSlider = new JSlider (JSlider.HORIZONTAL, SLOW_SPEED,

FAST_SPEED, SLOW_SPEED);speedSlider.addChangeListener(this);

}

Page 23: Graphical User Interfaces

A slider for controlling speed

public void stateChanged (ChangeEvent evt) {speed = speedSlider.getValue();If (droppedBall != null) {

droppedBall.setSpeed(speed);}

}}

Complete SliderBallController class

Page 24: Graphical User Interfaces

Checklist for JSliders• Construct the JSlider; provide four parameters:

– Constant that says whether vertical or horizontal– Minimum and maximum integer values for slider– Initial value of the slider

• Add it to the content pane of the WindowController extension and validate

• So that the WindowController extension responds to events generated– Add this as a listener– Make sure WindowController extension implements

ChangeListener– Add stateChanged method

• Import javax.swing.event package

Page 25: Graphical User Interfaces

Labels• JLabel class provided by

Swing• Single line of read-only

text• A passive component -

no event listener required!

• Useful for providing labels for sliders, text fields, etc.

Page 26: Graphical User Interfaces

Using JLabels

• Two ways to construct– Providing text only

JLabel speedLabel = new JLabel("Speed is ");– Providing text and justification

JLabel speedLabel = new JLabel("Speed is ",JLabel.RIGHT);

• Modifiable with setText methodspeedLabel.setText("Speed is " + speed);

LabelBallController.java

Page 27: Graphical User Interfaces

Handling Keystrokes

• Can write Java programs so that every keystroke made by the user generates an event that can be handled

• No need to add special components to our already-existing window

Page 28: Graphical User Interfaces

Handling Keystrokes• Need to associate listener with keyboard events

this.addKeyListener( this ); // in a WindowController extensioncanvas.addKeyListener( this ); // sometimes need this!setFocusable( true );

• Need to make sure listener has appropriate method to handle key events– public void keyPressed ( KeyEvent evt )– public void keyReleased ( KeyEvent evt )– public void keyTyped ( KeyEvent evt )

• KeyEvents include: VK_A – VK_Z, VK_0 – VK_9

Page 29: Graphical User Interfaces

Review• Construct the GUI component

• Add the component to a container (i.e., a panel or the content pane of a window)

• If a listener is needed– Add this as a listener for the GUI component– Make sure the WindowController extension implements

the appropriate listener interface– Add the event-handling methods promised by the

listener interface


Recommended