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();
 }

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值