因为Java SDK里面没有VB或者Delphi所带的表单设计器,所以需要用代码来定位各个组件在容器里面的布局方式。
setLayout()就是布局管理器,可以设置容器的布局方式。
具体的布局方式很多,有FlowLayout、BorderLayout、GridLayout、BoxLayout、GridBagLayout、 SpringLayout,另外还有无布局和自定义布局,可以看相关的书。
setLayout()就是布局管理器,可以设置容器的布局方式。
具体的布局方式很多,有FlowLayout、BorderLayout、GridLayout、BoxLayout、GridBagLayout、 SpringLayout,另外还有无布局和自定义布局,可以看相关的书。
不过很多时候你可以自定义布局!
你可以用this.setLayout(null);
//FlowLayout框架的使用
import javax.swing.*;
import java.awt.*;
public class c1_02_01
{
public static void main(String args[])
{
JFrame frame=new JFrame("c1_02_01(FlowLayout)");
Container c=frame.getContentPane();
// FlowLayout f1=new FlowLayout(FlowLayout.LEFT);
FlowLayout f1=new FlowLayout(FlowLayout.CENTER);
// FlowLayout f1=new FlowLayout(FlowLayout.RIGHT);
// FlowLayout f1=new FlowLayout(FlowLayout.TRAILING);
// FlowLayout f1=new FlowLayout(FlowLayout.LEADING);
c.setLayout(f1);//<---在这里应用.
for(int i=1;i<=10;i++)
{
c.add(new JButton("no"+i));
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}