O'Reilly logo

Learning Java Through Games by Lubomir Stanchev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 Lab els ................................................................. 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,andgrid 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 c la ss ButtonGame {
public s ta tic void main( String [ ] args ) {
MyFrame f = new MyFrame ( ) ;
267
268 Learning Java through Games
f . setVisible(true);
}
}
class MyFrame extends JFrame {
public MyFrame ( ) {
final JButton button = new JButton ( " Press me please " );
button . addActionListener (new ActionListener () {
public void actionPerformed ( ActionEvent e ) {
button . setText (" Thank You!" );
}
});
add ( button ) ;
pack ( ) ;
}
}
First, note that no panel is created. The button is added directly to the window. It
turns out that every window has a default canvas to which one can add GUI components.
However, if we want to use the paintComponent() method to draw something, then we need
to create a JPanel. A button belongs to the JButton class (we have the letter J because
we are using Swing). As the example shows, the constructor of the JButton class takes
as input the text of the button. An empty constructor that creates a button with no text
is also available. A button is a very simple component. The only thing that can happen
in the life of a button is that it is pressed. Therefore, a button generates action events
when it is being pressed. One can register an event listener with a button by calling the
addActionListener method. When the button is pressed, the actionPerformed method
of the event listener is called. The setText method changes the text of the button. Note
that most of the GUI components that we will examine in this chapter belong to Swing.
Remember that all classes in Swing start with the letter J.
The pack method is used to automatically set the size of the window. The method
evaluates the size of the components inside the window and sets the window size accordingly.
Note that the method should be called after all the components are added to the window. Go
ahead and run the application. Note that when the window is resized, the button continues
to take the whole window.
12.2 Flow Layout
The flow layout is the simplest layout and it is the default layout for panels. The idea
of the flow layout is that the GUI components fill the container starting from the top left
corner and filling it line by line. To see how this works, let us create a very simple application
that displays three buttons that do nothing.
import java . awt . ;
import javax . swing . ;
public c la ss Test {
public s ta tic void main( String [ ] args ){
MyFrame f = new MyFrame ( ) ;
f . setVisible(true);
}

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required