chap007

skatechildrenΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

87 εμφανίσεις

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

1

McGraw
-
Hill/Irwin

Chapter 7


Making Selections with Check Boxes and
Option Buttons

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

2

McGraw
-
Hill/Irwin

Objectives


Allow the user to enter yes/no responses using check boxes.


Combine check boxes in a group to act as option buttons.


Test integer conditions with a switch statement.


Incorporate Swing components into an applet.


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

3

McGraw
-
Hill/Irwin

Check Boxes and Option Buttons


Checkboxes

and

option

buttons

are

based

on

the

Checkbox

class
.


When

you

place

check

boxes

in

a

group,

the

checkboxes

turn

into

option

buttons
.


The

checkboxes

and

option

buttons

give

the

user

only

the

choice

of

yes/no

or

true/false

or

on/off
.


If there are multiple checkboxes, each checkbox operates
independently of other. So the user can choose, none or one or
any number he wants.


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

4

McGraw
-
Hill/Irwin

Checkboxes and Option Buttons

Method

Purpose

getCheckboxGroup()

Returns the name of the group or null if
there is no group.

getState()

Returns true er false depending on or off
(selected or deselected).

setCheckboxGroup(CheckboxGroup
group
)

Sets the group for a check box. Can be
set to null group. If the check box was
already in a group and is given a new
group, it is removed from the previous
group.

setState(boolean value)

Sets the component to true or false (on
or off)

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

5

McGraw
-
Hill/Irwin

The Checkbox Component
-

Constructors

Checkbox()

Checkbox(String
label
)

Checkbox (String
label
, CheckboxGroup groupName, boolean state)

Checkbox(String
label
, boolean state, CheckboxGroup groupName)

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

6

McGraw
-
Hill/Irwin

The Checkbox Component
-

Example

Checkbox chkBold = new Checkbox(“Bold”);

Checkbox chkItalic = new Checkbox(“Red”, null, true);

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

7

McGraw
-
Hill/Irwin

Creating Checkboxes


The

default

state

(on

or

off)

of

a

checkbox

is

off
.


The groupName is only used for option buttons.


When you want to create checkboxes (no group name), set the
groupName argument to null and set the initial state.


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

8

McGraw
-
Hill/Irwin

The CheckboxGroup
-

Constructor

CheckboxGroup()

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

9

McGraw
-
Hill/Irwin

The CheckboxGroup
-

Example

CheckboxGroup cbgColor = CheckboxGroup();

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

10

McGraw
-
Hill/Irwin

Creating Option Buttons


Option Buttons Example


//Declare the components


CheckboxGroup cbgColor = new CheckboxGroup();


Checkbox optRed = new Checkbox(“Red”,cbgColor,false);



Checkbox optBlue = new Checkbox(“Blue”,cbgColor,false);


Checkbox optGreen = new Checkbox(“Green”,cbgColor,false);


Checkbox optBlack = new Checkbox(“Black”,cbgColor,false);




//
add to the init


add(optRed)


add(optBlue);


add(optGreen);


add(optBlack);


In the init you add the components but not the group.


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

11

McGraw
-
Hill/Irwin

Checking the State of Check Boxes
and Option Buttons


You can check if the option button is selected or checkbox is
checked using the getState method.


For example chkItalic.getState() will return a Boolean value
true if the checkbox is checked or false if the checkbox is
unchecked.


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

12

McGraw
-
Hill/Irwin

Testing Multiple Checkboxes


If you have multiple checkboxes they all operate independent of
each other.


Therefore, you will use separate if statements to check the state
of each checkbox.


For option buttons they are dependent on each other.


Therefore, you will use if else if statements to check the state of
each option button.


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

13

McGraw
-
Hill/Irwin

Check Boxes and Option Buttons

Check Boxes

Option Buttons

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

14

McGraw
-
Hill/Irwin

ItemListener


You can an ItemListener to each checkbox or option button
that you want to check the change.


Every time the state of a component is changed an event is
triggered.


Like ActionListener, you must implement the ItemListener
interface and add an itemListener to one or more components.


Then write the code for the itemStateChanged method.


When the user clicks in a checkbox component, the code in
the itemStateChanged method executed immediately.

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

15

McGraw
-
Hill/Irwin

ItemListener
Continued


If you add item listeners to several components then you can use
the getSource method to determine which component triggered the
event.


You also need to determine if the object that triggered the event
was selected or deselected.


You usually need to check the source of the event as well as its
state true or false.


For example:


//Is the source the Red option button and is it selected?


if (eventSource == optRed && optRed.getState())


{



setBackground(Color.red);



showStatus("Color: Red");


}


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

16

McGraw
-
Hill/Irwin

The switch Statement


Whenever you want to test a single variable or expression for
multiple values, the switch statement provides a flexible and
powerful solution.


It is easier and cleared to read than nested if statements.


Java switch statement works only with int, char, shor, or byte
expressions.


You can check for an integer or a single character.


The expression can be (intCount


1). The body of the switch
statement must be in braces.


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

17

McGraw
-
Hill/Irwin

The Switch Statement


General
Format

switch(
expression
)

{


case ConstantValue:


statements;


[case Constant value:


statement(s);]





[default:


statement(s);]

}

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

18

McGraw
-
Hill/Irwin

The switch Statement
-

Example

switch(intChoice)

{


case 1:


HandleChoice1();


break;


case 2:


HandleChoice2();


break;


case 3:


HandleChoice3();


break;


default:


showStatus(“Choice must be 1, 2, or 3”);

}

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

19

McGraw
-
Hill/Irwin

The Switch Statement


The default statement is optional and if you omit the default and
none of the cases match then the executions passes through the
switch statement without executing anything.



When the variable meets the condition of the case, it executes the
body of that case but it does not jump out of the switch
statement.


It goes and executes the next body and the next till the end of the
switch statement (all the statements following are executed).


The break statement forces it out of the current block to the
closing brace of the switch statement.


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

20

McGraw
-
Hill/Irwin

Swing Components


These newer components are less dependent on the platform and
have more capabilities than the AWT classes.


Swing components are referred to lightweight components and
AWT components are referred to heavyweight components.


Swing components build graphical components from the library
classes rather than the underlying operating system.


They have three distinct “look and feel” options available.


You can select the Motif look, the Metal look or the Windows
look.

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

21

McGraw
-
Hill/Irwin

Swing Components
Continued


The default look is called the Basic “look and feel.”


The capabilities are buttons have keyboard shortcuts (often called
hot keys or accelerator keys).


You can add icons to buttons and labels and choose the alignment
of labels on check boxes, radio buttons.


Swing components are based on the Container class from the
java.awt. package. Therefore, you still have to import the AWT
package.


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

22

McGraw
-
Hill/Irwin

Swing Components and similar
AWT components

Swing Component

AWT Component

JApplet


Applet

JButton


Button

JLabel


Label

JPanel


Panel

JTextField


TextField

JTextArea


TextArea

JCheckBox


Checkbox

JRadioButton


Checkbox (in a group)

ButtonGroup


CheckboxGroup

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

23

McGraw
-
Hill/Irwin

Multiple Panes


A big advantage of Swing components over AWT involves the
layering of panels.


With AWT one component is hidden over another component or
container.


With Swing you can achieve the visual effect of placing an item
on top of another through use of multiple panes.


The basic pane is the Content Pane. Over that you can have the
Layered Pane and/or the Glass Pane.


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

24

McGraw
-
Hill/Irwin

Setting the Content Pane


The Content Pane must be a container.


Way to set up an applet is to declare a panel, add components to the panel, and set
the panel as the Content Pane.


Use the setContentPane method to set the panel as the Content Pane.


See Example:



//Declare Swing components



JPanel pnlPane = new JPanel(); //Panel for Content Pane



JLabel lblHello = new JLabel("Hello World")



//In the init method:



//Add component to the panel



pnlPane.add(lblHello);



//Make the panel the Content Pane



setContentPane(pnlPane);

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

25

McGraw
-
Hill/Irwin

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

26

McGraw
-
Hill/Irwin

Setting the Content Pane
Continued


An alternative is to skip creating the panel and add the
components directly to the Content Pane.


You can use the getContentPane method which returns the object
that is the ContentPane.


For example : getContentPane().add(lblHello);

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

27

McGraw
-
Hill/Irwin

Using Swing Components



To use swing components, you must import both java.awt.*
and javax.swing.*


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

28

McGraw
-
Hill/Irwin

Running Swing Applet in a
Browser



Most browsers cannot run swing components.


Internet Explorer and Netscape Navigator can run swing
components if they have a plug
-
in installed.


The plug
-
in has to installed only once on a computer, and then
applet can run in either browser assuming the paths are set
correctly.


The plug
-
in cause the browser to use the Java Runtime
Environment in JDK folder rather than the browser JVM.


You will find the plug
-
in in the RunSwingApplet.htm
.

Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

29

McGraw
-
Hill/Irwin

Using Enhanced Properties of
Swing Components



Some of the reasons for using Swing components rather than
AWT components are:


Swing has a more complete set of components.


Swing components have more functionality.


Swing components require fewer system resources.


Swing components more nearly have the look and feel of the
destination system.


With most Swing components, you can add icons, ToolTips,
keyboard shortcuts, and control the look of the borders.


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

30

McGraw
-
Hill/Irwin

ToolTips



To add a
Tooltip

use the setToolTipText method.


Example :

chkBold.setToolTipText("Change the Font Style
label bold");


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

31

McGraw
-
Hill/Irwin

Keyboard Shortcuts


Keyboard Shortcuts are also called access keys or hot keys and
allow the user to select an option of the keyboard or the mouse.


Use the setMnemonic method to set the single letter used for
keyboard access.


Example: btnOK.setMnemonic('o');


In this example, the
O

of
OK

on the button's label appears
underlined. The user can select the button by pressing Alt + O
(uppercase or lowercase).


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

32

McGraw
-
Hill/Irwin

Borders


To add a border to your component, you must import the
border package.


import javax.swing.border.*;.


You can choose from among several types of borders.


Here are examples of some of the possibilities:



pnlChoices.setBorder(new

BevelBorder(BevelBorder.RAISED));



btnOK.setBorder(new EtchedBorder());



btnOK.setBorder(new LineBorder(Color.black, 3));


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

33

McGraw
-
Hill/Irwin

Swing Component Border Types

Border Type

Options Available

Example

BevelBorder

The type of bevel (raised or
lowered) and the colors for
highlight and shadow.

BevelBorder
(bevelBorder.RAISED)

EtchedBorder

Type of etch (raised and lowered)
and the colors for the highlight and
shadow.

EtchedBorder()
//Defaults to lowered

EtchedBorder(EtchedB
order.LOWERED)


LineBorder


Color and width of the line.


LineBorder(Colo
r.blue, 3)


MatteBorder


A tile icon, width of insets, and a
color.


MatteBorder(10,
10,10,10,Color.
green)


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

34

McGraw
-
Hill/Irwin

Radio Buttons



One of the extra Swing components is a radio button, also called
an option button.


Instead of using a check box in AWT, you can use the Swing
JRadioButton.


You can create groups for radio buttons using the ButtonGroup
class.


Examples:




ButtonGroup grpColor = new ButtonGroup();



JRadioButton optRed = new JRadioButton ("Red");



JRadioButton optBlue = new JRadioButton ("Blue");



JRadioButton optGreen = new JRadioButton ("Green");



JRadioButton optGray = new JRadioButton ("Gray");


Programming with Java

© 2002 The McGraw
-
Hill Companies, Inc. All rights reserved.

35

McGraw
-
Hill/Irwin

Initializing Radio Buttons



When you sue radio button, you should initialize their stat (true
or false) in the init method.


If you fail to initialize the buttons, they will appear deselected
initially.


Java will generate a warning message.