Setting size of inner region of Java SWT shell window

后端 未结 3 1650
无人共我
无人共我 2021-01-21 12:56

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

For instance, if I use shell.setSize(300, 250) this would make the whole window

相关标签:
3条回答
  • 2021-01-21 13:15
    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();
        }
    }
    
    0 讨论(0)
  • 2021-01-21 13:17

    If I get you right you should set the size of the inner component to the needed size and use the method pack() (of the frame).

    0 讨论(0)
  • 2021-01-21 13:18

    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").

    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.

    >>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 ();
        }
    } 
    
    0 讨论(0)
提交回复
热议问题