eclipse rcp:布局自定义复合小部件

发布于 2024-12-10 19:36:32 字数 2330 浏览 1 评论 0原文

我正在尝试编写一个自定义小部件:

package amarsoft.rcp.base.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;

public class SearchBox extends Composite {

    private Text text;

    private Label icon;

    public SearchBox(Composite parent) {
        super(parent, SWT.BORDER);
        setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
        setBackgroundMode(SWT.INHERIT_FORCE);

        icon = new Label(this, SWT.NONE);
        icon.setImage(ResourceManager.getImage("icons/search.png"));

        text = new Text(this, SWT.NONE);
        addControlListener(new ControlAdapter() {
            @Override
            public void controlResized(ControlEvent e) {
                resize();
            }

        });
    }

    void resize() {
        Point iExtent = icon.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
        Point tExtent = text.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
        icon.setBounds(tExtent.x, 1, iExtent.x, iExtent.y);
        text.setBounds(1, 1, tExtent.x, tExtent.y);
    }

    @Override
    public Point computeSize(int wHint, int hHint, boolean changed) {
        Point iExtent = icon.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
        Point tExtent = text.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
        System.out.println(iExtent);
        System.out.println(tExtent);
        int width = iExtent.x + tExtent.x;
        int height = Math.max(iExtent.y, tExtent.y);
        if (wHint != SWT.DEFAULT)
            width = wHint;
        if (hHint != SWT.DEFAULT)
            height = hHint;
        System.out.println(new Point(width + 2, height + 2));
        return new Point(width + 5, height + 5);
    }

}

如果我尝试在具有 RowLayout 的父组合中使用它,它看起来像这样:
在此处输入图像描述

但如果我尝试在具有 FillLayout 的父复合材料中使用它,它看起来像: 在此处输入图像描述

我怎样才能使它始终像这样运行: 始终具有固定的高度,最小的宽度,如果有更多空间,可以在水平方向调整大小。

I am trying to write a custom widget:

package amarsoft.rcp.base.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;

public class SearchBox extends Composite {

    private Text text;

    private Label icon;

    public SearchBox(Composite parent) {
        super(parent, SWT.BORDER);
        setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
        setBackgroundMode(SWT.INHERIT_FORCE);

        icon = new Label(this, SWT.NONE);
        icon.setImage(ResourceManager.getImage("icons/search.png"));

        text = new Text(this, SWT.NONE);
        addControlListener(new ControlAdapter() {
            @Override
            public void controlResized(ControlEvent e) {
                resize();
            }

        });
    }

    void resize() {
        Point iExtent = icon.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
        Point tExtent = text.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
        icon.setBounds(tExtent.x, 1, iExtent.x, iExtent.y);
        text.setBounds(1, 1, tExtent.x, tExtent.y);
    }

    @Override
    public Point computeSize(int wHint, int hHint, boolean changed) {
        Point iExtent = icon.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
        Point tExtent = text.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
        System.out.println(iExtent);
        System.out.println(tExtent);
        int width = iExtent.x + tExtent.x;
        int height = Math.max(iExtent.y, tExtent.y);
        if (wHint != SWT.DEFAULT)
            width = wHint;
        if (hHint != SWT.DEFAULT)
            height = hHint;
        System.out.println(new Point(width + 2, height + 2));
        return new Point(width + 5, height + 5);
    }

}

if I try to use it in a parent composite which has a RowLayout, it looks like this:
enter image description here

but if I try to use it in a parent composite which has a FillLayout, it looks like :
enter image description here

how can I make it always behavior like this:
always has a fixed height, a minimal width, can be resized at horizontal direction if there is more space for it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽容 2024-12-17 19:36:32

父组合体可以为其子组合体提供所需的任何大小。对于单个子项,FillLayout 将调整子项的大小以占用所有可用空间。这就是它的工作原理以及它应该如何工作。

父组合有责任以合适的方式布局其子组合。换句话说,FillLayout 在这里使用的布局是错误的。使用常规 Text 控件进行尝试。 FillLayout 将使它比单行文本所需的高度高得多。

The parent composite can give its children any size it wants. With a single child, FillLayout will resize the child to consume all available space. That's just how it works and how it's supposed to work.

It's the responsibility of the parent composite to layout its children in a suitable way. In other words, FillLayout is just the wrong layout to use here. Try it with a regular Text control. The FillLayout will make it much higher than it needs to be for its single line of text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文