Java Reference
In-Depth Information
4.
You might be wondering why you are using setPreferredSize instead of just setSize , which is
also available as a method. This is because setBounds and setSize only have an effect when the
layout manager of the container holding your components is set to null . This is a common mis-
take that throws many beginners off, especially since all containers have default layout managers
and are thus not equal to null . By using setPreferredSize , you effectively signal to the layout
manager: I would like this component to be this size, if you deem it possible.
The FlowLayout comes with three constructors. These are:
public FlowLayout();
public FlowLayout(int align);
public FlowLayout(int align, int hgap, int vgap);
// align: either FlowLayout.LEFT (or FlowLayout.LEADING),
// FlowLayout.RIGHT (or FlowLayout.TRAILING),
// or FlowLayout.CENTER
// hgap, vgap: horizontal/vertical gap between the components
aBout right‐to‐left orientation
Some languages, such as Arabic, are read from right to left, and operating systems
include functionality to also make UI components appear in the correct reading
order. In Java, it is also possible to change the reading orientation, and Swing will
honor this setting as well—FlowLayout, for instance, will then order components
from right to left. You can try this out with the following code snippet:
import java.awt.ComponentOrientation;
import javax.swing.JFrame;
public class RightToLeft {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Right to Left Frame");
frame.setComponentOrientation(
ComponentOrientation.RIGHT_TO_LEFT);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
Borderlayout
In a BorderLayout , a container is divided into five “zones,” called North, South, West, East, and
Center, in which components can be placed. You do not need to add components to all zones for a
BorderLayout to work, as the components are allowed to stretch to fill available space.
 
 
Search WWH ::




Custom Search