Layout
You should read
Basic
What is a component?
JLabel
, JButton
are components. I can't give the exact
definition of component, but they are the "atoms" of Java swing application. We use them to
build up complicated applications.
With the exception of top-level containers,
all Swing components whose names begin with "J" descend from the JComponent
class. For example, JPanel, JScrollPane,
JButton, and JTable all inherit from JComponent. However, JFrame doesn't because
it implements a top-level container.
JLabel and JButton
We will concentrate 2 simple components JLabel
and
JButton
.
some useful constructors
JLabel(String text)
JLabel(String text, int horizontalAlignment)
JButton(String text)
LabelFrame.java
You can get your program here .
1:import javax.swing.*;
2:public class LabelFrame extends JFrame {
3: private String text;
4: private JLabel label;
5: private int alignment;
6: public LabelFrame(String text, int alignment) {
7: super(text);
8: label = new JLabel(text, alignment);
9: getContentPane().add(label);
10: setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
11: }
12:
13: public static void main(String[] args) {
14: LabelFrame f1 = new LabelFrame("Left", JLabel.LEFT);
15: // setLocation(x,y) is the set location of the frame, the upper left
16: // corner will be at (x,y)
17: f1.setLocation(100,100);
18: f1.setSize(100,100);
19: f1.setVisible(true);
20: LabelFrame f2 = new LabelFrame("Center", JLabel.CENTER);
21: f2.setLocation(200,100);
22: f2.setSize(100,100);
23: f2.setVisible(true);
24: LabelFrame f3 = new LabelFrme("Right", JLabel.RIGHT);
25: f3.setLocation(300,100);
26: f3.setSize(100,100);
27: f3.setVisible(true);
28: }
29:}
super
At line 7, we call the JFrame
's constructor
JFrame(String title)
to set up the title of JFrame.
You don't have to call JFrame
's constructor everytime. If you don't
insert that line. The compiler will call the construtor with no argument
JFrame()
for you.
Question:
what happens if your superclass doesn't have a constructor with no
argument but you don't call superclass' constructor in the constructors?
JLabel.LEFT ....
JLabel.LEFT
is a static final int
. It has an integer
value 2. But we never use new JLabel("hi", 2)
because the code is
not readable. Who knows what the 2 stands for? Besides what happens if the Java
designer decides to change the value of JLabel.LEFT
?
So a better way to write this is new JLabel("hi", JLabel.LEFT)
.
setLocation
setLocation(x,y)
set the location of the JFrame
such
that its upper left corner is at (x,y).
alignment
The meaning of aligment is straight forward from the pictures.
What happen if you want to put more than one components on the JFrame?
We need to use layout manager to set the layout.
In below we will discuss different layout manager. We will concentrate on
3 layout manager BorderLayout, FlowLayout, GridLayout
.
BorderLayout
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class BorderTest extends JFrame {
5: private String title = "Border Test";
6: private JButton northButton = new JButton("North");
7: private JButton eastButton = new JButton("East");
8: private JButton southButton = new JButton("South");
9: private JButton westButton = new JButton("West");
10: private JButton centerButton = new JButton("Center");
11: private Container contentPane = getContentPane();
12:
13: public BorderTest() {
14: // the following line is useless because the default layout of JFrame is border layout
15: contentPane.setLayout(new BorderLayout());
16: contentPane.add(northButton, BorderLayout.NORTH);
17: contentPane.add(eastButton, BorderLayout.EAST);
18: contentPane.add(southButton, BorderLayout.SOUTH);
19: contentPane.add(westButton, BorderLayout.WEST);
20: contentPane.add(centerButton, BorderLayout.CENTER);
21: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22: }
23:
24: public static void main(String[] args) {
25: BorderTest test = new BorderTest();
26: test.setSize(250,200);
27: int x = 100, y = 200;
28: test.setLocation(x,y);
29: test.setVisible(true);
30: }
31:}
After you resize it:

Set the layout
To set the layout, use setLayout(new BorderLayout())
You are going to learn different layouts later.
Constructor
There are 2 constructors of BorderLayout
:
BorderLayout()
: Constructs a new border layout with no gaps
between components.
BorderLayout(int hgap, int vgap)
:
Constructs a border layout with the specified gaps between components.
The horizontal gap is specified by hgap and the vertical gap is specified by
vgap.
Initiate member variables outside of the constructor
Notice how I initiate the variables outside of the constructor
from line 5 to line 11.
Add components
To add components, we use
add(Component component, int position)
method. The position
can be BorderLayout.CENTER, BorderLayout.NORTH, BorderLayout.EAST,
BorderLayout.SOUTH, BorderLayout.WEST
.
If you remove approprate lines form line 16-line 21 and recompile it, we can get
BorderTest2.java
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class BorderTest2 extends JFrame {
5: private String title = "Border Test";
6: private JButton northButton = new JButton("North");
7: private JButton eastButton = new JButton("East");
8: private JButton southButton = new JButton("South");
9: private JButton westButton = new JButton("West");
10: private JButton centerButton = new JButton("Center");
11: private Container contentPane = getContentPane();
12:
13: public BorderTest2() {
14: // horizontal gap and vertical gap
15: int hgap = 10, vgap = 5;
16: contentPane.setLayout(new BorderLayout(hgap, vgap));
17: contentPane.add(northButton, BorderLayout.NORTH);
18: contentPane.add(eastButton, BorderLayout.EAST);
19: contentPane.add(southButton, BorderLayout.SOUTH);
20: contentPane.add(westButton, BorderLayout.WEST);
21: contentPane.add(centerButton); // same as contentPane.add(centerButton, BorderLayout.CENTER);
22: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23: }
24:
25: public static void main(String[] args) {
26: BorderTest2 test = new BorderTest2();
27: test.setSize(250,200);
28: // get the dimension of the screen
29: Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
30: // get the dimension of the frame
31: Dimension frameSize = test.getSize();
32: // draw the frame at the center of the screen
33: int x = screenSize.width/2 - frameSize.width/2;
34: int y = screenSize.height/2 - frameSize.height/2;
35: test.setLocation(x,y);
36: test.setVisible(true);
37: }
38:}
Pay attentions to the gaps between the components.
Constructor
Notice I use other constructor for BorderLayout
to set the
vgap
and hgap
.
getSize()
getSize()
is to get the size of the frame.
Find the size of the screen
You won't be tested on this part for the midterm and final.
I want to show the frame at the center. In order to find the size of the
screen I use
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();