有没有办法设置组合的最大尺寸?我只找到了一些方法
setMinimumSize(Point point)
setSize(Point point)
这些选项允许我设置最小和首选大小。
Widget
/Composite
的最大尺寸设置为您认为“看起来不错”的值。根据最终用户的屏幕分辨率和文本大小,所选的最大尺寸可能会看起来不合适。这就是为什么布局通常会自动适应可用空间。
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
final Button left = new Button(shell, SWT.PUSH);
left.setText("Restricted width");
left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
left.addListener(SWT.Resize, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
Point size = left.getSize();
if(size.x > 200)
{
left.setSize(200, size.y);
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
调整大小之前:
调整大小之后:
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
final Text text = new Text(shell, SWT.WRAP | SWT.MULTI | SWT.V_SCROLL);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
text.setText(getLongText());
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
shell.pack()
将使窗口使用首选大小显示其内容。然后,布局会要求文本小部件计算其首选大小。文本小部件不知道窗口也会被扩展,并且没有固定的大小。final Text text = new Text(shell, SWT.WRAP | SWT.MULTI | SWT.V_SCROLL) {
/** The maximum preferred size. */
private final Point maxSize = new Point(500, 400);
@Override
public Point computeSize(final int wHint, final int hHint, final boolean changed) {
final Point preferredSize = super.computeSize(wHint, hHint, changed);
return new Point(Math.min(maxSize.x, preferredSize.x), Math.min(maxSize.y, preferredSize.y));
}
@Override
protected void checkSubclass() {
// noop, allow subclassing, since we know what we are doing
}
};
现在用户仍然可以通过拖动操作来增加外壳的大小,而小部件仍然能够在超出最大尺寸的情况下占用更多空间。