|
package swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.junit.Test;
/**
*
*/
public class LayoutDemo_06 {
@Test
public void testGridLayout(){
Display display=new Display();
Shell topShell=new Shell(display,SWT.SHELL_TRIM|SWT.BORDER);
topShell.setText("测试testGridLayout布局");
topShell.setSize(800, 500);
GridLayout gridLayout=new GridLayout();
gridLayout.numColumns=3;
gridLayout.makeColumnsEqualWidth=true;//让每列大小相等
topShell.setLayout(gridLayout);
Button button=new Button(topShell, SWT.PUSH);
button.setText("按钮");
Button button2=new Button(topShell, SWT.PUSH);
button2.setText("a");
Button button3=new Button(topShell, SWT.PUSH);
button3.setText("按钮按钮按钮按钮按钮");
Button button4=new Button(topShell, SWT.PUSH);
GridData gridDataForbtn4=new GridData();
//设置水平填充方式充满自己的单元格
gridDataForbtn4.horizontalAlignment=SWT.FILL;
//设置垂直居中
gridDataForbtn4.verticalAlignment=SWT.CENTER;
button4.setText("按钮4");
button4.setLayoutData(gridDataForbtn4);
Button button5=new Button(topShell, SWT.PUSH);
button5.setText("a5");
Button button6=new Button(topShell, SWT.PUSH);
button6.setText("按钮按钮按钮按钮按钮6");
GridData gridData6=new GridData();
//当水平方向扩大,就跟着扩大.抢占水平方向
gridData6.grabExcessHorizontalSpace=true;
gridData6.horizontalAlignment=SWT.FILL;
button6.setLayoutData(gridData6);
Button button7=new Button(topShell, SWT.PUSH);
GridData gridData=new GridData();
gridData.horizontalSpan=3;
gridData.horizontalAlignment=SWT.FILL;
button7.setText("按钮7");
button7.setLayoutData(gridData);
// topShell.pack();
topShell.open();
while(!topShell.isDisposed()){
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
@Test
public void testRowLayout(){
Display display=new Display();
Shell topShell=new Shell(display,SWT.SHELL_TRIM|SWT.BORDER);
topShell.setText("测试row布局");
topShell.setSize(800, 500);
RowLayout rowLayout=new RowLayout();
rowLayout.type=SWT.HORIZONTAL;//默认
rowLayout.wrap=true;//默认
// rowLayout.pack=false;//默认是true,设置为false让每个控件等大小
// rowLayout.justify=true;//充满整行,平均分配空间
//当水平显示时,试图让各个控件等高;当垂直显示时,试图让各个控件等宽
rowLayout.fill=true;
topShell.setLayout(rowLayout);
Button button=new Button(topShell, SWT.PUSH);
//每个控件都有setLayoutData,参数可以是rowData,GridData,用于控制单个控件
//new RowData(100, 50)表示单独为button控件设置宽高,使用xxData的前提是使用xxLayout布局
button.setLayoutData(new RowData(100, 50));
button.setText("按钮");
Button button2=new Button(topShell, SWT.PUSH);
button2.setText("a");
Button button3=new Button(topShell, SWT.PUSH);
button3.setText("按钮按钮按钮按钮按钮");
// topShell.pack();
topShell.open();
while(!topShell.isDisposed()){
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
@Test
public void testFillOutLayout(){
Display display=new Display();
Shell topShell=new Shell(display,SWT.SHELL_TRIM|SWT.BORDER);
topShell.setText("测试fill布局");
topShell.setSize(800, 500);
FillLayout fillLayout=new FillLayout();
// fillLayout.type=SWT.VERTICAL;
fillLayout.type=SWT.HORIZONTAL;
topShell.setLayout(fillLayout);
Button button=new Button(topShell, SWT.PUSH);
button.setText("按钮");
Button button2=new Button(topShell, SWT.PUSH);
button2.setText("a");
Button button3=new Button(topShell, SWT.PUSH);
button3.setText("按钮按钮按钮按钮按钮");
topShell.open();
while(!topShell.isDisposed()){
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
@Test
public void testBounds(){
//负责和操作系统交互,如读取底层事件等
Display display=new Display();
//窗口
Shell topShell=new Shell(display,SWT.SHELL_TRIM|SWT.BORDER);
topShell.setText("测试绝对定位布局");
topShell.setSize(800, 500);
Button button=new Button(topShell, SWT.PUSH);
button.setText("按钮");
button.setBounds(200, 200, 100, 30);
// topShell.pack();
topShell.open();
while(!topShell.isDisposed()){
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
|