简体   繁体   English

GUI计算器应用程序

[英]GUI Calculator application

The calculator will have a simple interface.2 operands X and Y and the result which cannot be editable. 计算器将具有一个简单的界面。X和Y两个操作数,并且结果不可编辑。 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. 有4个按钮+,-,*,/。单击该按钮后,结果将显示在结果字段中。每次单击一个按钮,整个方程式将显示在下表中。因此,如果有10个操作完成后,所有表达式应显示在下面,例如3 + 2 = 5,5-3 = 2等。 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? 但是我无法在“否X op Y =结果”的以下框中打印操作。在这种情况下,您可以帮我吗?

Use the trim() method of the String class to trim the text you pass to Integer.parseInt() 使用String类的trim()方法来修剪传递给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. 即在将字符串内容传递给parseInt()方法的任何地方都对其进行修剪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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