eclipse rcp:布局自定义复合小部件
我正在尝试编写一个自定义小部件:
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:
but if I try to use it in a parent composite which has a FillLayout, it looks like :
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
父组合体可以为其子组合体提供所需的任何大小。对于单个子项,
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 regularText
control. TheFillLayout
will make it much higher than it needs to be for its single line of text.