启动Eclipse RCP应用程序时,窗口小部件不会显示

时间:2013-04-04 11:24:29

标签: java eclipse eclipse-plugin swt eclipse-rcp

我正在开发一个基于eclipase的插件,我在其中创建了一个应用程序(使用SWT)。有两个类别; RunAction.javarun()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(示例函数)

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

    }

2 个答案:

答案 0 :(得分: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个太多:

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

所以,现在你有两个名为Shell的{​​{1}}(btw将无法编译)......

shell

还有第三个。怎么了?

答案 1 :(得分: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();
}