SWT(JFace)体验之模拟BorderLayout布局

 更新时间:2009年06月25日 11:04:47   作者:  
SWT(JFace)体验之模拟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();
}
}

相关文章

  • springboot配置https访问的方法

    springboot配置https访问的方法

    这篇文章主要介绍了springboot配置https访问的方法,需要的朋友可以参考下
    2018-11-11
  • java类中使用Jfreechart的简单实例

    java类中使用Jfreechart的简单实例

    这篇文章介绍了java类中使用Jfreechart的简单实例,有需要的朋友可以参考一下
    2013-08-08
  • Java雪花算法生成分布式id详解

    Java雪花算法生成分布式id详解

    这篇文章主要介绍了Java雪花算法生成分布式id详解,随着业务的增长,有些表可能要占用很大的物理存储空间,为了解决该问题,后期使用数据库分片技术,将一个数据库进行拆分,通过数据库中间件连接,需要的朋友可以参考下
    2024-01-01
  • Nacos Namespace/Group/DataID三者关系解读

    Nacos Namespace/Group/DataID三者关系解读

    本文介绍了Nacos中的命名空间(Namespace)、配置分组(Group)和配置集ID(DataID)的概念,并详细说明了它们之间的关系和应用场景,同时,还提供了三者在不同环境下的配置切换方案,包括DataID方案、Group方案和命名空间方案
    2024-12-12
  • Spring Boot 搭建 ELK正确看日志的配置流程

    Spring Boot 搭建 ELK正确看日志的配置流程

    这篇文章主要介绍了Spring Boot 搭建 ELK正确看日志的配置流程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • 手把手教你使用IDEA创建多模块(maven)项目

    手把手教你使用IDEA创建多模块(maven)项目

    这篇文章主要给大家介绍了关于如何使用IDEA创建多模块(maven)项目的相关资料,文中通过图文以及实例代码介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • 配置springboot项目使用外部tomcat过程解析

    配置springboot项目使用外部tomcat过程解析

    这篇文章主要介绍了配置springboot项目使用外部tomcat过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • springboot使用redisTemplate操作lua脚本

    springboot使用redisTemplate操作lua脚本

    本文主要介绍了springboot使用redisTemplate操作lua脚本,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • java开发就业信息管理系统

    java开发就业信息管理系统

    这篇文章主要为大家详细介绍了java开发就业信息管理平台,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Java利用Dijkstra和Floyd分别求取图的最短路径

    Java利用Dijkstra和Floyd分别求取图的最短路径

    本文主要介绍了图的最短路径的概念,并分别利用Dijkstra算法和Floyd算法求取最短路径,最后提供了基于邻接矩阵和邻接表的图对两种算法的Java实现。需要的可以参考一下
    2022-01-01

最新评论