JPanel
The JPanel class provides general-purpose containers for lightweight components.
You can refer to How to Use Panels.
What is the difference between JPanel and JFrame?
You can add JPanel to a JFrame.
You can't add JFrame to other containers.
What's so good about JPanel ?
-
You can use it to create more complicated components.
-
Use as an intermediate container to contain other components.
Titanic.java
You can get the program here.
Download the titanic picture here .
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class Titanic extends JFrame {
5: private JPanel panel;
6: private JLabel movieLabel;
7: private JLabel directorLabel;
8: private JLabel actorLabel;
9: private JLabel actressLabel;
10: private JLabel moviePictureLabel;
11: private Container contentPane;
12: public Titanic() {
13: super("Titanic"); // set the title to titanic;
14: contentPane = getContentPane();
15: setUpLabels(); // set up the labels, see the method below
16: int col = 1;
17: // create a panel with GridLayout
18: panel = new JPanel(new GridLayout(0,col));
19: // add labels to the panel
20: panel.add(movieLabel);
21: panel.add(directorLabel);
22: panel.add(actorLabel);
23: panel.add(actressLabel);
24: // and panel to west side of the frame
25: contentPane.add(panel, BorderLayout.WEST);
26: // add moviePictureLabel to the center of the frame
27: contentPane.add(moviePictureLabel);
28:
29: }
30:
31: private void setUpLabels() {
32: movieLabel = new JLabel("Movie : Titanic");
33: Color foregroundColor = Color.red;
34: Color backgroundColor = Color.blue;
35: movieLabel.setOpaque(true); // set the label opaque means
36: // the label ignores the
37: // background for the parent container.
38: // that make it more
39: // efficent to draw
40: movieLabel.setForeground(foregroundColor); // set the color of the text
41: movieLabel.setBackground(backgroundColor); // setbackground color
42:
43: directorLabel = new JLabel("Director : James Cameron");
44: directorLabel.setOpaque(true);
45: directorLabel.setForeground(foregroundColor); // set the color of the text
46: directorLabel.setBackground(backgroundColor); // setbackground color
47:
48: actorLabel = new JLabel("Actor : Leonardo DiCaprio");
49: actorLabel.setOpaque(true);
50: actorLabel.setForeground(foregroundColor); // set the color of the text
51: actorLabel.setBackground(backgroundColor); // setbackground color
52:
53: actressLabel = new JLabel("Actress : Kate Winslet");
54: actressLabel.setOpaque(true);
55: actressLabel.setForeground(foregroundColor); // set the color of the text
56: actressLabel.setBackground(backgroundColor); // setbackground color
57:
58: moviePictureLabel = new JLabel(new ImageIcon("titanic.jpg"));
59: moviePictureLabel.setOpaque(true);
60: moviePictureLabel.setBackground(backgroundColor); // setbackground color
61: }
62:
63: public static void main(String[] args) {
64: Titanic t = new Titanic();
65: t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66: t.pack();
67: t.setVisible(true);
68: }
69:
70:}
71:
Explanation :
line 5: declares a JPanel.
line 6-10: declares different labals.
line 18 : constructor for JPanel. the layout is set to GridLayout with 1
column.
line 20 - 23 : add labels to the panel.
line 31 - 61 : initial the labels, set background color by
setBackground, set foreground color by setForeground
and also use setOpaque(true) to set it opaque (not transparent).
I don't specific a layout for JFrame. The default layout for JFrame is
BorderLayout.
Like other containers, a panel uses a layout manager
to position and size its components.
By default, a panel's layout manager
is an instance of FlowLayout,
which places the panel's contents in a row.
You can easily make a panel use any other layout manager
by invoking the setLayout method
or specifying a layout manager when creating the panel.
Here is an example of the first approach:
JPanel aPanel = new JPanel();
aPanel.setLayout(new BorderLayout());
Here is an example of setting the layout manager
at instantiation:
JPanel aPanel = new JPanel(new BorderLayout());
When you add components to a panel,
you use the add method.
Exactly which arguments you specify to the add method
depend on which layout manager the panel uses.
When the layout manager is FlowLayout, BoxLayout,
GridLayout,
or GridBagLayout,
you'll typically use the one-argument add method, like this:
aFlowPanel.add(aComponent);
aFlowPanel.add(anotherComponent);
When the layout manager is BorderLayout,
you need to provide a second argument
specifying the added component's position within the panel.
For example:
aBorderPanel.add(aComponent, BorderLayout.CENTER);
aBorderPanel.add(anotherComponent, BorderLayout.SOUTH);
Creating a JPanel
| Constructor
|
Purpose
|
JPanel()
JPanel(LayoutManager)
|
Create a panel.
The LayoutManager parameter provides a
layout manager for the new panel.
By default, a panel uses a FlowLayout
to lay out its components.
|
Managing a Container's Components
| Method
|
Purpose
|
void add(Component)
void add(Component, int)
|
Add the specified component to the panel.
When present, the int
parameter is the index of the component
within the container.
By default, the first component added is at index 0,
the second is at index 1, and so on.
|
void remove(Component)
void remove(int)
void removeAll()
|
Remove the specified component(s).
|
Setting/Getting the Layout Manager
| Method
|
Purpose
|
void setLayout(LayoutManager)
LayoutManager getLayout()
|
Set or get the layout manager for this panel.
The layout manager is responsible for positioning the
panel's components within the panel's bounds according
to some philosophy.
|