0
我試圖在shell元素下添加一個複合元素,現在沒有顯示按鈕小部件。我錯過了什麼嗎?試圖創建基本的JFace應用程序,不知道什麼問題
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class RadioButtonDemo {
/**
* Create radio buttons
*
* @param parent
* Parent widget
* @param labels
* Array of labels for the radio buttons
* @param defaultSelection
* The default button to select
* @return The newly created buttons complete with labels
*/
public final Button[] getRadioButtons(final Composite parent,
final String[] labels, int defaultSelection) {
// some sanity stuff
assert (defaultSelection < labels.length);
assert (!parent.equals(null));
final Button[] buttons = new Button[labels.length];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new Button(parent, SWT.RADIO);
buttons[i].setText(labels[i]);
buttons[i].setSelection(defaultSelection == i);
}
return buttons;
}
public void showDemo() {
// some setup
final Display display = new Display();
Shell shell = new Shell(display, SWT.DIALOG_TRIM);
// shell.setLayout(new RowLayout());
shell.setSize(new Point(200, 200)); // make it small for the demo
shell.setText("Radio button demo");
shell.setLayout(new FillLayout());
Composite c = new Composite(shell, SWT.NONE);
final String[] labels = new String[] {
"Never delete code coverage launches from history",
"Delete oldest code coverage launches from history" };
final Button[] radioButtons = this.getRadioButtons(c, labels, 0);
shell.pack(); // min-size...
shell.open(); // open shell
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose(); // cleanup
}
public static void main(String[] args) {
// new instance
RadioButtonDemo instance = new RadioButtonDemo();
instance.showDemo();
}
}
玩了之後,我幾乎同時發現了這個問題,並且發佈了你的答案。但是,我不明白*爲什麼這是必要的。 – BlackSheep 2013-02-11 15:47:57
@BlackSheep 1.佈局告訴我們應該如何放置並調整組合的直接子元素,而不是這些子元素的子元素,或者_their_子元素等等。2.「Composite」默認沒有_any_佈局,所以你需要用'setPosition'和'setSize'手動放置任何孩子。這可能不是最好的想法,但現在改變它可能會破壞現有的代碼。所以你的代碼說「'c'應該填充整個'shell',但我會自己放置按鈕。 – 2013-02-11 16:22:26