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);
}
}
這對我很好 - 點擊登錄時,用戶名和密碼被交換。 –
這應該起作用。你有沒有從Eclipse菜單中打開Project-> Build Automatically? –