2015-01-09 59 views
0

該計算器將有一個簡單的界面.2操作數X和Y以及無法編輯的結果。有4個按鈕+, - ,*,/。一旦按鈕被點擊,結果出現在結果欄中。每次點擊一個按鈕時,整個公式將顯示在下表中。因此,如果有10個操作完成後,所有表達式都應顯示在下面,例如3 + 2 = 5,5-3 = 2etc。這是個問題,到目前爲止我已經完成了這個任務。圖形用戶界面計算器應用程序

import java.util.ArrayList; 

import org.eclipse.jface.dialogs.MessageDialog; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.Text; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Label; 
import org.eclipse.swt.custom.SashForm; 
import org.eclipse.swt.graphics.Point; 
import org.eclipse.swt.events.SelectionAdapter; 
import org.eclipse.swt.events.SelectionEvent; 


public class Calculator { 

protected Shell shell; 

/** 
* Launch the application. 
* @param args 
*/ 
public static void main(String[] args) { 
    try { 
     Calculator window = new Calculator(); 
     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.setMinimumSize(new Point(150, 39)); 
    shell.setSize(450, 300); 
    shell.setText("Calculator"); 
    shell.setLayout(null); 

    Text xTextBox = new Text(shell, SWT.BORDER); 
    xTextBox.setText(" "); 
    xTextBox.setBounds(37, 10, 76, 21); 

    Text yTextBox = new Text(shell, SWT.BORDER); 
    yTextBox.setText(" "); 
    yTextBox.setBounds(37, 52, 76, 21); 

    ArrayList<String> resultList = new ArrayList<String>(); 
    String a = xTextBox.getText(); 
    String b = yTextBox.getText(); 
    CalculationClass clClass = new CalculationClass(a, b); 

    Label ResultsLabel = new Label(shell, SWT.NONE); 
    ResultsLabel.setBounds(10, 93, 424, 15); 
    ResultsLabel.setText("No. X Op Y =Result"); 

    Button addButton = new Button(shell, SWT.NONE); 
    addButton.setToolTipText("press to add the numbers"); 
    addButton.addSelectionListener(new SelectionAdapter() { 
     @Override 

      public void widgetSelected(SelectionEvent e) { 
       int a,b; 
       try{ 
        a=Integer.parseInt(xTextBox.getText()); 
       } 
       catch(Exception e1){ 
        MessageDialog.openError(shell, "Error", "Bad number"); 
       return; 
       } 
       try{ 
        b=Integer.parseInt(yTextBox.getText()); 
       } 
       catch(Exception e1){ 
        MessageDialog.openError(shell, "Error", "Bad number"); 
       return; 
       } 
       int answer=a+b; 
       ResultsLabel.setText("No. X Op Y =Result"+answer); 



     } 
    }); 
    addButton.setBounds(273, 10, 55, 21); 
    addButton.setText("+"); 

    Button subractButton = new Button(shell, SWT.NONE); 
    subractButton.setToolTipText("press to subract the numbers"); 
    subractButton.addSelectionListener(new SelectionAdapter() { 
     public void widgetSelected(SelectionEvent e) { 
      int a,b; 
      try{ 
       a=Integer.parseInt(xTextBox.getText()); 
      } 
      catch(Exception e1){ 
       MessageDialog.openError(shell, "Error", "Bad number"); 
      return; 
      } 
      try{ 
       b=Integer.parseInt(yTextBox.getText()); 
      } 
      catch(Exception e1){ 
       MessageDialog.openError(shell, "Error", "Bad number"); 
      return; 
      } 
      int answer=a-b; 
      ResultsLabel.setText("No. X Op Y = "+answer); 



    } 
    }); 
    subractButton.setBounds(273, 52, 55, 21); 
    subractButton.setText("-"); 

    Button multiplyButton = new Button(shell, SWT.NONE); 
    multiplyButton.addSelectionListener(new SelectionAdapter() { 
     @Override 
     public void widgetSelected(SelectionEvent e) { 
      int a,b; 
      try{ 
       a=Integer.parseInt(xTextBox.getText()); 
      } 
      catch(Exception e1){ 
       MessageDialog.openError(shell, "Error", "Bad number"); 
      return; 
      } 
      try{ 
       b=Integer.parseInt(yTextBox.getText()); 
      } 
      catch(Exception e1){ 
       MessageDialog.openError(shell, "Error", "Bad number"); 
      return; 
      } 
      int answer=a*b; 
      ResultsLabel.setText("No. X Op Y = "+answer); 



    } 
}); 
    multiplyButton.setToolTipText("press to multiply the numbers"); 
    multiplyButton.setBounds(356, 10, 55, 21); 
    multiplyButton.setText("*"); 

    Button divideButton = new Button(shell, SWT.NONE); 
    divideButton.addSelectionListener(new SelectionAdapter() { 
     public void widgetSelected(SelectionEvent e) { 
      int a,b; 
      try{ 
       a=Integer.parseInt(xTextBox.getText()); 
      } 
      catch(Exception e1){ 
       MessageDialog.openError(shell, "Error", "Bad number"); 
      return; 
      } 
      try{ 
       b=Integer.parseInt(yTextBox.getText()); 
      } 
      catch(Exception e1){ 
       MessageDialog.openError(shell, "Error", "Bad number"); 
      return; 
      } 
      int answer=a+b; 
      ResultsLabel.setText("No. X Op Y = "+answer); 



    } 
}); 
    divideButton.setToolTipText("press to divide the numbers"); 
    divideButton.setBounds(356, 52, 55, 21); 
    divideButton.setText("/"); 

    Label lblX = new Label(shell, SWT.NONE); 
    lblX.setBounds(10, 10, 16, 21); 
    lblX.setText("X"); 

    Label lblX_1 = new Label(shell, SWT.NONE); 
    lblX_1.setBounds(10, 10, 19, 15); 
    lblX_1.setText("X"); 

    Label lblY = new Label(shell, SWT.NONE); 
    lblY.setBounds(10, 52, 19, 15); 
    lblY.setText("Y"); 





    String result = new String(); 
    result = ResultsLabel.getText()+"/n"; 

    for(String s: resultList){ 
     result = result+s+"/n"; 
    } 

    SashForm sashForm = new SashForm(shell, SWT.NONE); 
    sashForm.setBounds(10, 10, 0, 0); 

    Button resetButton = new Button(shell, SWT.NONE); 
    resetButton.setBounds(0, 226, 434, 25); 
    resetButton.setText("Reset"); 

    Label label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); 
    label.setBounds(0, 79, 434, 2); 

    Label label_1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); 
    label_1.setBounds(-21, 85, 455, 2); 

    Label label_2 = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL); 
    label_2.setBounds(185, 0, 0, 80); 

    Label label_3 = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL); 
    label_3.setBounds(201, 0, 0, 81); 

    Label label_4 = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL); 
    label_4.setBounds(183, 0, 0, 77); 

    Label label_5 = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL); 
    label_5.setBounds(185, 3, 10, 70); 

} 
} 

不過,我不能在「無X運算Y =結果的下方盒打印的操作。 你能幫助我在這種情況下?

+0

你好有人嗎? – 2015-01-09 21:03:28

+1

您可以編輯您提供的問題代碼,僅包含相關代碼嗎?這會讓其他人更容易瞭解問題所在 – 2015-01-10 03:06:50

回答

0

利用的trim()方法String類修剪的文字傳遞給Integer.parseInt()

使用

Integer.parseInt(xTextBox.getText().trim()) 

,而不是

Integer.parseInt(xTextBox.getText()) 

即修剪字符串內容,無論您將它傳遞給parseInt()方法。