SWT(JFace)体验之模拟BorderLayout布局
发布时间:2022-08-17 18:09:51
标签:SWT,BorderLayout布局
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();
}
}


猜你喜欢
- 废话不多说了,直接给大家贴关键代码了。具体代码如下所示:using System;using System.Collections.Gene
- 首先我们看下object源码中如何定义hashcode与equals方法的public native int hashCode();publ
- 图像的旋转需要调用 Graphics2D 类的rota
- Android自带的跑马灯效果不太好控制,还必须要满足条件才能有效果,而且速度不受控制。前面我的博客中有一篇就是用Android自带的跑马灯
- 摘要在生产环境下,我们需要关闭swagger配置,避免暴露接口的这种危险行为。方法禁用方法1:使用注解 @Value() 推荐使用packa
- C# 5.0 给我们带来了三个非常有用的编译器特性CallerMemberNameCallerFilePathCallerLineNumbe
- C# WPF ListView控件的实例详解C#的WPF作为现在微软主流的桌面程序开发平台,相比过去的MFC时代,有了非常多的不同。本人刚从
- 自定义单元格表示值通过CellFormatting事件,可以自定义单元格的表示值。(比如:值为Error的时候,单元格被设定为红色)示例:p
- Spring Security 过滤器链及自定义Filter别名类名称Namespace Element or AttributeCHANN
- Stream流多字段求和、汇聚实现方法利用Collectors.toMap(Function keyMapper, Function val
- 在本篇博文中,我们主要讲解一下 IntelliJ IDEA 安装目录中的一些核心文件的功能及用法:如上图所示,我们定位到了 IntelliJ
- 本教程基于 JetBrains IntelliJ IDEA 2018.3.6 编写,高版本未经测试,或有不兼容,请见谅!JetBrains
- 前言Flutter (Channel stable, 2.10.3, on Microsoft Windows [Version 10.0.
- 前言这几天听朋友说JPA很好用,根本不用写sql。我在想一个程序员不写sql还能叫程序员?而且越高级的工具封装越多的工具,可拓展性和效率就非
- 问题背景: 我要在一个表单里同时一次性提交多名乘客的个人信息到SpringMVC,前端HTML和SpringMVC Controller里该
- 前言今天看代码看到有牵扯到弱引用的东西,就先稍微补一补Java的四种引用类型吧。Java为引用类型专门定义了一个类Reference,它是引
- 软硬件环境Windows 10Android studio 2.3.2OTT BOx with android 5.1.1前言App开发测试
- 我们知道,在java中,将一个非原型类型类型的对象引用,赋值给另一个对象的引用之后,这两个引用就指向了同一个对象,如:public clas
- 在Java解析XML文件的过程中,有时需要获取符合某些特定条件的节点,以下是实现代码。import javax.xml.xpath.XPat
- Java中避免NullPointerException的方法总结在字符串常量上调用equals// good"string lit