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

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

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

如何将内部大小(即shell窗口的内容显示区域)设置为300×250? 这是300×250,不包括窗框的宽度。

我试图减去一些偏移值,但事情是不同的操作系统有不同的窗口框架大小。 因此,具有恒定的偏移将是不准确的。

谢谢。

根据您的问题,我理解的是您要设置Client Area的维度。 在SWT术语中,它被定义为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 by 250 。 为了实现这一点,我使用了shell.addShellListener()事件监听器。 当shell完全处于活动状态时(参见public void shellActivated(ShellEvent e) )然后我计算不同的边距并再次设置我的shell的大小。 计算和重置shell尺寸可以得到所需的shell尺寸。

>>Code:

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

如果我找到你,你应该将内部组件的大小设置为所需的大小,并使用方法pack() (框架)。

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