StackLayout堆栈式布局

本文通过示例代码展示了如何使用Eclipse SWT中的StackLayout实现堆栈式布局,使得Composite面板每次仅显示一个文本框,并可通过按钮切换显示不同的文本框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值