JavaGUI之SWT框架 【SashForm CBanner】

文章目录

SashForm 分割窗

SashForm分割窗,能够将容器面板进行切割,分为两块。SashForm分割的方向有两种,一种是水平分割,另一种是垂直分割。

SashForm的创建方式如下

java 复制代码
// 创建分割窗口
SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);

// 创建窗口1
Composite c1 = new Composite(sashForm, SWT.BORDER);
c1.setLayout(new FillLayout());
new Text(c1, SWT.NONE).setText("窗口1");
// 创建窗口2
Composite c2 = new Composite(sashForm, SWT.BORDER);
c2.setLayout(new FillLayout());
new Text(c2, SWT.NONE).setText("窗口2");

// 设定每个窗口weight的比例值
sashForm.setWeights(new int[]{20, 40});

效果

样式

SWT.HORIZONTAL

请注意,在SashForm中,HORIZONTAL表示的是垂直划分


SWT.VERTICAL

请注意,在SashForm中,VERTICAL表示的是水平划分


SWT.BORDER

加粗边框


SWT.SMOOTH

平滑边框

设置窗口显示比例

sashForm.setWeights(new int[]{25, 50, 25}); 其中int\[\]数组的元素个数等于绑定sashForm面板的元素的个数。int\[\]数组中的数值表示每个组件的宽度比例

多层划分窗口

如果想要在已经分割的窗口上继续分割,可以在窗口上继续绑定SashForm

java 复制代码
public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setSize(500, 400);

    // 创建两列的sashForm
    SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);
    sashForm.setLayout(new GridLayout(1, true));

    // 划分左侧区间, 创建两行的leftSashForm
    SashForm leftSashForm = new SashForm(sashForm, SWT.VERTICAL);
    leftSashForm.setLayout(new GridLayout(1, true));

    Composite leftUp = new Composite(leftSashForm, SWT.BORDER);
    leftUp.setLayout(new FillLayout());
    new Text(leftUp, SWT.NONE).setText("左上");

    Composite leftDown = new Composite(leftSashForm, SWT.BORDER);
    leftDown.setLayout(new FillLayout());
    new Text(leftDown, SWT.NONE).setText("左下");

    Composite right = new Composite(sashForm, SWT.BORDER);
    right.setLayout(new FillLayout());
    new Text(right, SWT.NONE).setText("右");

    shell.open();
    while (!shell.isDisposed()) {
        while (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    shell.dispose();
}

CBanner

CBanner将容器划分为三个部分,左侧,右侧,底栏

CBanner的创建方式如下

java 复制代码
CBanner cBanner = new CBanner(shell, SWT.BORDER);
cBanner.setLayout(new FillLayout());

// 创建3个面板
Composite c1 = new Composite(cBanner, SWT.NONE);
c1.setLayout(new FillLayout());
new Label(c1, SWT.NONE).setText("面板1");

Composite c2 = new Composite(cBanner, SWT.NONE);
c2.setLayout(new FillLayout());
new Label(c2, SWT.NONE).setText("面板2");

Composite c3 = new Composite(cBanner, SWT.NONE);
c3.setLayout(new FillLayout());
new Label(c3, SWT.NONE).setText("面板3");

// 设置控件
cBanner.setLeft(c1);
cBanner.setRight(c2);
cBanner.setBottom(c3);

效果

CBanner需要额外手动设置绑定位置。绑定为api为setLeft(Composite composite), setRight(Composite composite), setBottom(Composite composite)

相关推荐
咖啡八杯37 分钟前
GoF设计模式——迭代器模式
java·后端·设计模式·迭代器模式
AIGS0011 小时前
企业AI落地的关键认知:向量空间JBoltAI的本体语义平台
java·人工智能·人工智能ai大模型应用
KaMeidebaby2 小时前
卡梅德生物技术快报|抗体亲和力成熟工业化调控新机制:差异性浆细胞增殖工艺优化思路
java·开发语言·人工智能·算法·机器学习·架构·spark
醉城夜风~3 小时前
Java详解经典算法题:接雨水(三种实现方案+原理剖析)
java·开发语言·算法
数智化转型推荐官4 小时前
2026统一身份管理系统五大发展趋势解读
java·运维·微服务
看菜鸡互4 小时前
探索用 SlideML 让大模型生成 PPT 的实验方法
java·运维
程序员张36 小时前
SpringBoot集成BCrypt密码加密库
java·spring boot·后端
闲猫6 小时前
Spring AI Agentic 模式(第1部分):Agent Skills——模块化、可复用的能力
java·人工智能·spring
源图客6 小时前
云途物流API开发-鉴权认证(Java)
java·网络·python
liguojun20256 小时前
篮球馆自动计时收费系统:从规则配置到自动结算的全流程拆解
java·大数据·运维·人工智能·物联网·1024程序员节