SWT:StackLayout(堆栈式布局)

来源:互联网 发布:北大青鸟陈璇java视频 编辑:程序博客网 时间:2024/06/10 22:37
 shell.setLayout(new GridLayout());
  //创建放置文本框的面板
  final Composite parent = new Composite(shell, SWT.NONE);
  //设置面板的布局数据
  parent.setLayoutData(new GridData(GridData.FILL_BOTH));
  //创建堆栈式布局
  final StackLayout layout = new StackLayout();
  //将堆栈式布局应用于面板
  parent.setLayout(layout);
  //创建10个文本框
  final Text[] textArray = new Text[10];
  for (int i = 0; i < 10; i++)
  {
   textArray[i] = new Text(parent, SWT.MULTI);
   textArray[i].setText("这是第 " + i + "个文本框");
  }
  
  //设置堆栈中当前显示的控件
  layout.topControl = textArray[0];
  
  Button b = new Button(shell, SWT.PUSH);
  b.setText("显示下一个文本框");
  //保存当前显示的文本框的索引值
  final int[] index = new int[1];
  //为按钮添加单击事件
  b.addListener(SWT.Selection, new Listener(){
   public void handleEvent(Event e)
   {
    //计算出下一个文本框的索引数
    index[0] = (index[0] + 1) % 10;
    
    //设置当前显示的控件
    layout.topControl = textArray[index[0]];
    //重新刷新布局
    parent.layout();
   }
  });
  
  shell.setSize(200, 150);
原创粉丝点击