设置Java SWT Shell窗口内部区域的大小

3
在Java SWT shell窗口中,如何设置其内部大小而不是整个窗口框架大小?
例如,如果使用shell.setSize(300,250),这将使整个窗口显示为完全300x250。这300x250包括窗口框架的大小。
我如何将内部大小设置为300x250,即窗口shell的内容显示区域?也就是说,这300x250不包括窗口框架的宽度。
我尝试减去一些偏移值,但问题在于不同的操作系统具有不同的窗口框架大小。因此,具有恒定偏移量将不准确。
谢谢。
4个回答

7
从您的问题中,我理解到您想设置“客户区”的尺寸。在 SWT 术语中,“客户区”被定义为“描述接收器能够显示数据的区域的矩形(即没有被“修饰”覆盖的区域)”。
由于没有相应的 API,您无法直接设置“客户区”的尺寸。但是,您可以通过一些技巧来实现。在下面的示例代码中,我希望我的“客户区”大小为“300 x 250”。为了实现这一点,我使用了“shell.addShellListener()”事件监听器。当 shell 完全激活时(参见“public void shellActivated(ShellEvent e)”),我计算不同的边距并重新设置 shell 的大小。计算和重新设置 shell 的大小使我得到了所需的 shell 大小。 >>代码:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;

public class MenuTest {

    public static void main (String [] args) 
    {
        Display display = new Display ();
        final Shell shell = new Shell (display);

        GridLayout layout = new GridLayout();
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        layout.horizontalSpacing = 0;
        layout.verticalSpacing = 0;
        layout.numColumns = 1;
        shell.setLayout(layout);
        shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true));
        final Menu bar = new Menu (shell, SWT.BAR);
        shell.setMenuBar (bar);

        shell.addShellListener(new ShellListener() {

            public void shellIconified(ShellEvent e) {
            }
            public void shellDeiconified(ShellEvent e) {
            }
            public void shellDeactivated(ShellEvent e) {
            }
            public void shellClosed(ShellEvent e) {
                System.out.println("Client Area: " + shell.getClientArea());
            }
            public void shellActivated(ShellEvent e) {
                int frameX = shell.getSize().x - shell.getClientArea().width;
                int frameY = shell.getSize().y - shell.getClientArea().height;
                shell.setSize(300 + frameX, 250 + frameY);
            }
        });     

        shell.open ();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    }
} 

感谢您的帮助。您的示例非常有帮助!谢谢!! - Carven
不需要任何黑客技巧。computeTrim方法返回显示给定客户端区域所需的边界。 - Rüdiger Herrmann

0

如果我理解正确,您应该将内部组件的大小设置为所需的大小,并使用框架的pack()方法。


我在框架的顶部有一个菜单控件。pack()方法不考虑菜单栏的额外高度,而只考虑框架中的内容。我该如何获取菜单栏的高度或让pack()方法同时考虑菜单栏的大小? - Carven

0
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

public class SWTClientAreaTest
{
    Display display;
    Shell shell;
    final int DESIRED_CLIENT_AREA_WIDTH = 300;
    final int DESIRED_CLIENT_AREA_HEIGHT = 200;

    void render()
    {
        display = Display.getDefault();
        shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER);

        Point shell_size = shell.getSize();
        Rectangle client_area = shell.getClientArea();

        shell.setSize
        (
            DESIRED_CLIENT_AREA_WIDTH + shell_size.x - client_area.width,
            DESIRED_CLIENT_AREA_HEIGHT + shell_size.y - client_area.height
        );

        shell.open();

        while (!shell.isDisposed()) 
        {
            if (!display.readAndDispatch()) 
            {
                display.sleep();
            }
        }

        display.dispose();
    }

    public static void main(String[] args)
    {
        SWTClientAreaTest appl = new SWTClientAreaTest();
        appl.render();
    }
}

3
请添加一些评论来描述答案并回答特定的问题。在StackOverflow上,纯代码答案是不受欢迎的。 - clearlight

-1

使用computeTrim计算必要的边界以显示给定的客户端区域。该方法返回一个矩形,描述了为指定参数提供客户端区域所需的边界。

在此示例中,将设置外壳的大小,以便能够显示100 x 200(宽度x高度)的客户端区域:

Rectangle bounds = shell.computeTrim(0, 0, 100, 200);
shell.setSize(bounds.width, bounds.height);

本文介绍了 SWT 中用于小部件尺寸的术语: https://www.eclipse.org/articles/Article-Understanding-Layouts/Understanding-Layouts.htm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接