我想爲另一個程序編寫一個簡單的GUI。使用Eclipse和插件'WindowBuilder'。這是我到目前爲止:Java Swt IllegalArgumentException:參數不能爲空
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
public class LightGUI {
protected Shell shell;
private Text lichterEingabe;
private Text befehleEingabe;
private Text ipEingabe;
private Text portEingabe;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
LightGUI window = new LightGUI();
//PROBLEM APPARENTLY HERE:
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()) {
//...OR HERE:
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setBackground(SWTResourceManager.getColor(230, 230, 250));
shell.setSize(701, 513);
shell.setText("SWT Application");
Label lichter = new Label(shell, SWT.NONE);
lichter.setBackground(SWTResourceManager.getColor(230, 230, 250));
lichter.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
lichter.setBounds(70, 71, 76, 21);
lichter.setText("Lichter:");
Label befehle = new Label(shell, SWT.NONE);
befehle.setBackground(SWTResourceManager.getColor(230, 230, 250));
befehle.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
befehle.setBounds(70, 130, 76, 22);
befehle.setText("Befehle:");
Label ip = new Label(shell, SWT.NONE);
ip.setBackground(SWTResourceManager.getColor(230, 230, 250));
ip.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
ip.setBounds(350, 71, 76, 21);
ip.setText("IP:");
Label port = new Label(shell, SWT.NONE);
port.setBackground(SWTResourceManager.getColor(230, 230, 250));
port.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
port.setBounds(350, 130, 76, 22);
port.setText("Port:");
lichterEingabe = new Text(shell, SWT.BORDER);
lichterEingabe.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
lichterEingabe.setBounds(177, 71, 88, 28);
befehleEingabe = new Text(shell, SWT.BORDER);
befehleEingabe.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
befehleEingabe.setBounds(177, 124, 88, 28);
ipEingabe = new Text(shell, SWT.BORDER);
ipEingabe.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
ipEingabe.setBounds(456, 71, 88, 28);
portEingabe = new Text(shell, SWT.BORDER);
portEingabe.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
portEingabe.setBounds(455, 124, 89, 28);
Button btnNewButton = new Button(shell, SWT.NONE);
btnNewButton.setForeground(SWTResourceManager.getColor(138, 43, 226));
btnNewButton.setFont(SWTResourceManager.getFont("Segoe UI", 14, SWT.BOLD));
btnNewButton.setBounds(417, 239, 120, 49);
btnNewButton.setText("OK!");
Label rueckmeldung = new Label(shell, SWT.NONE);
rueckmeldung.setBackground(SWTResourceManager.getColor(230, 230, 250));
rueckmeldung.setFont(SWTResourceManager.getFont("Segoe UI", 13, SWT.NORMAL));
rueckmeldung.setBounds(70, 319, 134, 28);
rueckmeldung.setText("Rueckmeldung:");
StyledText styledText = new StyledText(shell, SWT.BORDER);
styledText.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
styledText.setEditable(false);
styledText.setBounds(70, 353, 340, 96);
btnNewButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
rueckmeldung.setText(null);
if(lichterEingabe.getText()!=null && befehleEingabe.getText()!=null && ipEingabe.getText()!=null && portEingabe.getText()!=null){
new Steuerung(Integer.parseInt(lichterEingabe.getText()), Integer.parseInt(befehleEingabe.getText()));
}
break;
}
}
});
}
}
該窗口是可見的,並在啓動後。這是我的代碼的結果:
一個是爲了寫號到四個文本框(第五,做大一個用於顯示錯誤消息的唯一目的)。填寫完所有內容後,必須點擊按鈕OK。在此基礎上,一些計算將在後臺進行(計算結果在此工作之前將不相關)。
但是,會發生以下情況: 只要單擊按鈕,我的窗口就會自動消失。 而Eclipse控制檯顯示以下錯誤信息:
java.lang.IllegalArgumentException: Argument cannot be null
at org.eclipse.swt.SWT.error(SWT.java:4514)
at org.eclipse.swt.SWT.error(SWT.java:4448)
at org.eclipse.swt.SWT.error(SWT.java:4419)
at org.eclipse.swt.widgets.Widget.error(Widget.java:482)
at org.eclipse.swt.widgets.Label.setText(Label.java:403)
at LightGUI$1.handleEvent(LightGUI.java:126)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4418)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4236)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3824)
at LightGUI.open(LightGUI.java:49)
at LightGUI.main(LightGUI.java:34)
任何人都可以看到的東西我沒有?這是爲什麼發生?不應該有任何可見的變化,但程序似乎認爲'window.open();'或display.readAndDispatch()由於某種原因返回null?
一個提示,究竟是什麼問題將不勝感激,因爲我甚至沒有最微不足道的想法。
哇,我現在覺得非常愚蠢。非常感謝你!你從大腦毀滅性的封鎖中解救了幾個小時。 :) – Aye