【问题标题】:Eclipse Windowbuilder event handler not functioning?Eclipse Windowbuilder 事件处理程序不起作用?
【发布时间】:2017-09-19 22:07:25
【问题描述】:

我正在尝试学习 Java GUI,并开始学习 Eclipse Windowbuilder 指南。 我完成了事件处理的部分,它只是拒绝做任何事情。起初我以为 MessageBox 不起作用,所以我尝试了一些简单的方法,只需交换用户名和密码即可显示功能,但仍然损坏。

有人知道问题出在哪里吗?

如果有一个控制台来推断问题会很有帮助。

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);
    }
}

【问题讨论】:

  • 这对我来说很好 - 单击登录时交换用户名和密码。
  • 这应该可以。您是否从 Eclipse 菜单中打开了 Project->Build Automatically?

标签: java eclipse user-interface swt windowbuilder


【解决方案1】:

我是个白痴。我正在按下调色板顶部的那个按钮。那是检查美学的预览按钮。没有功能......我可以说它令人困惑,但不,我是个白痴。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多