java - JFace 对话框布局问题,在窗口边界之外绘制的小部件

标签 java dialog swt jface

我正在尝试布局一个对话框,左侧有一个滚动面板,右侧有一个 Canvas ,填充可用空间但不被按钮栏覆盖。在我的示例中可以看到我遇到的问题是,复合 Material 似乎认为它们有无限的空间来吸引自己,而我无法理解这应该如何与任何东西一起工作。

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class FMLDialog extends Dialog
{   
    public FMLDialog(Shell parentShell)
    {
        super(parentShell);
    }


    @Override
    protected void configureShell(Shell shell)
    {
        super.configureShell(shell);
        shell.setSize(new Point(700,500));
        shell.setText("FML");
    }   

    @Override
    public Control createDialogArea(final Composite comp)
    {

        Composite content = (Composite) super.createDialogArea(comp);
        Composite parent = new Composite(content, SWT.NONE);        

        GridLayout gridLayout1 = new GridLayout(1, false);
        gridLayout1.verticalSpacing=10;
        GridLayout gridLayout2 = new GridLayout(2, false);
        parent.setLayout(gridLayout2);
        parent.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));           

        final ScrolledComposite scroll = new ScrolledComposite(parent, SWT.V_SCROLL);
        scroll.setExpandHorizontal(true);
        scroll.setExpandVertical(true);

        Composite optionsPanel = new Composite(scroll, SWT.BORDER);

        optionsPanel.setSize(optionsPanel.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        optionsPanel.setLayout(gridLayout1);
        scroll.setContent(optionsPanel);

        for (int i = 0; i < 200; i++) {
            new Label(optionsPanel, SWT.NONE).setText("this is item " + i);
        }

        scroll.setSize(optionsPanel.computeSize(SWT.DEFAULT, SWT.DEFAULT));

        final Canvas c = new Canvas(parent, SWT.BORDER);
        c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        c.addPaintListener(new PaintListener() {

            @Override
            public void paintControl(PaintEvent e) {
                Point size = c.getSize();
                e.gc.drawText("Width: " + size.x + "\nHeight: " + size.y, 5, 5);
                e.gc.drawOval(2, 2, size.x - 4, size.y - 4);
            }
        });

        return content;     

    }

    public static void main(String[] args) {
        Display d = new Display();
        Shell s = new Shell();

        FMLDialog fml = new FMLDialog(s);
        fml.open();
    }

}

最佳答案

这里存在一些问题,首先是不调用 ScrolledComposite#setMinSize(),以及 GridDataLayout 的一些小问题。

这是代码的修改版本,可以运行:

public class FMLDialog extends Dialog
{
    public FMLDialog(Shell parentShell)
    {
        super(parentShell);
    }

    @Override
    protected void configureShell(Shell shell)
    {
        super.configureShell(shell);
        shell.setSize(new Point(700, 500));
        shell.setText("FML");
    }

    @Override
    public Control createDialogArea(final Composite comp)
    {
        Composite content = (Composite) super.createDialogArea(comp);
        content.setLayout(new GridLayout(2, false));
        content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        final ScrolledComposite scroll = new ScrolledComposite(content, SWT.V_SCROLL);
        scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        scroll.setExpandHorizontal(true);
        scroll.setExpandVertical(true);

        Composite optionsPanel = new Composite(scroll, SWT.BORDER);
        optionsPanel.setLayout(new FillLayout(SWT.VERTICAL));
        optionsPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        for (int i = 0; i < 200; i++)
        {
            new Label(optionsPanel, SWT.NONE).setText("this is item " + i);
        }

        scroll.setContent(optionsPanel);
        scroll.setMinSize(optionsPanel.computeSize(SWT.DEFAULT, SWT.DEFAULT));

        final Canvas c = new Canvas(content, SWT.BORDER);
        c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        c.addPaintListener(new PaintListener()
        {
            @Override
            public void paintControl(PaintEvent e)
            {
                Point size = c.getSize();
                e.gc.drawText("Width: " + size.x + "\nHeight: " + size.y, 5, 5);
                e.gc.drawOval(2, 2, size.x - 4, size.y - 4);
            }
        });

        return content;
    }

    public static void main(String[] args)
    {
        Display d = new Display();
        Shell s = new Shell();

        FMLDialog fml = new FMLDialog(s);
        fml.open();
    }
}

关于java - JFace 对话框布局问题,在窗口边界之外绘制的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42119220/

相关文章:

jquery - 我可以将 HTML DOM 对象移动到浏览器对话框以实现 "detach"功能吗

java - 删除在 SWT 显示上创建的使用 GC 制作的绘图

java - Spring Boot Kafka 客户端是否有 "Circuit Breaker"?

java - 使用子类化替换未实现接口(interface)的 Java 类

java - 如何访问 arraylist 中的列表元素

Android:从列表中嵌入的按钮调用对话框

jQuery Ui 对话框按钮,如何添加类?

Java SWT 中断异常

java - 即时更改 Eclipse 标题中按钮上的文本

java - Hibernate 不连接 MySql