如何制作在所有窗口分辨率下显示相似行为(布局)的 SWT/JFace 对话框

Posted

技术标签:

【中文标题】如何制作在所有窗口分辨率下显示相似行为(布局)的 SWT/JFace 对话框【英文标题】:How to make SWT/JFace dialogs which show similar behaviour(layouts) under all window resolutions 【发布时间】:2012-09-26 07:29:29 【问题描述】:

我创建的 JFace 对话框在不同分辨率下显示布局差异。如何在 JFace 或 SWT 中创建在所有分辨率下显示相同布局的对话框,就像在 Eclipse 中使用的对话框一样。

我的意思是,在将屏幕分辨率更改为 800x600 像素并选择超大字体时,对话框的布局会受到干扰,而 Eclipse IDE 对话框并非如此。请查看我的对话框的屏幕截图。

【问题讨论】:

能否详细说明您的问题?也许添加屏幕截图?我不太明白你在问什么。 好的,它在 Eclipse 中的外观如何?您是否尝试过查看 SWT 源代码以获得答案? 是的,我已尽我所能进行管理,在像 1024x768 这样的正常分辨率的情况下它看起来很正常,但如果分辨率发生变化,布局会受到干扰......你可以注意到截图。 Eclipse IDE 对话框不是这样的”:你说它们看起来并没有受到干扰。 没错.....如果分辨率发生变化,Eclipse 的对话框不会受到干扰 【参考方案1】:

好的,根据聊天中的讨论,之前处理此代码的人似乎限制了各个小部件的大小。我创建了一个类似于您问题中的虚拟对话框。它不限制小部件的大小。相反,布局负责调整大小:

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class DummyDialog extends Dialog 

    private Composite composite;

    public DummyDialog(Shell parentShell)
    
        super(parentShell);
        setShellStyle(parentShell.getStyle() | SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
        setBlockOnOpen(true);
    

    protected Control createDialogArea(Composite parent) 
        this.composite = (Composite) super.createDialogArea(parent);

        GridLayout layout = new GridLayout(1, false);
        layout.marginHeight = 5;
        layout.marginWidth = 10;

        composite.setLayout(layout);

        createContent();

        return composite;
    

    private void createContent()
    
        createTopContent();

        createMiddleContent();

        createBottomContent();
    

    private void createTopContent()
    
        Composite top = new Composite(composite, SWT.NONE);

        top.setLayout(new GridLayout(2, false));
        top.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        Label firstLabel = new Label(top, SWT.NONE);
        firstLabel.setText("X-Ref Library");
        Text firstText = new Text(top, SWT.BORDER);
        firstText.setText("MANISH2XA");
        firstText.setEditable(false);
        firstText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));

        Label secondLabel = new Label(top, SWT.NONE);
        secondLabel.setText("Text");
        Text secondText = new Text(top, SWT.BORDER);
        secondText.setText("Test Lib for Manish");
        secondText.setEditable(false);
        secondText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
    

    private void createMiddleContent()
    
        Composite middle = new Composite(composite, SWT.NONE);
        middle.setLayout(new GridLayout(3, false));
        middle.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Group leftGroup = new Group(middle, SWT.NONE);
        leftGroup.setText("Object Library(s)");
        leftGroup.setLayout(new GridLayout(1, false));
        leftGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        List leftList = new List(leftGroup, SWT.BORDER);
        leftList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        leftList.add("DUMMY");

        Composite buttons = new Composite(middle, SWT.NONE);
        buttons.setLayout(new GridLayout(1, false));

        Button moveUp = new Button(buttons, SWT.PUSH);
        moveUp.setText("Move up");
        moveUp.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
        Button moveDown = new Button(buttons, SWT.PUSH);
        moveDown.setText("Move down");
        moveDown.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
        Button modify = new Button(buttons, SWT.PUSH);
        modify.setText("Modify");
        modify.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
        Button remove = new Button(buttons, SWT.PUSH);
        remove.setText("Remove");
        remove.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));

        Group rightGroup = new Group(middle, SWT.NONE);
        rightGroup.setText("Source Library(s)");
        rightGroup.setLayout(new GridLayout(1, false));
        rightGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        List rightList = new List(rightGroup, SWT.BORDER);
        rightList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        rightList.add("DUMMY");
    

    private void createBottomContent()
    
        Composite bottom = new Composite(composite, SWT.NONE);
        bottom.setLayout(new GridLayout(3, false));
        bottom.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        Label leftLabel = new Label(bottom, SWT.NONE);
        leftLabel.setText("Library");

        Label rightLabel = new Label(bottom, SWT.NONE);
        rightLabel.setText("Type");

        new Label(bottom, SWT.NONE);

        Text leftText = new Text(bottom, SWT.BORDER);
        leftText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));

        Combo combo = new Combo(bottom, SWT.NONE);
        combo.add("Object Library");
        combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));

        Button add = new Button(bottom, SWT.PUSH);
        add.setText("Add");
        add.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
    

    protected void configureShell(Shell newShell)
    
        super.configureShell(newShell);
        newShell.setText("Application Library List");
    

    @Override
    protected void createButtonsForButtonBar(Composite parent) 
        super.createButtonsForButtonBar(parent);

        Button ok = getButton(IDialogConstants.OK_ID);
        ok.setText("Apply Changes");
        setButtonLayoutData(ok);

        Button cancel = getButton(IDialogConstants.CANCEL_ID);
        cancel.setText("Cancel");
        setButtonLayoutData(cancel);
    

    public void okPressed()
    
        this.close();
    

    public static void main(String[] args)
    
        new DummyDialog(new Shell()).open();
    

这是它的外观:

【讨论】:

我在 Windows 上以不同的分辨率和字体大小执行了你的演示......这次布局非常好......如果我们不限制小部件的大小,那就对了依靠布局本身来处理大小,不会出现这个分辨率问题....

以上是关于如何制作在所有窗口分辨率下显示相似行为(布局)的 SWT/JFace 对话框的主要内容,如果未能解决你的问题,请参考以下文章

第十一周课程总结

WPF 不同DPI下,窗口大小的处理

wxpython 布局管理

用于固定组件位置和可滚动窗口的 Java GUI 布局

布局管理

XAML 调整窗口大小和窗口内的所有元素