麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Java > 正文

SWT(JFace)體驗之模擬BorderLayout布局

2020-01-31 16:51:47
字體:
來源:轉載
供稿:網友
SWT中沒有AWT的BorderLayout布局管理器。下面是SWT下的自定義實現:
BorderLayout.java
復制代碼 代碼如下:

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 EAST = 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 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;
}
}

測試代碼:
BorderLayoutSample.java
復制代碼 代碼如下:

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();
}
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久精品久久久久 | 欧美a一| 99麻豆久久久国产精品免费 | 欧美成人高清视频 | 蜜桃麻豆视频 | 国产精品久久在线观看 | 国产精品999在线观看 | 在线观看福利网站 | 9999久久 | 亚洲成人福利在线 | 国产一级片91 | 免费观看一级黄色片 | 成人在线免费视频播放 | 最新黄色电影网站 | 欧美日韩精品中文字幕 | 毛片在线免费 | 毛片视频观看 | 看免费黄色大片 | 久久午夜神器 | 国产精品久久久久久久久久妇女 | 91成人免费视频 | av手机在线免费播放 | 免费毛片播放 | 一区二区三区日本在线观看 | 逼特逼视频在线观看 | 久久久一区二区三区精品 | 中国av一级片 | 欧美成人免费看 | 高清国产午夜精品久久久久久 | 国产美女视频一区二区三区 | 91在线视频网址 | 中文字幕国产一区 | 999久久久| 亚欧美一区二区 | 久久在线免费视频 | 91精品国产91久久久久久吃药 | 久久久久免费精品国产小说色大师 | 国产一级一片免费播放 | 欧美成人国产va精品日本一级 | 午夜在线成人 | 日本网站在线播放 |