窗口内部区域的大小

窗口内部区域的大小

本文介绍了设置 Java SWT shell 窗口内部区域的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java SWT shell 窗口中,如何设置其内部大小而不是整个窗口框架大小?

In a Java SWT shell window, how do I set its inner size than its whole window frame size?

例如,如果我使用 shell.setSize(300, 250) 这将使整个窗口显示为 300x250.这个 300x250 包括窗框的大小.

For instance, if I use shell.setSize(300, 250) this would make the whole window appearing as exactly 300x250. This 300x250 includes the size of the window frame.

如何设置内部尺寸,即shell窗口的内容显示区域为300x250?那就是这个 300x250 不包括窗框的宽度.

How can I set the inner size, that is the content display region of the shell window to 300x250 instead? That's this 300x250 excludes the width of the window frame.

我试图减去一些偏移值,但问题是不同的操作系统有不同的窗口框架大小.所以有一个恒定的偏移量是不准确的.

I tried to minus some offset values but the thing is different Operating Systems have different window frame sizes. So having a constant offset would not be accurate.

谢谢.

推荐答案

从你的问题我了解到你想要设置客户区的尺寸.在 SWT 术语中,它被定义为 一个矩形,它描述了能够显示数据的接收器区域(即不被修剪"覆盖).

From your question what I understood is that you want to set the dimension of the Client Area. And in SWT lingo it is defined as a rectangle which describes the area of the receiver which is capable of displaying data (that is, not covered by the "trimmings").

Client Area 的维度不能直接设置,因为没有API.虽然你可以通过一点点黑客来实现这一点.在下面的示例代码中,我希望我的客户区为 300 x 250.为了实现这一点,我使用了 shell.addShellListener() 事件侦听器.当 shell 完全处于活动状态时(请参阅 public void shellActivated(ShellEvent e))然后我计算不同的边距并再次设置我的 shell 的大小.外壳尺寸的计算和重置为我提供了所需的外壳尺寸.

You cannot directly set the dimension of Client Area because there is no API for it. Although you can achieve this by a little hack. In the below sample code I want my client area to be 300 by 250. To achieve this I have used the shell.addShellListener() event listener. When the shell is completely active (see the public void shellActivated(ShellEvent e)) then I calculate the different margins and again set the size of my shell. The calculation and resetting of the shell size gives me the desired shell size.

>>代码:

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 ();
    }
}

这篇关于设置 Java SWT shell 窗口内部区域的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:45