我正在开发一个基于 eclipase 的插件,我在其中创建了一个应用程序(使用 SWT)。有两类: RunAction.java
由 run()
、 dispose()
和 init()
方法和 组成Sample.java
由带有 Label 小部件的示例应用程序组成。现在,当我通过将应用程序作为 Eclipse 应用程序运行来测试该应用程序时,仅显示 shell,而没有标签小部件。 问题是什么?我正在共享代码。
RunAction.java
public class RunWizardAction extends Action implements IWorkbenchWindowActionDelegate {
/** Called when the action is created. */
Sample samp=new Sample();
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
public void init(IWorkbenchWindow window) {
}
/** Called when the action is discarded. */
public void dispose() {
}
/** Called when the action is executed. */
public void run(IAction action) {
//InvokatronWizard wizard= new InvokatronWizard();
new Thread(new Runnable(){
@Override
public void run() {
samp.sampleApp(shell);
}
}).start();
}
}
<小时/>
Sample.java (The sample function)
public void sampleApp(Shell shell) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Hello");
JLabel lab=new JLabel("Hello World");
Label username_checkout=new Label(shell, SWT.BOLD);
username_checkout.setText("User Name");
Button button=new Button(shell,SWT.PUSH);
button.setText("push");
shell.open();
shell.setSize(270,270);
while (!shell1.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
最佳答案
您的 Shell
没有布局。这段代码对我有用:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
/* SET LAYOUT */
shell.setLayout(new FillLayout());
shell.setText("Hello");
JLabel lab = new JLabel("Hello World");
Label username_checkout = new Label(shell, SWT.BOLD);
username_checkout.setText("User Name");
Button button = new Button(shell, SWT.PUSH);
button.setText("push");
shell.open();
shell.pack();
shell.setSize(270, 270);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
}
<小时/>
此外,您的代码中有太多Shell
:
public void sampleApp(Shell shell) {
Display display = new Display();
Shell shell = new Shell(display);
所以,现在你有两个名为 shell
的 Shell
(顺便说一句,它不会编译)...
while (!shell1.isDisposed())
还有第三个。这是怎么回事?
关于java - 启动 Eclipse RCP 应用程序时小部件不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15809786/