首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

SWT/Jface 自定义格局 原创

2012-10-18 
SWT/Jface自定义布局 原创不多说了 直接贴代码 import org.eclipse.swt.*import org.eclipse.swt.graphic

SWT/Jface 自定义布局 原创

不多说了 直接贴代码

 

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

public class BorderLayout extends Layout{
 
 private Control north;
 private Control south;
 private Control east;
 private Control west;
 private Control center;
 
 @Override
 protected Point computeSize(Composite composite, int wHint, int hHint,
   boolean flushCache) {
  getControls(composite);
  //定义面板的宽和高
  int width = 0,height = 0;
  //计算面板的宽度
  width += west == null ? 0 : getSize(west,flushCache).x;
  width += east == null ? 0 : getSize(east,flushCache).x;
  width += center == null ? 0 : getSize(center,flushCache).x;
  //如果上部和下部有控件,则宽度去最大值
  if(north != null){
   Point pt = getSize(north, flushCache);
   width = Math.max(width, pt.x);
  }
  if(south != null){
   Point pt = getSize(south, flushCache);
   width = Math.max(width, pt.x);
  }
  
  //计算面板的高度
  height += north == null ? 0 : getSize(north, flushCache).y;
  height += south == null ? 0 : getSize(south, flushCache).y;
  System.out.println("width> "+width+"height>"+height);
  int heightOther = center == null ? 0 : getSize(center,flushCache).y;
  if(west != null){
   Point pt = getSize(west, flushCache);
   heightOther = Math.max(heightOther, pt.y);
  }
  if(east != null){
   Point pt = getSize(east, flushCache);
   heightOther = Math.max(heightOther, pt.y);
  }
  height +=heightOther;
  //计算的宽和高与默认的宽和高度作比较,返回之中较大的
  return new Point(Math.max(width, wHint),Math.max(height, hHint));
 }

 @Override
 protected void layout(Composite composite, boolean flushCache) {
  getControls(composite);
  //获得当前面板可显示的区域
  Rectangle rect = composite.getClientArea();
  int left = rect.x,
    right = rect.width,
    top = rect.y,
    bottom = rect.height;
  //将各个控件放置到面板中
  if(north != null){
   Point pt = getSize(north, flushCache);
   top += pt.y;
  }
  if(south != null){
   Point pt = getSize(south, flushCache);
   south.setBounds(left, rect.height-pt.y, rect.width, pt.y);
   bottom -= pt.y;
  }
  if(east != null){
   Point pt = getSize(east, flushCache);
   east.setBounds(rect.width-pt.x, top, pt.x, (bottom-top));
   right -= pt.x;
  }
  if(west != null){
   Point pt = getSize(west, flushCache);
   west.setBounds(left, top, pt.x,(bottom-top));
   left += pt.x;
  }
  if(center !=null){
   center.setBounds(left, top, (right-left), (bottom-top));
  }
 }

 private Point getSize(Control control, boolean flushCache) {
  return control.computeSize(SWT.DEFAULT,SWT.DEFAULT,flushCache);
 }


 private void getControls(Composite composite) {
  Control[] children = composite.getChildren();
  for(int i=0,n=children.length;i<n;i++){
   Control child = children[i];
   BorderData borderData = (BorderData)child.getLayoutData();
   if(borderData.region == SWT.TOP)
    north = child;
   else if(borderData.region == SWT.BOTTOM)
    south = child;
   else if(borderData.region == SWT.RIGHT)
    east = child;
   else if(borderData.region == SWT.LEFT)
    west = child;
   else
    center = child;
  }
 }
 

}

import org.eclipse.swt.SWT;

public class BorderData {

  public int region = SWT.CENTER;
  public BorderData(){
  }
  public BorderData(int region){
  this.region = region;
  }
}

 

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;

public class TestBorderLayout {

 public static void main(String[] args) {
  Display display =  new Display();
  Shell shell = new Shell(display);
  shell.setSize(200,150);
  shell.setLayout(new BorderLayout());
  shell.setLayoutData(new BorderData());
  Button buttonWest = new Button(shell,SWT.PUSH);
  buttonWest.setText("左");
  buttonWest.setLayoutData(new BorderData(SWT.LEFT));
  
  Button buttonEast = new Button(shell,SWT.PUSH);
  buttonEast.setText("右");
  buttonEast.setLayoutData(new BorderData(SWT.RIGHT));
  
  Button buttonSouth = new Button(shell,SWT.PUSH);
  buttonSouth.setText("下");
  buttonSouth.setLayoutData(new BorderData(SWT.BOTTOM));
  

  Button buttonNorth = new Button(shell,SWT.PUSH);
  buttonNorth.setText("上");
  buttonNorth.setLayoutData(new BorderData(SWT.TOP));
  
  Text text = new Text(shell,SWT.MULTI);
  text.setText("中间");
  text.setLayoutData(new BorderData(SWT.CENTER));
  
  shell.pack();
  shell.open();
  
  while (!shell.isDisposed()){
   if(!display.readAndDispatch()){
    display.sleep();
   }
  }
  display.dispose();
 }

}

 

 

 

热点排行