2013-10-04 24 views
1

今天我想用Eclipse創建一個簡單的Java SWT GUI應用程序,但爲了更清晰起見,我希望每個子窗口都在不同的類中。由於我對Java編程非常陌生,有沒有辦法讓一個不同的類通過調用一個方法來完成它的工作?我到處在互聯網上,但無法找到我一直在尋找...Java SWT - 從其他類創建一個新窗口

這裏是我到目前爲止

Button foo = new Button(shell, SWT.PUSH); 
foo.setText("Edit"); 
foo.addListener(SWT.Selection, new Listener() { 
    public void handleEvent(Event e) { 
     switch (e.type) { 
      case SWT.Selection: 
       // Call the other Class file here 
       break; 
     } 
    } 
}); 

回答

6

是。有可能的。我不會稱之爲「調用一個類」,而是用SWT術語「打開另一個窗口」。

您只需將Shell包裝在您的其他課程中,然後從「外部」調用open() API。

如果你想編輯東西,你甚至可以create wizards


有很多方法可以做你想做的事情,我只是選擇了一個簡單的版本。但這是而不是做到這一點的唯一方法。等待Baz回答,他會再來一個很酷的例子。 ;)

我建議你閱讀Shell的javadoc。

例子:

ShellTest.class(如Java應用程序運行此)

/** 
* 
* @author ggrec 
* 
*/ 
public class ShellTest 
{ 

    // ==================== 2. Instance Fields ============================ 

    private AnotherShell anotherShell; 


    // ==================== 3. Static Methods ============================= 

    public static void main(final String[] args) 
    { 
     new ShellTest(); 
    } 


    // ==================== 4. Constructors =============================== 

    private ShellTest() 
    { 
     final Display display = new Display(); 
     final Shell shell = new Shell(display); 
     shell.setLayout(new GridLayout(1, false)); 

     anotherShell = new AnotherShell(); 

     createContents(shell); 

     shell.pack(); 
     shell.open(); 
     while (!shell.isDisposed()) 
     { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 
     display.dispose(); 
    } 


    // ==================== 5. Creators =================================== 

    private void createContents(final Composite parent) 
    { 
     final Button buttonOpen = new Button(parent, SWT.PUSH); 
     buttonOpen.setText("Open"); 

     buttonOpen.addSelectionListener(new SelectionAdapter() 
     { 
      @Override public void widgetSelected(final SelectionEvent e) 
      { 
       anotherShell.open(); 
      } 
     }); 

     final Button buttonClose = new Button(parent, SWT.PUSH); 
     buttonClose.setText("Close"); 

     buttonClose.addSelectionListener(new SelectionAdapter() 
     { 
      @Override public void widgetSelected(final SelectionEvent e) 
      { 
       anotherShell.close(); 
      } 
     }); 
    } 
} 

AnotherShell.class(這將是你的 「其他類」)

/** 
* 
* @author ggrec 
* 
*/ 
public class AnotherShell 
{ 

    // ==================== 2. Instance Fields ============================ 

    private Shell shell; 


    // ==================== 4. Constructors =============================== 

    public AnotherShell() 
    { 
     shell = new Shell(Display.getCurrent()); 
    } 


    // ==================== 6. Action Methods ============================= 

    public void open() 
    { 
     shell.open(); 
    } 

    public void close() 
    { 
       // Don't call shell.close(), because then 
       // you'll have to re-create it 
     shell.setVisible(false); 
    } 
} 
+0

請舉例? Thx提前 – forumfresser

+0

@forumfresser新增示例。祝你好運。 – GGrec

+0

非常感謝。正確的答案,不幸的是我不能upvote到目前爲止。 – forumfresser