Border Layout This layout is most commonly used when you want to create regions for adding other containers (Panel or Canvas). For example, you could create a Panel for holding buttons and add it to one of the regions.
The code to set a BorderLayout and add components is:
myFrame.setLayout(new BorderLayout()); myFrame.add("North", myPanel);
The BorderLayout looks something like this, without the borders:
Note that, when adding a component to a BorderLayout, you have to specify the region to which it is added.
North West Center East South
GridLayout
The GridLayout divides the component (Frame or Panel) into a grid. For example, if you wanted a grid with 3 rows and 5 columns:
myPanel.setLayout(new GridLayout(3, 5)); myPanel.add(myButton);
This GridLayout looks something like this, without the borders:
This layout gives you more control over placement of components. When components are added, the go in left to right, filling in the rows one at a time from top to bottom.
FlowLayout
The FlowLayout also fills in left to right, but wraps the components. Imagine in Word, you have your paragraph set for 'center text.' As you type in the words of your sentences, they are centered on a line until the line is too wide for the page. Word then wraps the sentence. FlowLayout works the same way.
myPanel.setLayout(new FlowLayout()); myPanel.add(myButton);CardLayout
The CardLayout works just like a stack of cards, with only one visible at a time. Methods exist that allow you to flip through the cards, displaying them either in turn are by specific card name.
myPanel.setLayout(new CardLayout()); myPanel.addLayoutComponent("Physicians", panelPhys); myPanel.addLayoutComponent("Locations", panelLocs); myPanel.show(myPanel, "Physicians");GridBagLayout
The GridBagLayout is, by far, the most complicate yet most most versatile, of all the Layout managers. It allows you to specify, component by component:
- how many rows and columns it will occupy
- how it will fill the area
- how it will align itself in the area
Frame
A Java Applet has a place to display visual components- a Panel is displayed in the space reserved in the browser window. It is inherently there. For a Java Application, you need a Frame. The default layout is the BorderLayout.
Panel
A Panel is used primarily for displaying other components. Components are added to the Panel based on the layout. The default layout is the FlowLayout.
Canvas
A Canvas is used primarily for displaying and drawing images. This component does not use a layout.
Label
Constructor Summary
Label() Constructs an empty label. Label(String text) Constructs a new label with the specified string of text, left justified. Label(String text, int alignment) Constructs a new label that presents the specified string of text with the specified alignment.
Method Summary (the more common ones)
int getAlignment() Gets the current alignment of this label. String getText() Gets the text of this label. void setAlignment(int alignment) Sets the alignment for this label to the specified alignment. void setText(String text) Sets the text for this label to the specified text.
The values for alignment are:
- Label.CENTER
- Label.LEFT
- Label.RIGHT
The default alignment is LEFT.
TextField
Constructor Summary
TextField() Constructs a new text field. TextField(int columns) Constructs a new empty text field with the specified number of columns. TextField(String text) Constructs a new text field initialized with the specified text. TextField(String text, int columns) Constructs a new text field initialized with the specified text to be displayed, and wide enough to hold the specified number of columns.
Method Summary (the more common ones)
void addActionListener(ActionListener l) Adds the specified action listener to receive action events from this text field. String getText() Gets the text that is presented by this text component void setEchoChar(char c) Sets the echo character for this text field. void setText(String t) Sets the text that is presented by this text component to be the specified text.Choice
Constructor Summary
Choice() Creates a new choice menu.
Method Summary (the more common ones)
void addItem(String item) Adds an item to this Choice. void addItemListener(ItemListener l) Adds the specified item listener to receive item events from this Choice menu. String getItem(int index) Gets the string at the specified index in this Choice menu. int getItemCount() Returns the number of items in this Choice menu. int getSelectedIndex() Returns the index of the currently selected item. String getSelectedItem() Gets a representation of the current choice as a string. void insert(String item, int index) Inserts the item into this choice at the specified position. void remove(int position) Removes an item from the choice menu at the specified position. void remove(String item) Remove the first occurrence of item from the Choice menu. void removeAll() Removes all items from the choice menu. void select(int pos) Sets the selected item in this Choice menu to be the item at the specified position. void select(String str) Sets the selected item in this Choice menu to be the item whose name is equal to the specified stringButton
Constructor Summary
Button() Constructs a Button with no label. Button(String label) Constructs a Button with the specified label.
Method Summary (the more common ones)
void addActionListener(ActionListener l) Adds the specified action listener to receive action events from this button. String getActionCommand() Returns the command name of the action event fired by this button.