Java Reference
In-Depth Information
The other problem with the previous output is that the Send button is stretched.
Because it was placed directly in the south region of a
BorderLayout
, Java stretches
the button horizontally to fill the frame. To avoid this problem, put the Send button
into a panel with a
FlowLayout
.
FlowLayout
doesn't stretch the components inside
it, so the button won't grow to such an odd size. Then add the panel to the south
region of the frame, rather than adding the Send button directly.
Here's a complete version of the program that contains these corrections and pro-
duces the proper graphical output:
1 // Creates a GUI that resembles an email Compose Message window.
2
3
import
java.awt.*;
4
import
javax.swing.*;
5
6
public class
EmailMessage {
7
public static void
main(String[] args) {
8 JFrame frame =
new
JFrame();
9 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
10 frame.setSize(
new
Dimension(400, 300));
11 frame.setTitle("Send Message");
12 frame.setLayout(
new
BorderLayout());
13
14 JPanel northWest =
new
JPanel(
new
GridLayout(3, 1));
15 northWest.add(
new
JLabel("From: "));
16 northWest.add(
new
JLabel("To: "));
17 northWest.add(
new
JLabel("Subject: "));
18
19 JPanel northCenter =
new
JPanel(
new
GridLayout(3, 1));
20 northCenter.add(
new
JTextField());
21 northCenter.add(
new
JTextField());
22 northCenter.add(
new
JTextField());
23
24 JPanel northEast =
new
JPanel(
new
GridLayout(3, 1));
25 northEast.add(
new
JButton("Browse..."));
26 northEast.add(
new
JButton("Browse..."));
27
28 JPanel north =
new
JPanel(
new
BorderLayout());
29 north.add(northWest, BorderLayout.WEST);
30 north.add(northCenter, BorderLayout.CENTER);
31 north.add(northEast, BorderLayout.EAST);
32
33 JPanel south =
new
JPanel(
new
FlowLayout());
34 south.add(
new
JButton("Send"));
35
Search WWH ::
Custom Search