java - 获取 SWT View 的大小

标签 java eclipse-plugin swt

我正在尝试确定 SWT View 的大小,以便可以在插件中正确布局小部件。我正在使用 Java 8 的 Eclipse Neon 上运行。

我使用的代码如下:

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 表示 View 的大小。我尝试过scrollable.getClientArea和Composite.getSize。

如何确定可用的工作区域?

最佳答案

调用 createPartControl 时,尺寸尚未确定。

设置尺寸时将会发生Resize事件。

在主Composite上使用getClientArea()来获取 View 区域的大小。

关于java - 获取 SWT View 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51326028/

相关文章:

widget - SWT 使用 TAB 在 TableViewer 中移动

java - 32 位和 64 位 SWT 之间的性能差异是什么?

Mac 上的 Java 表 怪异

java - 如何使用 target/...jar 执行 scala 对象?

java - 在 Android 游戏逻辑中解锁关卡

html - Eclipse 的 JavaDoc 编辑器,用于创建格式化文本

java - 如何检查变量注释?

Java线程yield方法查询

java - 缩短 switch block 内 case 语句中的代码行

java - 如何在eclipse中将另一个java项目打包为osgi依赖项