在SWT下实现BorderLayout布局

由于SWT下不支持BorderLayout布局,所以需要自己定义一个布局来实现BorderLayout布局的效果。

 

BorderLayout类

 

  1. import org.eclipse.swt.SWT;
  2. import org.eclipse.swt.graphics.Point;
  3. import org.eclipse.swt.graphics.Rectangle;
  4. import org.eclipse.swt.widgets.Composite;
  5. import org.eclipse.swt.widgets.Control;
  6. import org.eclipse.swt.widgets.Layout;
  7. class BorderLayout extends Layout {
  8.   // Region constants.
  9.   public static final int NORTH = 0;
  10.   public static final int SOUTH = 1;
  11.   public static final int CENTER = 2;
  12.   public static final int EAST = 3;
  13.   public static final int WEST = 4;
  14.   /**
  15.    * Indicates the region that a control belongs to.
  16.    *
  17.    */
  18.   public static class BorderData {
  19.     public int region = CENTER; // default.
  20.     public BorderData() {
  21.     }
  22.     public BorderData(int region) {
  23.       this.region = region;
  24.     }
  25.   }
  26.   // Controls in all the regions.
  27.   public Control[] controls = new Control[5];
  28.   // Cached sizes.
  29.   Point[] sizes;
  30.   // Preferred width and height
  31.   int width;
  32.   int height;
  33.   /*
  34.    * (non-Javadoc)
  35.    *
  36.    * @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.Composite,
  37.    *      int, int, boolean)
  38.    */
  39.   protected Point computeSize(
  40.     Composite composite,
  41.     int wHint,
  42.     int hHint,
  43.     boolean flushCache) {
  44.     if (sizes == null || flushCache == true)
  45.       refreshSizes(composite.getChildren());
  46.     int w = wHint;
  47.     int h = hHint;
  48.     if (w == SWT.DEFAULT)
  49.       w = width;
  50.     if (h == SWT.DEFAULT)
  51.       h = height;
  52.     return new Point(w, h);
  53.   }
  54.   /*
  55.    * (non-Javadoc)
  56.    *
  57.    * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite,
  58.    *      boolean)
  59.    */
  60.   protected void layout(Composite composite, boolean flushCache) {
  61.     if (flushCache || sizes == null)
  62.       refreshSizes(composite.getChildren());
  63.     Rectangle clientArea = composite.getClientArea();
  64.     // Enough space for all.
  65.     if (controls[NORTH] != null) {
  66.       controls[NORTH].setBounds(
  67.         clientArea.x,
  68.         clientArea.y,
  69.         clientArea.width,
  70.         sizes[NORTH].y);
  71.     }
  72.     if (controls[SOUTH] != null) {
  73.       controls[SOUTH].setBounds(
  74.         clientArea.x,
  75.         clientArea.y + clientArea.height - sizes[SOUTH].y,
  76.         clientArea.width,
  77.         sizes[SOUTH].y);
  78.     }
  79.     if (controls[WEST] != null) {
  80.       controls[WEST].setBounds(
  81.         clientArea.x,
  82.         clientArea.y + sizes[NORTH].y,
  83.         sizes[WEST].x,
  84.         clientArea.height - sizes[NORTH].y - sizes[SOUTH].y);
  85.     }
  86.     if (controls[EAST] != null) {
  87.       controls[EAST].setBounds(
  88.         clientArea.x + clientArea.width - sizes[EAST].x,
  89.         clientArea.y + sizes[NORTH].y,
  90.         sizes[EAST].x,
  91.         clientArea.height - sizes[NORTH].y - sizes[SOUTH].y);
  92.     }
  93.     if (controls[CENTER] != null) {
  94.       controls[CENTER].setBounds(
  95.         clientArea.x + sizes[WEST].x,
  96.         clientArea.y + sizes[NORTH].y,
  97.         clientArea.width - sizes[WEST].x - sizes[EAST].x,
  98.         clientArea.height - sizes[NORTH].y - sizes[SOUTH].y);
  99.     }
  100.   }
  101.   private void refreshSizes(Control[] children) {
  102.     for (int i = 0; i < children.length; i++) {
  103.       Object layoutData = children[i].getLayoutData();
  104.       if (layoutData == null || (!(layoutData instanceof BorderData)))
  105.         continue;
  106.       BorderData borderData = (BorderData) layoutData;
  107.       if (borderData.region < 0 || borderData.region > 4// Invalid.
  108.         continue;
  109.       controls[borderData.region] = children[i];
  110.     }
  111.     width = 0;
  112.     height = 0;
  113.     if (sizes == null)
  114.       sizes = new Point[5];
  115.     for (int i = 0; i < controls.length; i++) {
  116.       Control control = controls[i];
  117.       if (control == null) {
  118.         sizes[i] = new Point(00);
  119.       } else {
  120.         sizes[i] = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);      }
  121.     }
  122.     width = Math.max(width, sizes[NORTH].x);
  123.     width =
  124.       Math.max(width, sizes[WEST].x + sizes[CENTER].x + sizes[EAST].x);
  125.     width = Math.max(width, sizes[SOUTH].x);
  126.     height =
  127.       Math.max(Math.max(sizes[WEST].y, sizes[EAST].y), sizes[CENTER].y)
  128.         + sizes[NORTH].y
  129.         + sizes[SOUTH].y;
  130.   }
  131. }

下面是引用示例:

 

  1. import org.eclipse.swt.SWT;
  2. import org.eclipse.swt.widgets.Button;
  3. import org.eclipse.swt.widgets.Display;
  4. import org.eclipse.swt.widgets.Shell;
  5. import org.eclipse.swt.widgets.Text;
  6. public class BorderLayoutSample {
  7.   Display display = new Display();
  8.   Shell shell = new Shell(display);
  9.   public BorderLayoutSample() {
  10.     shell.setLayout(new BorderLayout());
  11.     Button buttonWest = new Button(shell, SWT.PUSH);
  12.     buttonWest.setText("West");
  13.     buttonWest.setLayoutData(new BorderLayout.BorderData(BorderLayout.WEST));
  14.     Button buttonEast = new Button(shell, SWT.PUSH);
  15.     buttonEast.setText("East");
  16.     buttonEast.setLayoutData(new BorderLayout.BorderData(BorderLayout.EAST));
  17.     Button buttonNorth = new Button(shell, SWT.PUSH);
  18.     buttonNorth.setText("North/nYes");
  19.     buttonNorth.setLayoutData(new BorderLayout.BorderData(BorderLayout.NORTH));
  20.     Button buttonSouth = new Button(shell, SWT.PUSH);
  21.     buttonSouth.setText("South");
  22.     buttonSouth.setLayoutData(new BorderLayout.BorderData(BorderLayout.SOUTH));
  23.     Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
  24.     text.setText("Center");
  25.     text.setLayoutData(new BorderLayout.BorderData(BorderLayout.CENTER));
  26.     shell.pack();
  27.     shell.open();
  28.     //textUser.forceFocus();
  29.     // Set up the event loop.
  30.     while (!shell.isDisposed()) {
  31.       if (!display.readAndDispatch()) {
  32.         // If no more entries in event queue
  33.         display.sleep();
  34.       }
  35.     }
  36.     display.dispose();
  37.   }
  38.   private void init() {
  39.   }
  40.   public static void main(String[] args) {
  41.     new BorderLayoutSample();
  42.   }
  43. }

你可能感兴趣的:(java)