Java Reference
In-Depth Information
Chapter 12
Layout Management and GUI Components
12.1 Creating Buttons ....................................................................... 267
12.2 Flow Layout ............................................................................ 268
12.3 Border Layout .......................................................................... 269
12.4 Text Fields and Labels ................................................................. 270
12.5 Grid Layout ............................................................................. 273
12.6 Creating Text Areas with Scroll Bars .................................................. 275
12.7 The Combo Box ........................................................................ 276
12.8 Check Boxes ............................................................................ 279
12.9 Radio Buttons
..........................................................................
281
12.10 Document Listeners ..................................................................... 282
12.11 Creating Dialog Boxes .................................................................. 284
12.12 Working with Password Fields ......................................................... 285
12.13 Summary ................................................................................ 288
12.14 Syntax
..................................................................................
288
12.15 Important Points
.......................................................................
290
12.16 Exercises
................................................................................ 291
12.17 Lab
......................................................................................
292
12.18 Project
.................................................................................. 292
So far, we have seen how to create menus and how to draw graphics and pictures on a panel.
In this chapter, we will see how to display different graphic user interface (GUI) components
inside a window. These include buttons, labels, text fields, text areas, combo boxes, check
boxes, radio buttons, and so on. However, the goal of this chapter is not to list all GUI
components that are available in Swing. Rather, we will list the main GUI components
and we will show how to use them as part of building working software. We will also show
how the components can be arranged inside a window and we will explore the flow layout ,
border layout ,and grid layout . As part of presenting the material, working code for simple
programs, such as converting Celsius to Fahrenheit, a calculator program, and a poor man's
version of Notepad, will be presented.
12.1 Creating Buttons
The simplest type of a GUI component is a button. A button has text that is displayed on
it. We will start with a very simple application that displays a button. Initially, the button
will have the text Press me please . Once the button is pressed, its text will change to
Thank you! .
import java .awt. event .
;
import javax . swing . ;
public class ButtonGame {
public static void main(String [] args)
{
MyFrame f = new MyFrame ( ) ;
267
 
Search WWH ::




Custom Search