The simulation BorderLayout layout _java programming of SWT (JFace) Experience

Source: Internet
Author: User
Tags static class
There is no AWT borderlayout layout manager in SWT. Here is the custom implementation under SWT:
Borderlayout.java
Copy Code code as follows:

Package Swt_jface.demo2;
Import Org.eclipse.swt.SWT;
Import Org.eclipse.swt.graphics.Point;
Import Org.eclipse.swt.graphics.Rectangle;
Import Org.eclipse.swt.widgets.Composite;
Import Org.eclipse.swt.widgets.Control;
Import Org.eclipse.swt.widgets.Layout;
public class BorderLayout extends Layout {
public static final int north = 0;
public static final int SOUTH = 1;
public static final int CENTER = 2;
public static final int = 3;
public static final int west = 4;
public static class Borderdata {

public int region = CENTER;
Public Borderdata () {
}
Public Borderdata (int region) {
this.region = region;
}
}
Public control[] Controls = new CONTROL[5];
Point[] sizes;
int width;
int height;
Protected point computesize (composite composite, int whint, int hhint, Boolean flushcache) {

if (sizes = NULL | | flushcache = = TRUE)
Refreshsizes (Composite.getchildren ());
int w = whint;
int h = hhint;
if (w = = SWT. DEFAULT) w = width;
if (h = = SWT. DEFAULT) h = height;
Return to New Point (W, h);
}
protected void layout (composite composite, Boolean Flushcache) {
if (Flushcache | | sizes = NULL)
Refreshsizes (Composite.getchildren ());
Rectangle Clientarea = Composite.getclientarea ();
if (Controls[north]!= null) {
Controls[north].setbounds (
Clientarea.x,
CLIENTAREA.Y,
Clientarea.width,
SIZES[NORTH].Y);
}
if (Controls[south]!= null) {
Controls[south].setbounds (
Clientarea.x,
CLIENTAREA.Y + CLIENTAREA.HEIGHT-SIZES[SOUTH].Y,
Clientarea.width,
SIZES[SOUTH].Y);
}
if (Controls[west]!= null) {
Controls[west].setbounds (
Clientarea.x,
CLIENTAREA.Y + SIZES[NORTH].Y,
Sizes[west].x,
CLIENTAREA.HEIGHT-SIZES[NORTH].Y-SIZES[SOUTH].Y);
}
if (Controls[east]!= null) {
Controls[east].setbounds (
Clientarea.x + clientarea.width-sizes[east].x,
CLIENTAREA.Y + SIZES[NORTH].Y,
Sizes[east].x,
CLIENTAREA.HEIGHT-SIZES[NORTH].Y-SIZES[SOUTH].Y);
}
if (Controls[center]!= null) {
Controls[center].setbounds (
Clientarea.x + sizes[west].x,
CLIENTAREA.Y + SIZES[NORTH].Y,
Clientarea.width-sizes[west].x-sizes[east].x,
CLIENTAREA.HEIGHT-SIZES[NORTH].Y-SIZES[SOUTH].Y);
}
}
private void refreshsizes (control[] children) {

for (int i = 0; i < children.length; i++) {
Object layoutdata = Children[i].getlayoutdata ();
if (Layoutdata = null | | (! (Layoutdata instanceof Borderdata)))
Continue
Borderdata Borderdata = (borderdata) layoutdata;
if (Borderdata.region < 0 | | borderdata.region > 4)//Invalid.
Continue
Controls[borderdata.region] = Children[i];
}
width = 0;
Height = 0;
if (sizes = null)
Sizes = new Point[5];
for (int i = 0; i < controls.length; i++) {
Control control = Controls[i];
if (control = = null) {
Sizes[i] = new Point (0, 0);
} else {
Sizes[i] = control.computesize (SWT. DEFAULT, SWT. DEFAULT, True);
}
}
width = Math.max (width, sizes[north].x);
width = Math.max (width, sizes[west].x + sizes[center].x + sizes[east].x);
width = Math.max (width, sizes[south].x);
Height = Math.max (Math.max (SIZES[WEST].Y, sizes[east].y), SIZES[CENTER].Y)
+ SIZES[NORTH].Y
+ sizes[south].y;
}
}

Test code:
Borderlayoutsample.java
Copy Code code as follows:

Package Swt_jface.demo2;
Import Org.eclipse.swt.SWT;
Import Org.eclipse.swt.widgets.Button;
Import Org.eclipse.swt.widgets.Display;
Import Org.eclipse.swt.widgets.Shell;
Import Org.eclipse.swt.widgets.Text;
public class Borderlayoutsample {

Display display = new display ();
Shell shell = new shell (display);
Public Borderlayoutsample () {

Shell.setlayout (New BorderLayout ());

Button buttonwest = New button (Shell, SWT. PUSH);
Buttonwest.settext ("West");
Buttonwest.setlayoutdata (New Borderlayout.borderdata (borderlayout.west));

Button Buttoneast = New button (Shell, SWT. PUSH);
Buttoneast.settext ("East");
Buttoneast.setlayoutdata (New Borderlayout.borderdata (Borderlayout.east));
Button Buttonnorth = New button (Shell, SWT. PUSH);
Buttonnorth.settext ("North");
Buttonnorth.setlayoutdata (New Borderlayout.borderdata (Borderlayout.north));

Button Buttonsouth = New button (Shell, SWT. PUSH);
Buttonsouth.settext ("South");
Buttonsouth.setlayoutdata (New Borderlayout.borderdata (Borderlayout.south));

Text text = new text (Shell, SWT. MULTI | Swt. BORDER | Swt. V_scroll | Swt. H_scroll);
Text.settext ("Center");
Text.setlayoutdata (New Borderlayout.borderdata (Borderlayout.center));

Shell.pack ();
Shell.open ();

while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
Display.dispose ();
}
public static void Main (string[] args) {
New Borderlayoutsample ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.