Advertisement
Constructor | Description |
---|---|
public FlowLayout() | Creates a default FlowLayout object, which positions the components in the center and maintains a space of 5 pixels between components in a container. |
public FlowLayout(int align) | Creates a FlowLayout which aligns the line holding the components according to the value of the align constant. |
public FlowLayout(int how, int horizontalSpace, int verticalSpace) | Creates a Flowlayout, which aligns the line holding the components according to the value of the align constant and also lets us specify the horizontal and vertical space between the components. |
Value of align constant | Description |
---|---|
FlowLayout.RIGHT | Positions the line containing the components to the right. |
FLowLayout.LEFT | Positions the line containing the components to the left. |
FLowLayout.CENTER | Positions the line containing the components in the center. |
FlowLayout.LEADING | Positions the line containing the components at the leading edge. |
FlowLayout.TRAILING | Positions the line containing the components at the trailing edge. |
Method | Description |
---|---|
public void setLayout(LayoutManager object) | Positions the element in a Container. |
Advertisement
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FlowLayoutEx
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});
}//Closing the main method
}//Closing the class A
class A
{
JLabel label1, label2, label3;
JTextField field1, field2;
JButton button1, button2, button3;
JFrame jf;
A()
{
jf = new JFrame("FlowLayout Example");
jf.setSize(400,150);
label1= new JLabel("Enter your name");
label2= new JLabel("Enter your city");
field1 = new JTextField(15); //calling TextField(int widthOfTextField)
field2 = new JTextField(15);
button1 = new JButton("Clear");
button2 = new JButton("Submit");
button3 = new JButton("Exit");
//Setting the positioning of the components in container.
jf.setLayout(new FlowLayout()); // calling the first constructor of FlowLayout class, which
// positions each line of components in the center of JFrame
jf.add(label1); //Adding the first Jlabel component to JFrame container
jf.add(field1); //Adding the first JTextField component to JFrame container
jf.add(label2); //Adding the second JLabel component to JFrame container
jf.add(field2); //Adding the second JTextField component to JFrame container
jf.add(button1); //Adding the first JButton component to JFrame container
jf.add(button2); //Adding the second JButton component to JFrame container
jf.add(button3); //Adding the third JButton component to JFrame container
jf.setSize(300,200);
jf.setVisible(true);
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement