[英]SWT window don't show components until maximizing
我正在學習SWT,但遇到了一些問題。 我有一個菜單,其中有兩個元素“文件”和“幫助”。 在文件中有兩個項目“打開文件”和“退出”
加載文件后(使用“打開文件”),它應該打印一個Combo和一個Label,但是除非我調整窗口大小(或最大化),否則它不會顯示。 我做錯了什么?
打開顯示
public Shell open (Display display) {
this.display = display;
initResources();
this.shell = new Shell(display);
createShellContents();
shell.open();
return shell;
}
選單
public void createShellContents() {
shell.setText(getResourceString("window.title"));
shell.setImage(appIcon);
shell.setLayout(new FillLayout());
Menu bar = new Menu(shell, SWT.BAR);
shell.setMenuBar(bar);
createFileMenu(bar);
createHelpMenu(bar);
}
打開並加載文件后(可以正常工作),我嘗試同時創建Combo和Label
private void createLayout () {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
gridLayout.marginHeight = gridLayout.marginWidth = 0;
shell.setLayout(gridLayout);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.widthHint = 185;
createComboView(shell, gridData);
Label numObjectsLabel = new Label(shell, SWT.BORDER);
gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
gridData.widthHint = 185;
numObjectsLabel.setLayoutData(gridData);
numObjectsLabel.setText("Sample");
}
private void createComboView(Composite parent, Object layoutData) {
Label labelGame = new Label(parent, 0);
labelGame.setText("Game: ");
combo = new Combo(parent, SWT.NONE);
combo.setLayoutData(layoutData);
String[] gamesStr = null;
if (games != null && !games.isEmpty()) {
System.out.println("Games has " + games.size());
gamesStr = new String[games.size()];
for (int i = 0; i < games.size(); i++) {
gamesStr[i]= "GAME " + String.valueOf(games.get(i).getId());
}
combo.setItems(gamesStr);
}
else {
System.out.println("NO GAMES");
}
combo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int k = combo.getSelectionIndex();
System.out.println("Selected" + k);
}
});
}
這是它的外觀(在加載文件並最大化或調整窗口大小之后):
此外,如果您發現任何錯誤,請告訴我。 這是我的第一個GUI程序,我不了解布局如何工作以及如何顯示元素。 我想要一行(組合在哪里)顯示一些信息,一旦選擇了一個項目,請繪制窗口,不確定我應該使用哪個布局。 我可能會問另一個問題。
PD:看起來圖像無法加載(不知道為什么),但是總是失敗。 這是鏈接: http : //s28.postimg.org/4v7k34dzx/Analyzer.png
如果在打開外殼后在外殼上添加或更改控件,則需要重做布局並重新計算外殼大小。 就像是:
shell.layout(true, true);
shell.pack();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.