1.简单的理解布局
shell是充满式布局。放置Composite面板和Button按钮控件。
Composite面板采用StackLayout堆栈式布局,每一次仅仅显示一个文本框。
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.layout.*;
public class D{
public static void main(String[] args){
Display display=new Display();
Shell shell=new Shell(display);
shell.setText("StackLayout堆栈式布局");
//理解:GridLayout是布局,GridData是设置一个控件的大小
//(1)设置shell的布局。指的是shell中的控件是如何放置的。
shell.setLayout(new FillLayout(SWT.VERTICAL));
//在shell中放置一个面板和一个按钮
final Composite composite=new Composite(shell,SWT.NONE);
//面板中放置文本框控件
final Text[] textArray=new Text[10];
for(int i=0;i<textArray.length;i++){
textArray[i]=new Text(composite,SWT.MULTI);
textArray[i].setText("这是第"+i+"个文本框");
}
//(2)设置面板的布局。指的是面板中的控件是如何放置的。
//composite.setLayout(new GridLayout());
final StackLayout stackLayout=new StackLayout();
stackLayout.topControl=textArray[0];
composite.setLayout(stackLayout);
//定义每一个文本框的索引,保存当前文本框的索引值
final int[] index=new int[1];
Button button=new Button(shell,SWT.PUSH);
button.setText("显示下一个文本框");
button.addListener(SWT.Selection, new Listener(){
public void handleEvent(Event event){
//获取下一个索引的值
index[0]=(index[0]+1)%10;
stackLayout.topControl=textArray[index[0]];
//必须重新调整面板composite的布局,否则这个布局是没有效果的
composite.layout();
}
});
//打开窗口,进行窗口的显示
shell.setSize(400,400);
//shell.pack();
shell.open();
while(!shell.isDisposed()){
//当窗口没有被释放的时候
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}
2.GridData是控件的大小布局之类的。(这个可以不用看)
(1)Shell有一个布局是GridLayout(网格式布局),体现在Composite面板和Button上。
(2)Composite也有布局,它的布局是StackLayout(堆栈式布局),特点是每次仅能设置一个显示StackLayout.topControl=一个控件。Composite里面的数据布局用的是GridData,利用网格式来进行数据的布局。
图1是默认的网格式布局;图2是SWT.FILL_BOTH应该是网格式数据全屏布局;
图3是SWT.FILL_VERTICAL网格式垂直数据布局;图4是SWT.FILL_HORIZONTAL网格式水平数据布局。
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.layout.*;
public class B{
public static void main(String[] args){
Display display=new Display();
Shell shell=new Shell(display);
shell.setText("StackLayout堆栈式布局");
//shell采用网格布局
shell.setLayout(new GridLayout());//shell必须有布局
//(1)创建一个面板,用作放置文本框的面板
final Composite composite=new Composite(shell,SWT.NONE);
//(2)设置面板的布局数据,设置面板控件的布局数据
//默认是图1
composite.setLayoutData(new GridData(GridData.FILL_BOTH));//全屏,图2
//composite.setLayoutData(new GridData(GridData.FILL_VERTICAL));//垂直,图3
//composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));//水平,图4
//(3)向面板中添加数据
final Text[] textArray=new Text[10];
for(int i=0;i<10;i++){
textArray[i]=new Text(composite,SWT.MULTI);
textArray[i].setText("这是第"+i+"个文本框");
}
//(4)设置面板的布局为网格式布局
// GridLayout gridLayout=new GridLayout();
// gridLayout.numColumns=3;
// composite.setLayout(gridLayout);
//(4)设置面板的布局为堆栈式布局
final StackLayout stackLayout=new StackLayout();
composite.setLayout(stackLayout);
//(5)设置堆栈中当前显示的控件
stackLayout.topControl=textArray[0];
//保存当前文本框的索引值
final int[] index=new int[1];
//在shell窗口中添加一个按钮
Button bt=new Button(shell,SWT.PUSH);
bt.setText("显示下一个文本框");
//bt.addListener(eventType, listener)
bt.addListener(SWT.Selection, new Listener(){
public void handleEvent(Event event){
//计算下一个文本框的索引值
index[0]=(index[0]+1)%10;
stackLayout.topControl=textArray[index[0]];
//重新布局(必须有)
composite.layout();
}
});
//打开窗口,进行窗口的显示
shell.setSize(400,400);
//shell.pack();
shell.open();
while(!shell.isDisposed()){
//当窗口没有被释放的时候
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
}