SWT(JFace)体验之模拟BorderLayout布局
时间:2022-08-17 18:09:51
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();
}
}
标签:SWT,BorderLayout布局
0
投稿
猜你喜欢
Java基于Runtime调用外部程序出现阻塞的解决方法
2023-11-09 04:24:23
Java算法比赛常用方法实例总结
2023-11-28 07:15:26
Springboot导出文件,前端下载文件方式
2023-07-21 11:27:05
单点登录的三种方式和JWT的介绍与使用
2023-05-19 22:10:59
springcloud eureka切换nacos的配置方法
2022-05-19 01:58:47
MyBatis查询数据返回null的解决
2021-11-17 20:46:48
Java设计模式的事件模型详解
2023-11-29 04:47:08
SpringBoot中使用Redis Stream实现消息监听示例
2021-09-02 04:42:07
C#七大经典排序算法系列(上)
2023-08-14 04:30:45
全局记录Feign的请求和响应日志方式
2021-08-19 18:48:02
解析android中的dip,dp,px,sp和屏幕密度
2023-09-26 20:44:38
Java Swing JLabel标签的使用方法
2021-09-16 05:48:13
详解Java类型擦除机制
2023-10-29 06:41:21
Java Stream流零基础教程
2023-08-15 19:33:20
Java接口和抽象类有什么区别
2021-08-21 07:01:18
列举java语言中反射的常用方法及实例代码
2022-10-31 13:45:07
springboot如何使用logback-spring配置日志格式,并分环境配置
2023-11-10 04:37:34
Java swing实现酒店管理系统
2021-08-08 02:24:12
一篇文章带你Java Spring开发入门
2021-06-25 10:04:15
一文搞懂Java ScheduledExecutorService的使用
2022-11-22 14:23:35