java 中flowlayout布局是什么样子
时间: 2024-01-02 16:03:02 浏览: 31
FlowLayout布局是Java中的一个布局管理器,用于在容器中按行进行排列组件。它的特点是组件按照添加的顺序依次排列,并且当容器不足以容纳所有组件时,会自动将组件放在下一行中。在FlowLayout中,组件之间的间隔是相等的,并且它们的大小也不会发生改变。以下是一个简单的例子:
```java
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FlowLayoutDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
// 创建一个FlowLayout布局管理器
FlowLayout layout = new FlowLayout();
frame.setLayout(layout);
// 添加一些按钮
JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");
JButton btn3 = new JButton("Button 3");
JButton btn4 = new JButton("Button 4");
JButton btn5 = new JButton("Button 5");
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.setVisible(true);
}
}
```
运行这个程序,你会看到5个按钮依次排列,并且当窗口大小改变时它们会自动换行。