Graphical User Interface In Java - Software Development Methods | CS 314, Study notes of Computer Science

Material Type: Notes; Professor: France; Class: Software Development Methods; Subject: Computer Science; University: Colorado State University; Term: Fall 2007;

Typology: Study notes

Pre 2010

Uploaded on 03/18/2009

koofers-user-0rs
koofers-user-0rs 🇺🇸

10 documents

1 / 64

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Graphical User Interfaces in Java
CS314
Colorado State University
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40

Partial preview of the text

Download Graphical User Interface In Java - Software Development Methods | CS 314 and more Study notes Computer Science in PDF only on Docsity!

Graphical User Interfaces in Java

CS

Colorado State University

GUI Components

menu bar

button

combo box

menus

scroll bars

Some basic GUI

components

Component
Description
JLabel

An area where uneditable text or icons can be displayed.

JTextField

An area in which the user inputs data from the keyboard. The area can alsodisplay information.

JButton

An area that triggers an event when clicked with the mouse.

JCheckBox

A GUI component that is either selected or not selected.

JComboBox

A drop-down list of items from which the user can make a selection byclicking an item in the list or possibly by typing into the box.

JList

An area containing a list of items from which the user can make a selectionby clicking on any element in the list. Multiple elements can be selected.

JPanel

A container in which components can be placed and organized.

Heavyweight versus Lightweight Components •^

Heavyweight components:

AWT components

(java.awt.Button) use native code.

-^

Lightweight components:

written in pure Java

(more portable).

-^

Most Swing components are lightweight^ – Exceptions: JApplet, JDialog, JFrame, and JWindow

are lightweight.

-^

Developing lightweight (pure Java) components:extend java.awt.Component and override paint():

public

class

LightWeightButton

extends

Component

public

void

paint(Graphics

g)

Java code

goes here */

SwingHierarchy (Part II)

Swing componentsnames start with ‘J’.

AWT and

Swing

-^

Swing’s top-level elements -- JApplet, JDialog,JFrame, and JWindow – inherit from their AWTcounterparts.

-^

The base Swing class (JComponent) is derivedfrom java.awt.Container.^ – Swing components are fundamentally based on the

AWT.

-^

All GUI programs use classes defined in the AWT:– layout managers (java.awt.FlowLayout),– fonts (java.awt.Font),– colors (java.awt.Color).

JLabelJLabelJLabelJLabel

  • Label
    • Provide text on GUI– Defined with class

JLabel

  • Can display:
    • Single line of read-only text• Image• Text and image

3

import

java.awt.*;

4

import

java.awt.event.*;

5

import

javax.swing.*;

6 7

public class

LabelTest

extends

JFrame {

8

private

JLabel label1, label2, label3;

9 10

// set up GUI

11

public

LabelTest()

12

{

13

super

(^ "Testing JLabel"

);

1415

// get content pane and set its layout

17

setLayout(

new

FlowLayout() );

1819

// JLabel constructor with a string argument

20

label1 =

new

JLabel(

"Label with text"

);

21

label1.setToolTipText(

"This is label1"

);

22

add( label1 );

23

50 51

}^ // end class LabelTest

TextFields

  • JTextField
    • Single-line area in which user can enter text
      • JPasswordField
        • Extends

JTextField

  • Hides characters that user enters

// construct textfield with default text,// 20 visible elements and no event handler textField3 =

new

JTextField(

"Uneditable text field"

,^20

);

textField3.setEditable(

false

);

add( textField3 ); // construct passwordfield with default text passwordField =

new

JPasswordField(

"Hidden text"

);

add( passwordField ); // register event handlers TextFieldHandler handler =

new

TextFieldHandler();

textField1.addActionListener( handler );textField2.addActionListener( handler );textField3.addActionListener( handler );passwordField.addActionListener( handler );setSize(

325

,^100

);

setVisible(

true

);

}^ // end constructor TextFieldTest public static void

main( String args[] )

{ TextFieldTest application =

new

TextFieldTest();

application.setDefaultCloseOperation(

JFrame.EXIT_ON_CLOSE

);

}

// private inner class for event handling private class

TextFieldHandler

implements

ActionListener {

// process textfield events public void

actionPerformed( ActionEvent event )

{ String string =

// user pressed Enter in JTextField textField1 if^ ( event.getSource() == textField1 )string =

"textField1: "

  • event.getActionCommand();

// user pressed Enter in JTextField textField2 else if

( event.getSource() == textField2 ) string =

"textField2: "

  • event.getActionCommand();

// user pressed Enter in JTextField textField3 else if

( event.getSource() == textField3 ) string =

"textField3: "

  • event.getActionCommand();

// user pressed Enter in JTextField passwordField else if

( event.getSource() == passwordField ) { string =

"passwordField: "

new

String( passwordField.getPassword() );

}

How Event Handling Works

-^

Two open questions^ – How did event handler get registered?

  • Answer:
    • Through component’s method

addActionListener

  • Lines 39-42 of

TextFieldTest.java

  • How does component know to call

actionPerformed

?

  • Answer:
    • Event is dispatched only to listeners of appropriate type– Each event type has corresponding event-listener interface

» Event ID specifies event type that occurred