我正在尝试实现一个非常基本的目标,该目标在.net 平台中曾经非常简单:创建一个可重用的组件并以另一种形式使用它。我尝试执行以下操作:
package ***.composites;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
public class CompTest extends Composite {
/**
* Create the composite.
* @param parent
* @param style
*/
public CompTest(Composite parent, int style) {
super(parent, style);
Composite composite = new Composite(this, SWT.NONE);
composite.setBounds(10, 10, 273, 261);
Button btnCheckButton = new Button(composite, SWT.CHECK);
btnCheckButton.setBounds(82, 112, 93, 16);
btnCheckButton.setText("Check Button");
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
和
package ***.composites;
import org.eclipse.swt.widgets.Display;
public class WindTest {
protected Shell shell;
private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
WindTest window = new WindTest();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 377);
shell.setText("SWT Application");
Composite composite = formToolkit.createComposite(shell, SWT.NONE);
composite.setBounds(10, 10, 173, 105);
formToolkit.paintBordersFor(composite);
}
}
如何在第二个复合类中添加第一个复合类?有没有办法在设计模式下做到这一点?我在做正确的事吗?