|
Previous Next
Flow Layout
Using the FlowLayout causes the components to be placed on the container in a left to right order. When the space on the row is insufficient for the next component, a new row is started. If during execution, the window is resized, then the components are rearranged to fit the new size.
After testing the code below, be sure to increase and decrease the size of the window and note the position changes of the components.
Also, note that the buttons are aligned to the left of the frame using "FlowLayout.LEFT". Test modifying the alignment by changing the following from
FlowLayout nature = new FlowLayout(FlowLayout.LEFT);
FlowLayout nature = new FlowLayout(FlowLayout.RIGHT);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimalFlow extends JFrame {
JButton dog = new JButton("Dog");
JButton cat = new JButton("Cat");
JButton bird = new JButton("Bird");
JButton pig = new JButton("Pig");
JButton mouse = new JButton("Mouse");
JButton lion = new JButton("Lion");
JButton tiger = new JButton("Tiger");
JButton bear = new JButton("Bear");
JButton deer = new JButton("Deer");
public AnimalFlow() {
super("Animal FlowLayout");
setSize(260, 260);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
FlowLayout nature = new FlowLayout(FlowLayout.LEFT);
pane.setLayout(nature);
pane.add(dog);
pane.add(cat);
pane.add(bird);
pane.add(pig);
pane.add(mouse);
pane.add(lion);
pane.add(tiger);
pane.add(bear);
pane.add(deer);
add(pane);
setVisible(true);
}
public static void main(String[] arguments) {
AnimalFlow frame = new AnimalFlow();
}
}
Previous Next
|