2016-12-14 76 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