简体   繁体   English

Java的SWT:如何正确设置GUI组件?

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

I'm trying to create a gui for chess game and could use some help. 我正在尝试为国际象棋游戏创建GUI,可以使用一些帮助。
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: 我尝试使用FormLayout()对象设置主面板(同时包含左右面板的面板),然后进行设置:

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? 顺便说一句,如果我没有什么特别的建议,应该将Composite设置为哪种样式?
(as you can see I set it to SWT.NO_RADIO_GROUP because I didn't know what to put there...) (如您所见,我将其设置为SWT.NO_RADIO_GROUP,因为我不知道该放在那里...)

Any help would greatly appreciated. 任何帮助将不胜感激。

**the code was edited **代码已编辑

Never ever use setBounds unless absolutely necessary. 除非绝对必要,否则切勿使用setBounds Use a Layout instead. 请改用Layout

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: 看起来像这样:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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