[英]Java's SWT: How to properly set GUI components?
我正在嘗試為國際象棋游戲創建GUI,可以使用一些幫助。
我設置了兩個面板:左面板和右面板(兩個都位於主面板上)。 左側面板將包含棋盤本身,右側面板將包含更多小部件。
問題是,我只需要將正確的面板放在窗口的右側即可。
因此,首先給您一些背景知識,這就是我希望它看起來的樣子:
外觀如下:
我嘗試使用FormLayout()對象設置主面板(同時包含左右面板的面板),然后進行設置:
FormData form_data=new FormData();
form_data.left=new FormAttachment(left_panel);
right_panel.setLayoutData(form_data);
但這什么也沒做。
以下是相關代碼:
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();
順便說一句,如果我沒有什么特別的建議,應該將Composite設置為哪種樣式?
(如您所見,我將其設置為SWT.NO_RADIO_GROUP,因為我不知道該放在那里...)
任何幫助將不勝感激。
**代碼已編輯
除非絕對必要,否則切勿使用setBounds
。 請改用Layout
。
這是有關布局的出色教程。
這是一些示例代碼,應該為您提供一個起點:
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();
}
}
看起來像這樣:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.