简体   繁体   中英

Some questions about WindowBuilder SWT java gui building

I'm trying to learn WindowBuilder SWT but I'm having problems finding good samples or good tutorials with real explanations about how to do correct GUIs, for example with all the components centered in the middle of the shell.

I can find some super simple tutorials where you can see only the components and the parts of windowbuilder and a hello world alike project.

I only found that you can use a gridlayout and make a label to fill all the width and height and then it's centered, but that is not the behaviour I'm trying to achieve.

I want that group of components centered on the screen without filling the screen. I mean that the component must not become bigger when the user maximices the screen.

In Android this is very easy, you only need to put a Layout inside a Layout, and in the parent layout you must simply center it's content. But here I can't find the way to do it. Even I can't find the way to put a layout inside a layout. So I can't understand how to do cool GUIs. These are my questions:

  1. How can i center a group of components (for example, a login panel) in the center of the shell even when the shell is maximized?

  2. It is possible to put layouts inside layouts? if not, then, how can I do complex and nice GUIs?

  3. Is there a repository anywhere where I can download sample codes with complex or nice GUIs done with WindowBuilder SWT?

This is a simple SWT app that shows centered controls in a shell. It an inner composite centered in shell.

  public static void main(final String [] args)
  {
    Display.setAppName("Stackoverflow");

    Display display = new Display();

    Shell shell = new Shell(display, SWT.SHELL_TRIM);

    shell.setText("Stack Overflow");

    shell.setSize(1000, 800);

    shell.setLayout(GridLayoutFactory.swtDefaults().create());

    Composite inner = new Composite(shell, SWT.BORDER);
    inner.setLayoutData(GridDataFactory.swtDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, true).create());

    inner.setLayout(GridLayoutFactory.swtDefaults().create());

    Text text = new Text(inner, SWT.LEAD);
    text.setLayoutData(GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).hint(200, SWT.DEFAULT).create());

    Button button = new Button(inner, SWT.PUSH);
    button.setText("Login");
    button.setLayoutData(GridDataFactory.swtDefaults().align(SWT.CENTER, SWT.CENTER).create());

    shell.open();

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

    display.dispose();
  }

I have used GridDataFactory and GridLayoutFactory rather that GridLayout and GridData because I think they are slightly easier to understand.

Windowbuilder is a very powerful tool but when you are new to GUI design you should take your time to read the manual. Please google for "WindowBuilder User Guide" and you will find the manual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM