Java AWT |流式布局
FlowLayout 用于一个接一个地按顺序排列组件。小程序和面板的默认布局是 FlowLayout。
构造函数:
- FlowLayout() :它将构造一个居中对齐的新 FlowLayout。水平和垂直间隙将为 5 个像素。
- FlowLayout(int align) :它将构造一个具有给定对齐方式的新 FlowLayout。水平和垂直间隙将为 5 个像素。
- FlowLayout(int align, int HorizontalGap, int VerticalGap) :它将构造一个具有给定对齐方式的新 FlowLayout,组件之间的给定水平和垂直间隙。
- JLabel(String text) :它将创建一个带有指定文本的 JLabel 实例。
常用方法:
- setTitle(String Text) : 该方法用于设置 JFrame 的标题。您要设置的标题作为字符串传递。
- getAlignment() :返回此布局的对齐方式。
- setAlignment(int align) :用于设置此布局的对齐方式。
- removeLayoutComponent(Component comp) :用于从布局中删除作为参数传递的组件。
下面的程序将说明Java中 FlowLayout 的示例。
- 程序1:下面的程序通过在一个JFrame中排列几个JLabel组件来说明FlowLayout的使用,其实例类命名为“Example”。我们创建了 5 个名为“l1”、“l2”……“l5”的 JLabel 组件,然后通过 this.add() 方法将它们添加到 JFrame。我们通过 setTitle 和 setBounds 方法设置框架的标题和边界。
布局由 setLayout() 方法设置;// Java program to show Example of FlowLayout. // in java. Importing different Package. import java.awt.*; import java.awt.event.*; import javax.swing.*; class Example extends JFrame { // Declaration of objects of JLabel class. JLabel l1, l2, l3, l4, l5; // Constructor of Example class. public Example() { // Creating Object of "FlowLayout" class FlowLayout layout = new FlowLayout(); // this Keyword refers to current object. // Function to set Layout of JFrame. this.setLayout(layout); // Initialization of object "l1" of JLabel class. l1 = new JLabel("Label 1 "); // Initialization of object "l2" of JLabel class. l2 = new JLabel("Label 2 "); // Initialization of object "l3" of JLabel class. l3 = new JLabel("Label 3 "); // Initialization of object "l4" of JLabel class. l4 = new JLabel("Label 4 "); // Initialization of object "l5" of JLabel class. l5 = new JLabel("Label 5 "); // this Keyword refers to current object. // Adding Jlabel "l1" on JFrame. this.add(l1); // Adding Jlabel "l2" on JFrame. this.add(l2); // Adding Jlabel "l3" on JFrame. this.add(l3); // Adding Jlabel "l4" on JFrame. this.add(l4); // Adding Jlabel "l5" on JFrame. this.add(l5); } } class MainFrame { // Driver code public static void main(String[] args) { // Creating Object of Example class. Example f = new Example(); // Function to set title of JFrame. f.setTitle("Example of FlowLayout"); // Function to set Bounds of JFrame. f.setBounds(200, 100, 600, 400); // Function to set visible status of JFrame. f.setVisible(true); } }
输出:
通过使用这些 FlowLayout 字段,我们可以控制流布局排列中组件的对齐方式。
1) RIGHT :- 每行组件向右移动。
2) LEFT :- 每行组件向左移动。 - 程序 2:下面的程序通过在 FLowLayout 的构造函数中传递参数 FlowLayout.RIGHT 来说明使用右对齐的 FlowLayout 的使用。我们创建了 5 个名为“l1”、“l2”……“l5”的 JLabel 组件,然后通过 this.add() 方法将它们添加到 JFrame。我们通过 setTitle 和 setBounds 方法设置框架的标题和边界。
布局由 setLayout() 方法设置;// Java program to show example of // FlowLayout and using RIGHT alignment import java.awt.*; import java.awt.event.*; import javax.swing.*; class Example extends JFrame { // Declaration of objects of JLabel class. JLabel l1, l2, l3, l4, l5; // Constructor of Example class. public Example() { // Creating Object of "FlowLayout" class, passing // RIGHT alignment through constructor. FlowLayout layout = new FlowLayout(FlowLayout.RIGHT); // this Keyword refers to current object. // Function to set Layout of JFrame. this.setLayout(layout); // Initialization of object "l1" of JLabel class. l1 = new JLabel("Label 1 "); // Initialization of object "l2" of JLabel class. l2 = new JLabel("Label 2 "); // Initialization of object "l3" of JLabel class. l3 = new JLabel("Label 3 "); // Initialization of object "l4" of JLabel class. l4 = new JLabel("Label 4 "); // Initialization of object "l5" of JLabel class. l5 = new JLabel("Label 5 "); // this Keyword refers to current object. // Adding Jlabel "l1" on JFrame. this.add(l1); // Adding Jlabel "l2" on JFrame. this.add(l2); // Adding Jlabel "l3" on JFrame. this.add(l3); // Adding Jlabel "l4" on JFrame. this.add(l4); // Adding Jlabel "l5" on JFrame. this.add(l5); } } class MainFrame { // Driver code public static void main(String[] args) { // Creating Object of Example class. Example f = new Example(); // Function to set title of JFrame. f.setTitle("Example of FlowLayout"); // Function to set Bounds of JFrame. f.setBounds(200, 100, 600, 400); // Function to set visible status of JFrame. f.setVisible(true); } }
输出:
参考:https://www.geeksforgeeks.org/message-dialogs-java-gui/