GUI 4 - Tiny Window with a JLabel
- Using a JPanel to hold components.
- Setting the layout of the content pane to FlowLayout.
- Creating a JLabel and adding it to the panel.
- Setting the window's content pane to the panel, and doing the layout.
JLabel
JLabel(title) // Constructor
JPanel
JPanel() // Constructor setLayout(layout) // Sets JPanel's layout add(component) // Adds component to panel.
JFrame
setContentPane(...) // Sets JFrame's content pane. pack() // Perform the layout. setLocationRelativeTo(null) // Center window on screen.
FlowLayout
FlowLayout() // Constructor
The main program is like the previous example, after changing the GUI class name, so it isn't repeated here.
JFrame constructor and methods
|
The previous example called the JFrame constructor, activated the close box,
set the title bar, and made the window visible.
This example shows how to center the window on the screen,
and work with the content pane. We'll create a |
JPanel constructor and methods
|
JPanel is used primarily for holding components, so there are
few operations that it performs. You need to call the constructor, tell what
layout to use, and add the components.
|
JLabel constructor and methods
|
Usage: Because there is usually no need to refer to a label again,
it is common to pass it directly to the content.add(new JLabel("Name")); If you want change the font (or text), save the JLabel in a variable. |
FlowLayout - Left to right, top to bottom
Making a good component layout can be a lot of work.
Java uses layout managers to implement various layout strategies.
The simplest is FlowLayout
, which simply centers the components
adding them from left to right, and if necessary flowing them to another line.
This example only shows one component, but you'll see more in the next example.
The only thing you do with a layout object is create it with new
and use it to set the layout of a JPanel
.
Source code - a subclass of JFrame
Here's the source code, which goes through all the normal steps in building a GUI, even though we're adding only one component, a JLabel, to it. |
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 |
// File : gui-tutorial/tw4/TinyWindow4.java // Purpose: Create a window with a JLabel. // Author : Fred Swartz - 2006-11-09 import java.awt.*; // Needed for FlowLayout. //Note 1 import javax.swing.*; ////////////////////////////////////////////////////// class TinyWindow4 class TinyWindow4 extends JFrame { //============================================================= main public static void main(String[] args) { TinyWindow4 window = new TinyWindow4(); window.setVisible(true); } //======================================================= constructor public TinyWindow4() { //... Create content panel, set layout JPanel content = new JPanel(); //Note 2 content.setLayout(new FlowLayout()); // Use FlowLayout //Note 3 //... Add one label to the content pane. JLabel greeting = new JLabel("We come in peace."); //Note 4 content.add(greeting); // Add label //Note 5 //... Set window (JFrame) characteristics setContentPane(content); //Note 6 pack(); // Do layout. //Note 7 setTitle("Tiny Window using JFrame Subclass"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); // Center window. } } |
Notes
- Some elements of the GUI interface are defined in the older AWT package, although the components and containers (starting with "J") are all part of Swing.
- Create a new JPanel to hold the components for the window's content pane.
- A layout manager should be explicitly set for the content pane.
- Create a JLabel.
- Add the label to the content pane. There really was no reason to assign the JLabel to a variable - the new JLabel(..) could have been an argument to add(...).
- The content pane of this JFrame is now set to the JPanel that we created earlier, named "content".
- After the content pane of the JFrame has been set and populated with components, it's necessary to call pack() to do the layout -- it sets the location and size of each component.
Creating a new content pane rather than using the defualt
Every window (JFrame
) has a content pane that arranges
the components which have been added to it according to the specified layout.
The content pane is usually implemented as a JPanel
.
Altho every JFrame comes with a predefined content pane, these examples
create a new JPanel
for the content pane.
If you're interested in the issues and options for handling the content
pane, look at
Content Pane (or Content Pain?),
but it isn't necessary.