问题描述
我正在尝试确定SWT视图的大小,以便可以在插件中正确布置窗口小部件.我正在Java 8的Eclipse Neon上运行.
I am attempting to determine the size of a SWT view so I can layout the widgets correctly in a plugin. I am running on Eclipse Neon with Java 8.
我正在使用的代码如下:
The code I am using follows:
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
public class ClockView extends ViewPart {
/**
* The constructor.
*/
public ClockView() {
}
/**
* This is a callback that will allow us
* to create the viewer and initialize it.
*/
@Override
public void createPartControl(Composite parent) {
final RowLayout layout = new RowLayout(SWT.HORIZONTAL);
layout.center = true;
layout.justify = true;
final Point area = parent.getSize();
final ImmutableList<Integer> clockWidths = ImmutableList.<Integer>of(100, 200, 400);
layout.marginWidth = (area.x - Iterables.getLast(clockWidths, null)) / 2;
layout.marginHeight = (area.y - Iterables.getLast(clockWidths, null)) / 2;
parent.setLayout(layout);
clockWidths.forEach(width -> {
ClockWidget c = new ClockWidget(parent, SWT.NONE);
c.setLayoutData(new RowData(width, width));
});
}
/**
* Passing the focus request to the viewer's control.
*/
@Override
public void setFocus() {
}
}
我的问题是,我尝试的每个方法都返回0,视图的大小返回0.我已经尝试过scrollable.getClientArea和Composite.getSize.
My problem is that every method I tried returns 0, 0 for the size of the view. I have tried scrollable.getClientArea and Composite.getSize.
如何确定可以使用的区域?
How can I determine the area available to work with?
推荐答案
调用 createPartControl
时,大小尚未确定.
Sizes haven't been determined when createPartControl
is called.
设置大小后,将发生Resize事件.
There will be a Resize event when the size is set.
在主 Composite
上使用 getClientArea()
获取视图区域的大小.
Use getClientArea()
on the main Composite
to get the size of the view area.
这篇关于获取SWT视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!