Java Reference
In-Depth Information
The BorderLayout comes with two constructors. These are:
public BorderLayout();
public BorderLayout (int hgap, int vgap);
// hgap, vgap: horizontal/vertical gap between the components
Note that the BorderLayout is the default layout manager for the content pane of a windowed con-
tainer (e.g., for a JFrame ). You've already seen how it works in your first Try It Out for this chapter,
but look at another example here.
experimenting with the BorderLayout
try it out
In this Try It Out, you create a JFrame to see how the BorderLayout layout manager works.
1.
As always, feel free to create a new project in Eclipse when you want to. Create a class called
BorderLayoutFrame with the following content:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class BorderLayoutFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("BorderLayout frame");
/* Since the default layout manager for bordered containers is
* already a BorderLayout, the following line is OPTIONAL here.
*/
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(new JButton("NORTH"), BorderLayout.NORTH);
// ... or BorderLayout.PAGE_START
frame.getContentPane().add(new JButton("WEST"), BorderLayout.WEST);
// ... or BorderLayout.LINE_START
frame.getContentPane().add(new JButton("EAST"), BorderLayout.EAST);
// ... or BorderLayout.LINE_END
frame.getContentPane().add(new JButton("SOUTH"), BorderLayout.SOUTH);
// ... or BorderLayout.PAGE_END
frame.getContentPane().add(new JButton("CENTER"), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
2.
Run the project from Eclipse. You should see the window in Figure 11-10.
Search WWH ::




Custom Search