简体   繁体   中英

Java's SWT: How to properly set GUI components?

I'm trying to create a gui for chess game and could use some help.
I set up two panels: left panel, and right panel (both seats on a main panel). the left panel will contain the chess board itself, and the right panel will contain some more widgets.
The problem is, I just can get the right panel to be positioned at the right side of the window.

So to give you first some more background, here's how I want it to look:
在此处输入图片说明

and here's how it looks:
在此处输入图片说明

I tried to set the main panel (the one that holds both the left and the right panels) with FormLayout() object and then to set:

FormData form_data=new FormData();
form_data.left=new FormAttachment(left_panel);
right_panel.setLayoutData(form_data); 

but that didn't do anything.

Here's the relevant code:

    shell=new Shell(display,SWT.DIALOG_TRIM);
    shell.setText("Chess");

    /*********************
     * setting main panel
     *********************/
    Composite main_panel=new Composite(shell, SWT.NONE);
    main_panel.setBackgroundImage(background); //size of this image is 800x650, which means it should encompass the left and right panels exactly
    main_panel.setBackgroundMode(SWT.INHERIT_DEFAULT);
    main_panel.setBounds(background.getBounds());
    main_panel.setLayout(new GridLayout(2,false);


    /*********************
     * setting left panel
     *********************/
    left_panel=new Composite(main_panel, SWT.NONE); 
    GridData data=new GridData(SWT.FILL, SWT.FILL, false, true);
data.widthHint = 650;
    left_panel.setLayoutData(data);

    board_panel=new Composite(left_panel,SWT.NONE);
    board_panel.setBackgroundImage(game_board); //size of this image is 520x520
    board_panel.setLocation(65, 65); //board size is 520x520, so this centralizes it in left panel


    /*********************
     * setting right panel
     *********************/
    right_panel=new Composite(main_panel, SWT.NONE);
    right_panel.setBackgroundImage(panel); //size of this image is 150x650
    right_panel.setLayoutData(new GridData(SWT.END, SWT.FILL, true, true));


    shell.pack();

BTW, what style should I set the Composite to, if I don't anything special in it?
(as you can see I set it to SWT.NO_RADIO_GROUP because I didn't know what to put there...)

Any help would greatly appreciated.

**the code was edited

Never ever use setBounds unless absolutely necessary. Use a Layout instead.

Here is an excellent tutorial about layouts.


Here is some example code that should give you a starting point:

public static void main(String[] args)
{
    final Display display = new Display();

    Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(2, false));

    Composite chessBoard = new Composite(shell, SWT.BORDER);
    chessBoard.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Composite settings = new Composite(shell, SWT.BORDER);
    GridData data = new GridData(SWT.END, SWT.FILL, false, true);
    data.widthHint = 100;
    settings.setLayoutData(data);

    shell.pack();
    shell.open();
    shell.setSize(400, 200);
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
}

Looks like this:

在此处输入图片说明

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