2017-09-19 7 views
0

Java GUI를 배우려고하고 Eclipse Windowbuilder 가이드를 시작했습니다. 이벤트 처리에 관한 부분에 다가 갔지만 아무 것도하지 않습니다. 처음에는 MessageBox가 작동하지 않는다고 생각하여 간단한 것을 시도했지만 사용자 이름과 암호를 바꿔서 함수를 표시했지만 여전히 고장났습니다.Eclipse Windowbuilder 이벤트 핸들러가 작동하지 않습니까?

누가 문제를 알 수 있습니까?

문제를 추론 할 콘솔이 있다면 도움이 될 것입니다.

import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.Label; 
import org.eclipse.swt.SWT; 
import org.eclipse.wb.swt.SWTResourceManager; 
import org.eclipse.swt.graphics.Image; 
import org.eclipse.swt.widgets.Event; 
import org.eclipse.swt.widgets.Text; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Listener; 
import org.eclipse.swt.widgets.MessageBox; 
import org.eclipse.swt.events.SelectionAdapter; 
import org.eclipse.swt.events.SelectionEvent; 




public class MainWindow { 

    protected Shell shell; 
    private Label icon; 
    private Text userNameTxt; 
    private Text passwordTxt; 


    private String userName = null; 
    private String password = null; 

    /** 
    * Launch the application. 
    * @param args 
    */ 
    public static void main(String[] args) { 
     try { 
      MainWindow window = new MainWindow(); 
      window.open(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Open the window. 
    */ 
    public void open() { 
     Display display = Display.getDefault(); 
     createContents(); 
     shell.open(); 
     shell.layout(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) { 
       display.sleep(); 
      } 
     } 
    } 

    /** 
    * Create contents of the window. 
    */ 
    protected void createContents() { 
     shell = new Shell(); 
//  shell.setImage(SWTResourceManager.getImage(MainWindow.class, "/resources/ember-icon.png")); 
//  Image image = SWTResourceManager.getImage(MainWindow.class, "/resources/538449165.jpg"); 
//  Shell shell = new Shell(SWT.NO_TRIM); 
     shell.setSize(700, 500); 
//  shell.setBackgroundImage(image); 
     shell.setBounds(10,10,620,420); 
     shell.setText("Application"); 
     shell.setBackgroundMode(SWT.INHERIT_DEFAULT); 

     icon = new Label(shell, SWT.NONE); 
//  icon.setImage(SWTResourceManager.getImage(MainWindow.class, "/resources/Camp-Fire.png")); 
     icon.setBounds(237, 38, 128, 128); 

     Label lblNewLabel_1 = new Label(shell, SWT.NONE); 
     lblNewLabel_1.setBounds(178, 206, 55, 15); 
     lblNewLabel_1.setText("Username"); 

     Label lblNewLabel_2 = new Label(shell, SWT.NONE); 
     lblNewLabel_2.setBounds(178, 244, 55, 15); 
     lblNewLabel_2.setText("Password"); 

     userNameTxt = new Text(shell, SWT.BORDER); 
     userNameTxt.setBounds(249, 203, 188, 21); 

     passwordTxt = new Text(shell, SWT.BORDER); 
     passwordTxt.setBounds(249, 241, 188, 21); 

     Button login = new Button(shell, SWT.NONE); 
     login.addSelectionListener(new SelectionAdapter() { 
      @Override 
      public void widgetSelected(SelectionEvent e) { 
       userName = userNameTxt.getText(); 
       password = passwordTxt.getText(); 
       userNameTxt.setText(password); 
       passwordTxt.setText(userName); 
      } 
     }); 
//  login.addSelectionListener(new SelectionAdapter() { 
//   public void widgetSelected(SelectionEvent e) { 
//    
//    userName = userNameTxt.getText(); 
//    password = passwordTxt.getText(); 
//    
//    userNameTxt.setText(""); 
//    passwordTxt.setText(""); 
//    if (userName == null || userName.isEmpty() || password == null || password.isEmpty()) { 
//     String errorMsg = null; 
//     MessageBox messageBox = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR); 
// 
//     messageBox.setText("Alert"); 
//     if (userName == null || userName.isEmpty()) { 
//      errorMsg = "Please enter userName"; 
//     } else if (password == null || password.isEmpty()) { 
//      errorMsg = "Please enter password"; 
//     } 
//     if (errorMsg != null) { 
//      messageBox.setMessage(errorMsg); 
//      messageBox.open(); 
//     } 
//    } else { 
//     MessageBox messageBox = new MessageBox(shell, SWT.OK | SWT.ICON_WORKING); 
//     messageBox.setText("Info"); 
//     messageBox.setMessage("Valid"); 
//     messageBox.open(); 
//    } 
//   } 
//  }); 
     login.setBounds(249, 288, 75, 25); 
     login.setText("Login"); 


    } 
    public Image geticonImage() { 
     return icon.getImage(); 
    } 
    public void seticonImage(Image image) { 
     icon.setImage(image); 
    } 
} 
+2

잘 작동합니다. 로그인을 클릭하면 사용자 이름과 비밀번호가 바뀝니다. –

+1

이 방법이 효과적입니다. Eclipse 메뉴에서 Project-> Build Automatically turned on을 선택 했습니까? –

답변

0

나는 바보입니다. 팔레트 맨 위에있는 버튼을 눌러 보았습니다. 그것은 미학을 검사하기위한 미리보기 버튼입니다. 기능이 없습니다 ..... 혼란 스럽다고 말할 수 있지만, 아니, 나는 바보입니다.

실행 단추를 눌러 내 문제를 해결했습니다.

관련 문제