2017-09-19 13 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自動的にオンにしましたか? –

答えて

0

私はばかです。パレットの上部にあるボタンを押していました。これは、審美性をチェックするためのプレビューボタンです。機能はない......私は混乱していると言えるだろうが、いいえ、私はばかだ。

実行ボタンを押して問題を解決しました。

関連する問題