案例代码:
import javax.swing.*;
import java.awt.*;
public class ShowFlowLayout {
private JFrame f;
private JButton[] b;
private JButton jb;
public ShowFlowLayout(){
f=new JFrame("FlowLayout示例");
b=new JButton[5];
for(int i=1;i<=5;i++) {
b[i-1]=new JButton("JButton"+i);
}
jb=new JButton("Welcome to Java");
}
public void launchFrame(){
Container c=f.getContentPane();
c.setLayout(new FlowLayout(FlowLayout.RIGHT,10,15));
for(int i=0;i<5;i++){
c.add(b[i]);
}
c.add(jb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();//设定窗口的大小恰好能容纳其上添加的组件
f.setVisible(true);
}
public static void main(String args[]){
ShowFlowLayout sf=new ShowFlowLayout();
sf.launchFrame();
}
}
案例运行图: