2017-09-19 42 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? –

回答

0

我是个白痴。我正在按压调色板顶部的那个按钮。这是检查美学的预览按钮。没有功能.....我可以说它很混乱,但不,我是个白痴。

按运行按钮解决了我的问题。

相关问题