0

我正在开发一个基于 eclipase 的插件,我在其中创建了一个应用程序(使用 SWT)。有两个类,即;RunAction.java它由run()dispose()init()方法组成,并且Sample.java由带有标签小部件的示例应用程序组成。现在,当我通过将应用程序作为 Eclipse 应用程序运行来测试应用程序时,只会显示没有标签小部件的外壳。问题是什么?我正在分享代码。

运行动作.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(示例函数)

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();
          }
        }

    }
4

2 回答 2

3

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此外,您的代码中的 s太多:

public void sampleApp(Shell shell) {
    Display display = new Display();
    Shell shell = new Shell(display);

所以,现在你有两个Shells 被调用shell(顺便说一句不会编译)......

while (!shell1.isDisposed())

还有第三个。那是怎么回事?

于 2013-04-04T12:17:20.073 回答
3

如果您像 Baz 的回答一样在 Eclipse 之外运行,那么您的示例应用程序会很好。

由于您在 Eclipse 中运行,因此您已经拥有了一个 Display 和一个 Shell。使用它们。

public void sampleApp(Shell shell) { 
    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.setSize(270,270);
    shell.open();
} 
于 2013-04-04T12:23:11.357 回答