2012-11-13 2 views
0

Java 7을 사용하여 Windows 7 Home에서 SWT를 사용하여 파일을 열려면 FileDialog를 열어야합니다. BrowseBtn1 Button (SWT)을 누르면이 FileDialog가 열려 있어야합니다. 이 addSelectionListener 사용하고 있습니다. 이것이 올바른 접근 방법입니까? 그렇다면 왜 런타임 오류를 만나지 않을 때 내 대화 상자가 열리지 않습니다. 나는 AWT를 전혀 사용하지 않고 SWT 만 사용하고있다.SWT FileDialog 열기 문제

import org.eclipse.swt.*; 
import org.eclipse.swt.widgets.*; 
import org.eclipse.swt.widgets.FileDialog; 
import org.eclipse.swt.events.*; 
import org.eclipse.swt.graphics.FontData; 
import org.eclipse.swt.layout.*; 

public class MySwtApp { 
    public static Display display; 
    public static Shell shell; 

    public static void main(String[] args) { 

     display = new Display(); 
     shell = new Shell (display); 
     shell.setText("MY TITLE"); 
     GridLayout gridLayout = new GridLayout (3, false); 
     shell.setLayout (gridLayout); 

     Label Label1 = new Label (shell, SWT.NONE); 
     Label1.setText ("Select Message "); 
     GridData data = new GridData (200, SWT.DEFAULT); 
     Label1.setLayoutData (data); 

     Combo Combo1 = new Combo (shell, SWT.NONE); 
     Combo1.setItems (new String [] {"Option A", 
       "Option B"}); 
     // CaseStudyCombo.setText ("CaseStudyCombo"); 
     data = new GridData (200, SWT.DEFAULT); 
     Combo1.setLayoutData (data); 
     Combo1.addListener (SWT.DefaultSelection, new Listener() { 
      public void handleEvent (Event e) { 
       System.out.println (e.widget + " - Default Selection"); 
      } 
     }); 

     Label emptyLabel = new Label (shell, SWT.NONE); 
     emptyLabel.setText (""); 
     data = new GridData (50, SWT.DEFAULT); 
     emptyLabel.setLayoutData (data); 

     Label Label2 = new Label (shell, SWT.NONE); 
     Label2.setText ("Next Message: "); 
     data = new GridData (200, SWT.DEFAULT); 
     Label2.setLayoutData (data); 

     final Text text4 = new Text (shell, SWT.BORDER); 
     text4.setText (""); 
     data = new GridData (215, SWT.DEFAULT); 
     text4.setLayoutData (data); 

     Button BrowseBtn1 = new Button (shell, SWT.PUSH); 
     BrowseBtn1.setText ("Browse"); 
     data = new GridData (80, SWT.DEFAULT); 
     BrowseBtn1.setLayoutData (data); 
     BrowseBtn1.addSelectionListener(new SelectionAdapter() { 
      String result = ""; 
      public void WidgetSelected(SelectionEvent e) { 
       FileDialog dialog = new FileDialog (shell, SWT.OPEN); 
       dialog.setFilterExtensions(new String [] {"*.html"}); 
       //dialog.setFilterPath("c:\\temp"); 
       result = dialog.open(); 
       text4.setText(result); 
      } 
     }); 

     Label Label3 = new Label (shell, SWT.NONE); 
     Label3.setText ("Message Label 3: "); 
     data = new GridData (200, SWT.DEFAULT); 
     Label3.setLayoutData (data); 

     Text text5 = new Text (shell, SWT.BORDER); 
     text5.setText (""); 
     data = new GridData (215, SWT.DEFAULT); 
     text5.setLayoutData (data); 

     Button BrowseBtn2 = new Button (shell, SWT.PUSH); 
     BrowseBtn2.setText ("Browse"); 
     data = new GridData (80, SWT.DEFAULT); 
     BrowseBtn2.setLayoutData (data); 

     Label Label4 = new Label (shell, SWT.NONE); 
     Label4.setText ("Message Label 4: "); 
     data = new GridData (200, SWT.DEFAULT); 
     Label4.setLayoutData (data); 

     Combo Combo2 = new Combo (shell, SWT.NONE); 
     Combo2.setText ("Options"); 
     data = new GridData (200, SWT.DEFAULT); 
     Combo2.setLayoutData (data); 

     Button searchBtn1 = new Button (shell, SWT.PUSH); 
     searchBtn1.setText ("Search"); 
     data = new GridData (80, SWT.DEFAULT); 
     searchBtn1.setLayoutData (data); 

     Combo Combo3 = new Combo (shell, SWT.NONE); 
     // CaseStudyCombo.setItems (new String [] {"Item 1", "Item 2", "Item 2"}); 
     Combo3.setText ("Options:"); 
     data = new GridData (200, SWT.DEFAULT); 
     Combo3.setLayoutData (data); 

     Combo Combo4 = new Combo (shell, SWT.NONE); 
     Combo4.setText ("Options:"); 
     data = new GridData (200, SWT.DEFAULT); 
     Combo4.setLayoutData (data); 

     Button showDetailsBtn = new Button (shell, SWT.PUSH); 
     showDetailsBtn.setText ("Show Details"); 
     data = new GridData (80, SWT.DEFAULT); 
     showDetailsBtn.setLayoutData (data); 

     shell.pack(); 
     shell.open(); 

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

이 문제를 해결하는 데 도움을주십시오.

public void WidgetSelected(SelectionEvent e) 

참고 대문자 'W':

+2

팁 : [Java 변수 이름 지정 규칙] (http : //docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html). – Baz

답변

3

문제가있는 줄은입니다. 현재 익명 클래스에서 기존 widgetSelected (...) 메소드를 재정의하는 대신 새 메소드를 정의하고 있습니다. 당신이 'W'를 소문자로 변경하는 경우 다음과 같이, 그것은 작동합니다

public void widgetSelected(SelectionEvent e) 

@Override 주석을 사용 (권장보다 실제로 더) 좋은의 연습이다. 만약 당신이 그것을 사용했다면, 컴파일러는 오류를 표시했을 것입니다. (예 : Joshua Bloch Effective Java 2 판 항목 36 : Override 주석을 일관되게 사용하십시오.)

+1

+1 모든 대문자 변수 중에서이 점을 찾아냅니다. – Baz