简体   繁体   中英

GUI Calculator application

The calculator will have a simple interface.2 operands X and Y and the result which cannot be editable. There are 4 buttons +,-,*,/.As soon as the button is clicked,the result appears in the result field.Every time a button is clicked,the entire equation is shown in the table below.So if there are 10 operations done,all the expressions should be displayed below such as 3+2=5,5-3=2etc. THis is the question and so far i have done this.

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);

}
}

But I am unable to print the operations in the below box of the the "No X op Y = Result. Can you help me out in this case?

Use the trim() method of the String class to trim the text you pass to Integer.parseInt()

Use

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

instead of

Integer.parseInt(xTextBox.getText())

ie trim the string contents wherever you pass it to the parseInt() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM