Java 计算复合材料的最小尺寸
我需要计算复合材料的最小或默认大小,它可以显示所有组件而无需剪裁 我似乎只找到了计算复合材料首选尺寸的方法。这意味着一个表或其他滚动组合将有一个首选的大小,显示完整的内容而不滚动。ScrolledComposite将立即进入滚动模式,这不是我想要的 GridLayout通过将GridData提示视为最小宽度/高度来实现这一点,允许获取任何额外的可用空间 问题与此相关:应该是您正在搜索的内容:Java 计算复合材料的最小尺寸,java,swt,Java,Swt,我需要计算复合材料的最小或默认大小,它可以显示所有组件而无需剪裁 我似乎只找到了计算复合材料首选尺寸的方法。这意味着一个表或其他滚动组合将有一个首选的大小,显示完整的内容而不滚动。ScrolledComposite将立即进入滚动模式,这不是我想要的 GridLayout通过将GridData提示视为最小宽度/高度来实现这一点,允许获取任何额外的可用空间 问题与此相关:应该是您正在搜索的内容: Point size = comp.computeSize(SWT.DEFAULT, SWT.DEFAU
Point size = comp.computeSize(SWT.DEFAULT, SWT.DEFAULT);
System.out.println(size.x + " " + size.y);
应该是您正在搜索的内容:
Point size = comp.computeSize(SWT.DEFAULT, SWT.DEFAULT);
System.out.println(size.x + " " + size.y);
我设法找到了解决办法 关键是两件不同的事情: 如果添加了子项并调用了布局,请确保在内容和ScrolledComposite(如果从子项外部调整大小)上设置resize侦听器 确保同时设置GridData.grab和GridData.hint。提示将确保当您进行计算时,组合采用此大小,而grab将确保它将获取任何可用的额外空间。 代码示例如下:
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell(display);
ScrolledComposite sc = new ScrolledComposite(shell, SWT.NONE);
Composite foo = new Composite(sc, SWT.NONE);
foo.setLayout(new GridLayout(1, false));
StyledText text = new StyledText(foo, SWT.NONE);
text.setText("Ipsum dolor etc... \n etc... \n etc....");
GridDataFactory.fillDefaults().grab(true, true).hint(40, 40).applyTo(text);
Listener l = new Listener() {
public void handleEvent(Event e) {
Point size = sc.getSize();
Point cUnrestrainedSize = content.computeSize(SWT.DEFAULT, SWT.DEFAULT);
if(size.y >= cUnrestrainedSize.y && size.x >= cUnrestrainedSize.x) {
content.setSize(size);
return;
}
// does not fit
Rectangle hostRect = getBounds();
int border = getBorderWidth();
hostRect.width -= 2*border;
hostRect.width -= getVerticalBar().getSize().x;
hostRect.height -= 2*border;
hostRect.height -= getHorizontalBar().getSize().y;
c.setSize(
Math.max(cUnrestrainedSize.x, hostRect.width),
Math.max(cUnrestrainedSize.y, hostRect.height)
);
}
}
sc.addListener(SWT.Resize, l);
foo.addListener(SWT.Resize, l);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
我设法找到了解决办法 关键是两件不同的事情: 如果添加了子项并调用了布局,请确保在内容和ScrolledComposite(如果从子项外部调整大小)上设置resize侦听器 确保同时设置GridData.grab和GridData.hint。提示将确保当您进行计算时,组合采用此大小,而grab将确保它将获取任何可用的额外空间。 代码示例如下:
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell(display);
ScrolledComposite sc = new ScrolledComposite(shell, SWT.NONE);
Composite foo = new Composite(sc, SWT.NONE);
foo.setLayout(new GridLayout(1, false));
StyledText text = new StyledText(foo, SWT.NONE);
text.setText("Ipsum dolor etc... \n etc... \n etc....");
GridDataFactory.fillDefaults().grab(true, true).hint(40, 40).applyTo(text);
Listener l = new Listener() {
public void handleEvent(Event e) {
Point size = sc.getSize();
Point cUnrestrainedSize = content.computeSize(SWT.DEFAULT, SWT.DEFAULT);
if(size.y >= cUnrestrainedSize.y && size.x >= cUnrestrainedSize.x) {
content.setSize(size);
return;
}
// does not fit
Rectangle hostRect = getBounds();
int border = getBorderWidth();
hostRect.width -= 2*border;
hostRect.width -= getVerticalBar().getSize().x;
hostRect.height -= 2*border;
hostRect.height -= getHorizontalBar().getSize().y;
c.setSize(
Math.max(cUnrestrainedSize.x, hostRect.width),
Math.max(cUnrestrainedSize.y, hostRect.height)
);
}
}
sc.addListener(SWT.Resize, l);
foo.addListener(SWT.Resize, l);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
ControlcomputeSize将提供不带滚动条显示文本所需的大小。这不是我想要的。ControlcomputeSize将提供不带滚动条显示文本所需的大小。这不是我想要的。