`

SWT如何实现面板内容切换,解决方案(原创-logoc)

阅读更多

       最近一直在做软件,采用了SWT插件,经过近1个多月的使用,对SWT有了初步的认识,今日刚刚解决SWT面板如实现内容切换,而非窗口切换做一些分享,希望对各位有所帮助!

       建议入门同学读一下<理解SWT布局>这篇文章,多读几次也无妨。这是我的感受。

       现在切入正题,如何解决面板内容切换问题!

       在CSDN、百度、google我查了相关的资料,相关资料很少,不知道是我搜索关键字有问题还是其他原因。这个问题困扰我很久,网上提供的使用CTabItem的较多,但这是我想要的效果。我要的效果,是通过按钮实现内容切换而且,实现不同面板在不同的类中进行设计。经过反复调试测验,发现当我现在一个composite面板,调用setVisible(true/false)方法来实现面板的转换,但是发现不行,布局有问题,最后发现使用StackLayout()叠加布局类来实现。所有的面板都叠放在一起,通过按钮将需要的面板置顶即可。总体思路就是这样。如有描述错误,贻笑大方还请各位指点。

实现代码:可独立运行

 

package com.hbaas.main;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.hbaas.composite.SystemSetComposite;

public class SystemFrame {

 protected Shell shell;
 protected Button systemBt ;
 protected Button nhinfoBt ;
 protected Composite systemComp;
 protected Composite nhinfoComp;
 protected StackLayout stackLayout = new StackLayout();
 protected Composite displayComp;
 /**
  * Launch the application.
  * @author 李亮

  * @param Composite
  */
 public static void main(String[] args) {
  try {
   SystemFrame window = new SystemFrame();
   window.open();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * Open the window.
  */
 public void open() {
  Display display = Display.getDefault();
  createContents();
  shell.open();
  shell.layout();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch()) {
    display.sleep();
   }
  }
 }

 /**
  * Create contents of the window.
  */
 protected void createContents() {
  shell = new Shell(SWT.MIN | SWT.CLOSE);
  shell.setSize(1200, 800);
  shell.setLocation(Display.getCurrent().getClientArea().width/2 - shell.getShell().getSize().x/2, Display.getCurrent().getClientArea().height/2 - shell.getSize().y/2);
  
  shell.setText("湖北省xxxx系统");
  shell.setImage(new Image(null,"./icons/log.ico")); 
  shell.setLayout(new GridLayout());
  SashForm sashForm = new SashForm(shell, SWT.NONE);
 
  sashForm.setOrientation(SWT.VERTICAL);
  {
   GridData gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
   gridData.widthHint = 1185;
   gridData.heightHint = 755;
   sashForm.setLayoutData(gridData);
  }
  
  final Composite controlComp = new Composite(sashForm,SWT.NONE);
  controlComp.setBackgroundImage(new Image(shell.getDisplay(),"./icons/top.gif"));
  final GridData controlGridata = new GridData(SWT.FILL,SWT.CENTER);  
  {
   systemBt = new Button(controlComp, SWT.NONE);
   systemBt.setBounds(579, 24, 100, 60);
   systemBt.setText("系统设置");

   nhinfoBt = new Button(controlComp, SWT.NONE);
   nhinfoBt.setBounds(739, 24, 100, 60);
   nhinfoBt.setText("农户信息");

   Button dataBt = new Button(controlComp, SWT.NONE);
   dataBt.setBounds(889, 24, 100, 60);
   dataBt.setText("数据交换");
   Button aboutBt = new Button(controlComp, SWT.NONE);
   aboutBt.setBounds(1040, 24, 100, 60);
   aboutBt.setText("关于");
  }
  controlComp.setLayoutData(controlGridata);

//  systemBt.addSelectionListener(new CompSelectionListener());
//  nhinfoBt.addSelectionListener(new CompSelectionListener());
  
  displayComp = new Composite(sashForm,SWT.NONE);
  displayComp.setLayout(stackLayout);
  
//  systemComp = createSystemComp(displayComp);
  systemComp = new SystemSetComposite().createFrame(displayComp);
  nhinfoComp = createNhInfoComp(displayComp);  
  stackLayout.topControl = systemComp;
 
  
  Listener listener = new Listener(){
   public void handleEvent(Event event) {
    if (event.widget == systemBt){
     stackLayout.topControl = systemComp;
//     System.out.println(systemBt.getText());
    }else if(event.widget == nhinfoBt){
     stackLayout.topControl = nhinfoComp;
//     System.out.println(nhinfoBt.getText());
    } 
    displayComp.layout();
   }   
  };
  systemBt.addListener(SWT.Selection , listener);
  nhinfoBt.addListener(SWT.Selection, listener); 
  sashForm.setWeights(new int[] {118, 681});
 }
 private Composite createSystemComp(Composite displayComp) {
  Composite comp = new Composite(displayComp, SWT.NONE);
  comp.setLayout(new GridLayout());
  new Label(comp, SWT.NONE).setText("系统面板");
  return comp; 
 } 
 private Composite createNhInfoComp(Composite displayComp) {
  Composite comp = new Composite(displayComp, SWT.NONE);
  comp.setLayout(new GridLayout());
  new Label(comp, SWT.NONE).setText("农户信息面板");
  return comp;  
 }
}

分享到:
评论
2 楼 logoc 2012-06-27  
dotjar 写道
SystemSetComposite在哪里?

这个是一个新的面板,你可以自己定义的。
1 楼 dotjar 2012-06-08  
SystemSetComposite在哪里?

相关推荐

Global site tag (gtag.js) - Google Analytics