What We Will Cover
Illuminations
Homework Questions?
Homework Discussion Questions
- How easy was it to draw simple shapes using a
Graphics2D object?
- Did anyone use the
Turtle class?
What to take next?
- Schedule of Classes
- After completing CS-20J you are qualified to take:
- CS-19: C++ programming and software design methodologies
- CS-21: Introduction to Data Structures and Algorithms (Spring 2011 and needs Math 5A)
- CS-23: Discrete Mathematics (Spring 2012 and needs Math 5A)
- CS-24: Elementary Computer Organization (Fall 2010)
- CS-19 has an initial review section such that you could learn the syntax with some effort
^ top
12.1: Graphical-User Interfaces
Learner Outcomes
At the end of the lesson the student will be able to:
- Describe the three categories of GUI components
- Collect user input in an input dialog box
- Show messages using a message dialog
|
^ top
12.1.1: About Graphical User Interfaces (GUIs)
- Graphical User Interface (GUI) -- pronounced "gooey"
- Graphical: not just text or characters but includes windows, menus, buttons, etc.
- User: person using the program via mouse, keyboard, etc.
- Interface: interaction with the program using visual controls, widgets, etc.
- GUIs present a user-friendly way to interact with a program
- Most modern programs use a GUI
- Typical graphical components include:
- Window: portion of screen providing an area for the other components
- Button: looks like a button that can be pressed
- Labels: Displays text or images in the window
- Text field: place where users can type values
Example GUI
Java's GUI Libraries
- GUIs are built from objects called components:
- Also called controls or widgets
- Users interact with GUI components using a mouse, keyboard or other input device
- Java provides two sets of GUI components: AWT and Swing
- AWT (Abstract Window Toolkit) is the older set of components:
- Relies on the underlying visual components of the operating system
- GUIs look different on every operating system
- Swing (like the dance) is the newer set of components:
- Extends AWT components and adds new capabilities
- Components draw their own visual shapes on the screen
- GUIs look about the same on every operating system
- The inheritance hierarchy below shows how Swing extends AWT
- Note that Swing components all start with the letter "J"
- We will focus on how to use Swing components
Inheritance Hierarchy for GUI Components

^ top
12.1.2: Component Categories
- GUI components fall into three categories as shown below
Top-Level Containers
- Basic window structure that ties the GUI into the operating system
- Includes capabilities such as borders and resizability
- Holds intermediate containers and atomic components
- Every GUI program must have one top-level container
- Some of the frequently used components are:
| Component |
Description |
| JFrame |
A top-level window with a title, border and buttons for closing, maximizing/restoring and iconifying the window. |
| JApplet |
Container for applets, which are small programs that run inside a Web browser |
| JOptionPane |
Limited window that makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. |
Intermediate Containers
- Graphical object displayed in a top-level container
- Can also hold other intermediate containers and atomic components
- Used to simplify placement of controls
- Some of the frequently used components are:
| Component |
Description |
| JPanel |
Provides a general-purpose container for atomic components and other intermediate containers. |
| JRootPane |
A container used behind the scenes by JFrame, JApplet and other containers. |
| JScrollPane |
A container that provides a scrollable view of a component. |
Controls
- Graphical objects used to accept input or display information
- Must be placed in a container using the
add method
- Some of the frequently used components are:
| Component |
Description |
| JButton |
Display area that triggers an event when clicked. |
| JComboBox |
Drop-down list of items from which the user can make a selection by clicking an item in the list or possibly by typing into a box. |
| JCheckBox |
GUI component that can be selected or deselected and displays its state to the user as a checked square. |
| JRadioButton |
GUI component that can be selected or deselected and displays its state to the user as a filled or unfilled circle. |
| JLabel |
Display area for text or icons |
| JTextField |
Single-line display and input area for the user to enter data from the keyboard. |
| JTextArea |
Multi-line display and input area for the user to enter data from the keyboard. |
Further Information
^ top
12.1.3: Example GUI Using JOptionPane
Example Dialog Application
- Lets create a simple program to ask for and display a name
- We will use three types of dialog boxes in sequence:
showInputDialog: ask for name
showConfirmDialog: verify entry
showMessageDialog: display results
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import javax.swing.JOptionPane;
public class NameApp {
public static void main(String[] args) {
String inputString = JOptionPane.showInputDialog(
"Enter your first name: ");
int choice = JOptionPane.showConfirmDialog(null,
"Are you sure?");
String message = "First name: " + inputString + "\n"
+ "Choice: " + choice + "\n"
+ "Press enter to exit.";
JOptionPane.showMessageDialog(null, message);
System.exit(0);
}
}
|
Input with showInputDialog()
Input with showConfirmDialog()
Output with showMessageDialog()
Commonly-Used Methods of JOptionPane
^ top
12.1.4: Dialog Arguments
- Most methods of
JOptionPane are overloaded to accept arguments
- These options control dialog placement, messages and icons displayed, and title-bar wording
- Both
showInputDialog and showMessageDialog accept the arguments listed in the table below
showInputDialog can accept four arguments:
JOptionPane.showInputDialog(parentComponent, messageString,
titleString, messageTypeInt);
- To display a different dialog without an input field:
JOptionPane.showMessageDialog(parent, message);
- If the parent component is unknown, you can use
null for that argument
JOptionPane.showMessageDialog(null, "Hello, world!");
- You will need to look up the exact usage in the JOptionPane API documentation
Commonly-Used Arguments in JOptionPane Methods
| Argument |
Description |
| parentComponent |
An object representing the frame of the dialog box and controls its placement on the screen. If null is used, the dialog is centered on the screen. |
| messageString |
A descriptive message displayed in the dialog box. |
| titleString |
A title typically placed in the title bar above the dialog box. |
| messageTypeInt |
Indicates the type of icon to display in the dialog box. Choosing one of the message types shown below will display the corresponding icon. |
| icon |
Used if you want to display your own icon rather than a built-in icon. |
Icons provided by JOptionPane
| Icon |
Message Type |
Description |
 |
ERROR_MESSAGE |
Displays an error icon. |
 |
INFORMATION_MESSAGE |
Displays an information icon. |
 |
WARNING_MESSAGE |
Displays an warning icon. |
 |
QUESTION_MESSAGE |
Displays an question icon. |
| |
PLAIN_MESSAGE |
Does not display an icon. |
More Information
^ top
12.1.5: Summary
Check Yourself
- What are the three types of GUI components?
- What package(s) must be imported to create dialogs?
- How many statements are typically needed to code a dialog box?
^ top
Exercise 12.1
Take one minute to prepare answers to the above questions.
^ top
12.2: Building a Simple GUI Application
Learner Outcomes
At the end of the lesson the student will be able to:
- Work with frames
- Add a panel to a frame
- Add buttons to a panel
- Handle button events
|
^ top
12.2.1: Working With JFrames
Example JFrame
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import javax.swing.*;
public class HelloFrame extends JFrame {
public static final int X_LOC = 100, Y_LOC = 100,
WIDTH = 300, HEIGHT = 150;
public HelloFrame() {
super("Hello Frame Application");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(X_LOC, Y_LOC, WIDTH, HEIGHT);
setVisible(true);
}
public static void main(String[] args) {
new HelloFrame();
}
}
|
Commonly Used Methods of the Frame Class
Commonly Used Methods of the Window Class
| Method |
Description |
| hide() |
Hides this window without closing it. |
| pack() |
Size this window to fit the preferred size and layout of its components. |
| show() |
Makes this window visible. |
Commonly Used Methods of the Component Class
^ top
12.2.2: Adding a Panel to a Frame
Methods to Add Components to the Content Pane
^ top
12.2.3: Adding Buttons to a Panel
Example Application with Button Components
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import java.awt.*;
import javax.swing.*;
public class HelloFrame2 extends JFrame {
public final static int X_LOC = 100, Y_LOC = 100,
WIDTH = 300, HEIGHT = 150;
private JButton helloButton;
private JButton exitButton;
public HelloFrame2() {
super("Hello Frame Application");
setDefaultCloseOperation(EXIT_ON_CLOSE);
helloButton = new JButton("Hello");
exitButton = new JButton("Exit");
JPanel panel = new JPanel();
panel.add(helloButton);
panel.add(exitButton);
add(panel);
setBounds(X_LOC, Y_LOC, WIDTH, HEIGHT);
setVisible(true);
}
public static void main(String[] args) {
new HelloFrame2();
}
}
|
Commonly Used Constructors of the JButton Class
Some Methods of the JButton Class
^ top
12.2.4: Handling Button Events
- So far the buttons do not respond when the user clicks them
- To make them respond, you need to add code that is executed when a button is clicked
- This is known as event-handling code
- The following only shows how to handle button events
- However, you use the same principles to handle other types of events
How To Handle Action Events
- Specify that a class implements the
ActionListener interface:
public class HelloFrame3 extends JFrame
implements ActionListener {
- Register the
ActionListener object with the button by calling the addActionListener() method:
helloButton.addActionListener(this);
- Implement the
ActionListener interface by coding an actionPerformed() method:
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if (source == helloButton) {
JOptionPane.showMessageDialog(this, "Hello");
} else if (source == exitButton) {
System.out.println("Goodbye!");
System.exit(0);
} else {
System.out.println("Unknown ActionEvent");
}
}
Identifying the Component Firing the Event
Example Application with an Event Handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HelloFrame3 extends JFrame
implements ActionListener {
public final static int X_LOC = 100, Y_LOC = 100,
WIDTH = 300, HEIGHT = 150;
private JButton helloButton;
private JButton exitButton;
public HelloFrame3() {
super("Hello Frame Application");
setDefaultCloseOperation(EXIT_ON_CLOSE);
helloButton = new JButton("Hello");
helloButton.addActionListener(this);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
JPanel panel = new JPanel();
panel.add(helloButton);
panel.add(exitButton);
add(panel);
setBounds(X_LOC, Y_LOC, WIDTH, HEIGHT);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if (source == helloButton) {
JOptionPane.showMessageDialog(this, "Hey");
} else if (source == exitButton) {
System.out.println("Goodbye!");
System.exit(0);
} else {
System.out.println("Unknown ActionEvent");
}
}
public static void main(String[] args) {
new HelloFrame3();
}
}
|
^ top
12.2.5: Using Other Components
- In this section we will look at how to use some more Swing components
- These components are the most commonly used and may be all you need for games
- Also, many other components work like these if you decide to branch out
JLabels
Commonly-Used Constructors and Methods from JLabel
JTextField
Commonly-Used Constructors and Methods from JTextField
| Constructor/Method |
Description |
| JTextField(int) |
Creates an empty text field with the specified number of columns. |
| JTextField(String, int) |
Creates a text field that starts with the text and contains the number of columns. |
| getText() |
Returns the text contained in the text field as a String object. |
| setText() |
Sets the text field to the specified String. |
| setColumns(int) |
Sets the number of columns for the text field. |
Example with Various Components
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
import java.awt.event.*;
import javax.swing.*;
public class MultiComp extends JFrame
implements ActionListener {
public final static int X_LOC = 100, Y_LOC = 100,
WIDTH = 300, HEIGHT = 150;
private JTextField nameField;
private JButton okButton;
private JButton cancelButton;
private static final String INSTRUCTIONS =
"<html>Enter your name and press the OK button."
+ "<br>To skip this step, press the Cancel button";
public MultiComp() {
super("Multiple Components");
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create components
JLabel instructions = new JLabel(INSTRUCTIONS);
nameField = new JTextField(10);
okButton = new JButton("OK");
cancelButton = new JButton("Cancel");
// Create JPanel and add components to the panel
JPanel panel = new JPanel();
panel.add(instructions);
panel.add(nameField);
panel.add(okButton);
panel.add(cancelButton);
add(panel);
// Add action listeners
nameField.addActionListener(this);
okButton.addActionListener(this);
cancelButton.addActionListener(this);
setBounds(X_LOC, Y_LOC, WIDTH, HEIGHT);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if (source == okButton || source == nameField) {
JOptionPane.showMessageDialog(this,
"Hey, " + nameField.getText() + "!");
} else if (source == cancelButton) {
System.out.println("Action canceled");
System.exit(0);
} else {
System.out.println("Unknown ActionEvent");
}
}
public static void main(String[] args) {
new MultiComp();
}
}
|
^ top
12.2.6: Summary
- Most Java GUI applications inherit from a
JFrame:
public class HelloWindow extends JFrame
- To close a
JFrame, you often use the method setDefaultCloseOperation():
setDefaultCloseOperation(EXIT_ON_CLOSE);
- To add components to a
JFrame, you often use a JPanel as an intermediate container
- To add button components, you use the
JButton class
- The code for adding buttons is something like:
JPanel panel = new JPanel();
helloButton = new JButton("Hello");
panel.add(helloButton);
add(panel);
- To make a button respond when clicked, you add an
ActionEvent handler
- There are three steps for adding an
ActionEvent handler:
- Specify that a class implements the
ActionListener interface:
public class HelloFrame3 extends JFrame
implements ActionListener {
- Register the
ActionListener object with the button by calling the addActionListener() method:
helloButton.addActionListener(this);
- Implement the
ActionListener interface by coding the actionPerformed() method:
public void actionPerformed(ActionEvent ae) {
// event handling code
}
- You handle other events using code like that used for the
ActionEvent
Check Yourself
- How does a component, like a
JButton, signal that it was used?
- How does an object become a listener?
- How does an event get passed to a listener object?
- What code is executed when an event occurs?
^ top
Exercise 12.2
Take one minute to prepare answers to the above questions.
^ top
12.3: Laying Out Components
Learner Outcomes
At the end of the lesson the student will be able to:
- Describe the purpose of a layout manager
- Use the Flow layout Manager
- Use the Border layout Manager
- Describe how to organize layout code
|
^ top
12.3.1: About Layout Managers
- When you add multiple components to a GUI, your program has to set the size and position of each component.
- You use a layout manager to provide this functionality
- Each
Container object has a default layout manager
- For instance, a
JPanel uses a FlowLayout manager
- On the other hand, content panes like those used in
JFrame use a BorderLayout manager
- Normally, you only need to think about layout managers when:
- You add components to a container
- You do not like the default layout manager
- To change the layout manager, you use the
setLayout() method of the container
- We will discuss two commonly used layout managers in the next few sections
Method of the Container Class Used to Change Layout Managers
^ top
12.3.2: Using the FlowLayout Manager
- The
FlowLayout manager is the default for a JPanel
- It adds components to the top of a container moving from left to right
- When a container runs out of horizontal space, it starts adding components to a new row
- You can adjust the alignment of the components on a row using the alignment fields of the
FlowLayout class:
FlowLayout.CENTER
FlowLayout.LEFT
FlowLayout.RIGHT
- Unlike some layout managers, the
FlowLayout respects component sizes
Example Using a FlowLayout Manager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
import java.awt.*;
import javax.swing.*;
public class FlowLayoutApp extends JFrame {
public final static int X_LOC = 100, Y_LOC = 100,
WIDTH = 300, HEIGHT = 150;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
public FlowLayoutApp() {
super("Flow Layout Application");
setDefaultCloseOperation(EXIT_ON_CLOSE);
button1 = new JButton("Button One");
button2 = new JButton("Button Two");
button3 = new JButton("Button with a long name");
button4 = new JButton("Button Four");
button5 = new JButton("5");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
add(panel);
setBounds(X_LOC, Y_LOC, WIDTH, HEIGHT);
setVisible(true);
}
public static void main(String[] args) {
new FlowLayoutApp();
}
}
|
Commonly Used Constructors of the FlowLayout Class
| Constructor |
Description |
| FlowLayout() |
Constructs a new FlowLayout with a centered alignment. |
| FlowLayout(intAlign) |
Constructs a new FlowLayout with the specified alignment. |
More Information
^ top
12.3.3: Using the BorderLayout Manager
- You can align buttons left or right with a
FlowLayout manager
- However, you cannot place buttons at the bottom of the container
- To do that, you can use the
BorderLayout manager
- With a
BorderLayout manager, you can place components in 5 separate regions:

- Each region can hold only a single component
Adding and Using a BorderLayout Manager
- To create a new
BorderLayout, you typically call its no-parameter constructor:
BorderLayout bl = new BorderLayout()
- To add the
BorderLayout to a container you use the setLayout() method:
panel.setLayout(new BorderLayout());
- The default layout manager of a content pane is
BorderLayout
- To add a component to a container using a
BorderLayout, you specify:
add(ComponentObj, regionField);
- If you do not specify the region, the component is added to the center region
- Note that
BorderLayout ignores the preferred sizes of components
- Thus, a component will adjust itself to fit the size of the region in which it is placed
Example Using a BorderLayout Manager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
import java.awt.*;
import javax.swing.*;
public class BorderLayoutApp extends JFrame {
public final static int X_LOC = 100, Y_LOC = 100,
WIDTH = 300, HEIGHT = 150;
private JButton buttonNorth;
private JButton buttonSouth;
private JButton buttonEast;
private JButton buttonWest;
private JButton buttonCenter;
public BorderLayoutApp() {
super("BorderLayout Application");
setDefaultCloseOperation(EXIT_ON_CLOSE);
buttonNorth = new JButton("North");
buttonSouth = new JButton("South");
buttonEast = new JButton("East");
buttonWest = new JButton("West");
buttonCenter = new JButton("Center");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(buttonNorth, BorderLayout.NORTH);
panel.add(buttonSouth, BorderLayout.SOUTH);
panel.add(buttonEast, BorderLayout.EAST);
panel.add(buttonWest, BorderLayout.WEST);
panel.add(buttonCenter, BorderLayout.CENTER);
add(panel);
setBounds(X_LOC, Y_LOC, WIDTH, HEIGHT);
setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutApp();
}
}
|
Commonly Used Constructors of the BorderLayout Class
More Information
^ top
12.3.4: Combining Layout Managers
- The
FlowLayout manager lets you align components left, center or right
- However, it does not let you place components at the bottom of the container
- On the other hand, the
BorderLayout manager lets you put components at the bottom
- But, you can only place one component in the bottom region
- To get multiple components at the bottom of a window, you must combine layout managers

- Since you can only have one layout manager per container, you must have multiple containers to get the effect of multiple layouts
- The following code shows how you add two layouts together
- The first container is named
panel and uses a BorderLayout:
buttonNorth = new JButton("North");
buttonEast = new JButton("East");
buttonWest = new JButton("West");
buttonCenter = new JButton("Center");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(buttonNorth, BorderLayout.NORTH);
panel.add(buttonEast, BorderLayout.EAST);
panel.add(buttonWest, BorderLayout.WEST);
panel.add(buttonCenter, BorderLayout.CENTER);
- The second container is named
flowPanel and uses a FlowLayout:
label = new JLabel("Label text", JLabel.LEFT);
button = new JButton("Button text");
JPanel flowPanel = new JPanel();
flowPanel.setLayout(
new FlowLayout(FlowLayout.RIGHT));
flowPanel.add(label);
flowPanel.add(button);
- We then add the
secondPanel to the firstPanel:
panel.add(flowPanel, BorderLayout.SOUTH);
- Thus you combine layouts by nesting one container inside another container
- The complete code is shown below
Example Using a Combination of Layout Managers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import java.awt.*;
import javax.swing.*;
public class CombinedLayoutApp extends JFrame {
public final static int X_LOC = 100, Y_LOC = 100,
WIDTH = 300, HEIGHT = 150;
private JButton buttonNorth;
private JButton buttonEast;
private JButton buttonWest;
private JButton buttonCenter;
private JButton button;
private JLabel label;
public CombinedLayoutApp() {
super("Combined Layout Application");
setDefaultCloseOperation(EXIT_ON_CLOSE);
buttonNorth = new JButton("North");
buttonEast = new JButton("East");
buttonWest = new JButton("West");
buttonCenter = new JButton("Center");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(buttonNorth, BorderLayout.NORTH);
panel.add(buttonEast, BorderLayout.EAST);
panel.add(buttonWest, BorderLayout.WEST);
panel.add(buttonCenter, BorderLayout.CENTER);
label = new JLabel("Label text", JLabel.LEFT);
button = new JButton("Button text");
JPanel flowPanel = new JPanel();
flowPanel.setLayout(
new FlowLayout(FlowLayout.RIGHT));
flowPanel.add(label);
flowPanel.add(button);
panel.add(flowPanel, BorderLayout.SOUTH);
add(panel);
setBounds(X_LOC, Y_LOC, WIDTH, HEIGHT);
setVisible(true);
}
public static void main(String[] args) {
new CombinedLayoutApp();
}
}
|
^ top
12.3.5: Other Layout Managers
Java provides other layout managers you can research and use including:
- BoxLayout: positions components in a single row or column
- CardLayout: lets you create an area that can contain different components at different times
- GridBagLayout: places components in a grid and allows some components to span more than one cell
- GridLayout: displays components with equal size in a row-column layout like a spreadsheet
- GroupLayout: displays components with horizontal and vertical layouts defined separately
- SpringLayout: allows you to specify precise relationships between the edges of components
More Information
^ top
12.3.6: Absolute Positioning
- It is possible to do without a layout manager and use absolute positioning
- However, you should use a layout manager if at all possible
- Not using a layout manager creates a rigid and inflexible user interface
- If you allow the user to resize a frame, then components can get hidden
- To show the components optimally, you can make the frame a fixed size and not resizeable
- However, a fixed-size frame can interfere with other applications the user is running
- To get a feel for these limitations, run the following example
Example of Absolute Positioning
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import java.awt.*;
import javax.swing.*;
public class AbsoluteApp extends JFrame {
public final static int X_LOC = 100, Y_LOC = 100,
WIDTH = 300, HEIGHT = 170;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
public AbsoluteApp() {
super("Absolute Positioning Application");
setDefaultCloseOperation(EXIT_ON_CLOSE);
button1 = new JButton("Button One");
button2 = new JButton("Button Two");
button3 = new JButton("Button with a long name");
button4 = new JButton("Button Four");
button5 = new JButton("5");
button1.setBounds(10, 10, 100, 50);
button2.setBounds(120, 10, 100, 50);
button3.setBounds(230, 10, 150, 50);
button4.setBounds(10, 70, 100, 50);
button5.setBounds(120, 70, 50, 50);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
add(panel);
setBounds(X_LOC, Y_LOC, WIDTH, HEIGHT);
setVisible(true);
}
public static void main(String[] args) {
new AbsoluteApp();
}
}
|
More Information
^ top
12.3.7: Summary
- When you add multiple components to a GUI, your program has to set the size and position of each component
- You use a layout manager to provide this functionality
- Normally, you only need to think about layout managers when:
- You add components to a container
- You do not like the default layout manager
- To set a new layout for a container, you use the
setLayout() method
- To set a new
FlowLayout for a panel, you code something like:
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
- To set a new
BorderLayout for a panel, you code something like:
panel.setLayout(new BorderLayout());
- To add a component to a container using a
BorderLayout, you code something like:
panel.add(button1, BorderLayout.NORTH);
- If you do not specify the region, the component is added to the center region
- You can combine layout managers using multiple containers
- One container is added to another container
firstPanel.add(secondPanel, BorderLayout.SOUTH);
- Java has other layout managers you can use:
- Also, you can remove the layout manager and set all the component positions and sizes individually
- However, you produce more professional and user friendly applications by using a layout manager
^ top
Exercise 12.3
Take one minute to prepare answers to the following questions:
- How are components positioned with a
BorderLayout manager?
- How are components positioned with a
FlowLayout manager?
^ top
Wrap Up
Due Next: A10-Graphics and Recursion (5/5/10)
Course Project (5/26/10) Work on your project!
^ top
Home
| Blackboard
| Schedule
| Room Policies
| Syllabus
Help
| FAQ's
| HowTo's
| Links
Last Updated: May 05 2010 @12:33:58
|