GUI Calculator应用程序

时间:2015-01-09 20:31:22

标签: java swt

计算器将有一个简单的接口.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);

}
}

但我无法在&#34; No X op Y = Result的下方框中打印操作。 在这种情况下你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

使用trim()类的String方法修剪您传递给Integer.parseInt()的文字

使用

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

而不是

Integer.parseInt(xTextBox.getText())

,即将字符串内容修改为传递给parseInt()方法的任何位置。