How can we auto resize the size of components in SWT?

后端 未结 1 661
再見小時候
再見小時候 2020-12-09 20:21

In my SWT application i have certain components inside the SWT shell.

Now how can i auto re-size this components according to the size of display window.

         


        
相关标签:
1条回答
  • 2020-12-09 20:49

    That sounds suspiciously like you aren't using layouts.

    The whole concept of layouts makes worrying about resizing needless. The layout will take care of the size of all of its components.

    I would recommend to read the Eclipse article about layouts

    Your code can easily be corrected. Don't set the size of individual components, the layout will determine their size. If you want the window to have a predefined size, set the shell's size:

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));
    
        Group outerGroup = new Group(shell, SWT.NONE);
    
        // Tell the group to stretch in all directions
        outerGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        outerGroup.setLayout(new GridLayout(2, true));
        outerGroup.setText("Group");
    
        Button left = new Button(outerGroup, SWT.PUSH);
        left.setText("Left");
    
        // Tell the button to stretch in all directions
        left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    
        Button right = new Button(outerGroup, SWT.PUSH);
        right.setText("Right");
    
        // Tell the button to stretch in all directions
        right.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    
        shell.setSize(1000,400);
        shell.open();
    
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
    

    Before resizing:

    Before resizing

    After resizing:

    After resizing

    0 讨论(0)
提交回复
热议问题