Set flow from center for FlowLayout in Java
Description
The following code shows how to set flow from center for FlowLayout.
Example
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*from ww w . ja v a2 s.co m*/
public class Main {
public static void main(String[] args) {
int align = FlowLayout.CENTER; // or LEFT, RIGHT
JPanel panel = new JPanel(new FlowLayout(align));
JTextArea area = new JTextArea("text area");
area.setPreferredSize(new Dimension(100, 100));
JButton button = new JButton("button");
panel.add(button);
panel.add(new JScrollPane(area));
JFrame f = new JFrame();
f.add(panel);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »