17
A First Look at GUI Applications Part IV: Radio Buttons 1

A First Look at GUI Applications Part IV: Radio Buttons 1

Embed Size (px)

Citation preview

A First Look at GUI Applications

Part IV: Radio Buttons

1

Contents

A. What is Radio Button?B. Metric Converter Example

2

A. What is Radio Button?

Radio buttons allow the user to select one choice from several possible options

The JRadioButton class is used to create radio buttons

JRadioButton constructors:• JRadioButton(String text)• JRadioButton(String text, boolean selected)

Method isSelected() is for checking selection An instance of the ButtonGroup class is used to

group radio buttons

3

B. Metric Converter Example

Reuse the kilometer converter example Convert the distance to feet:

1 kilometer = 3281 feet Convert the distance to inches:

1 kilometer = 39370 inches The new interface is as below:

4

B. Metric Converter Example

1. Enhancing the Design2. Enhancing the KilometerConverterTest Class3. Enhancing the KilometerConverter Class4. Creating the Interface

5

1. Enhancing the Design

6

2. Enhancing the KilometerConverterTest Class

7

3. Enhancing the KilometerConverter Class

8

4. Creating the Interface

4.1. Creating the Activating Class4.2. Creating the Window

9

4.1. Creating the Activating Class

Class name: MetricConverterProgram The window’s basic properties:

• Class name: MetricConverter• Title: Metric Converter• Size: 400x100

10

4.2. Creating the Window

4.2.1. Generating the Window4.2.2. Creating the Components4.2.3. Grouping the Radio Buttons4.2.4. Converting to Miles4.2.5. Converting to Feet4.2.6. Converting to Inches

11

4.2.1. Generating the Window

12

4.2.2. Creating the Components

Use FlowLayout for this case

13

4.2.3. Grouping the Radio Buttons

Only one radio button is chosen at the same time

14

4.2.4. Converting to Miles

15

4.2.5. Converting to Feet

16

4.2.6. Converting to Inches

17