2016-12-14 74 views
1

你好,這是我的第一篇文章,我正在盡我所能學習編程! 我想在Eclipse中用SWT製作一個程序。 這是一個基本程序,應該從用戶接收輸入,並根據(if語句)在用戶輸入的哪一天顯示某個輸出。如何處理輸入?

問題:我第一次嘗試將if語句放在文本字段下,但我注意到我需要某種按鈕來使這個運行,否則它不起作用!

哪裏是使輸入的錯誤:Dilluns不輸出:MATESSS!

public void open() { 
    Display display = Display.getDefault(); 
    createContents(); 
    elsllibres.open(); 
    elsllibres.layout(); 
    while (!elsllibres.isDisposed()) { 
     if (!display.readAndDispatch()) { 
      display.sleep(); 
     } 
    } 
} 

protected void createContents() { 
    elsllibres = new Shell(); 
    elsllibres.setSize(450, 300); 
    elsllibres.setText("OSKARITOOO Industries"); 

    Label lblQuinsLlibresCal = new Label(elsllibres, SWT.NONE); 
    lblQuinsLlibresCal.setBounds(10, 10, 360, 15); 
    lblQuinsLlibresCal.setText("QUINS LLIBRES CAL PORTAR AVUI?"); 

    Label lblIndicaElDia = new Label(elsllibres, SWT.NONE); 
    lblIndicaElDia.setBounds(10, 31, 96, 15); 
    lblIndicaElDia.setText("INDICA EL DIA:"); 

    text_1 = new Text(elsllibres, SWT.BORDER); 
    text_1.setText("EX: Dilluns, Dimarts..."); 
    text_1.setBounds(10, 52, 123, 21); 

    Label info = new Label(elsllibres, SWT.NONE); 
    info.setBounds(10, 79, 174, 15); 
    info.setText("Cal portar els llibres de..."); 

    resultat = new Label(elsllibres, SWT.NONE); 
    resultat.setBounds(10, 100, 207, 15); 

    Button ok = new Button(elsllibres, SWT.NONE); 
    ok.addTouchListener(new TouchListener() { 
     public void touch(TouchEvent arg0) { 
      String quediaes; 
      quediaes = text_1.toString(); 
      if(quediaes.equals("Dilluns")){ 
       resultat.setText("MATESSSS!"); 
      } 
     } 
    }); 
    ok.setBounds(142, 48, 75, 25); 
    ok.setText("OK"); 


    text_1.addMouseTrackListener(new MouseTrackAdapter() { 
     @Override 
     public void mouseEnter(org.eclipse.swt.events.MouseEvent e) { 
      text_1.setText(""); 
     } 
    }); 
} 
+1

這不是Swing。它的SWT。 – camickr

+0

upsitubsy!我很困惑對不起 – oscar6662

+0

在這種情況下,我很抱歉,我批准了一個編輯替換SWT標記與擺動。相反,標題和文本應該已經改爲閱讀SWT。這太快了。 –

回答

0

您正在使用需要支持觸摸(並且必須啓用)的設備的觸摸偵聽器。

假設你使用的是普通的PC,你需要使用一個選擇偵聽器按鈕:

Button ok = new Button(elsllibres, SWT.NONE); 
ok.addSelectionListener(new SelectionAdapter() 
    { 
    @Override 
    public void widgetSelected(final SelectionEvent e) 
    { 
     String quediaes = text_1.getText(); // getText not toString 

     if (quediaes.equals("Dilluns")) { 
     resultat.setText("MATESSSS!"); 
    } 
    } 
}); 

您還必須調用文本控件的getText()方法,使用的是不返回toString方法輸入文字。

+0

謝謝你,先生:) – oscar6662