簡體   English   中英

GUI計算器應用程序

[英]GUI Calculator application

計算器將具有一個簡單的界面。X和Y兩個操作數,並且結果不可編輯。 有4個按鈕+,-,*,/。單擊該按鈕后,結果將顯示在結果字段中。每次單擊一個按鈕,整個方程式將顯示在下表中。因此,如果有10個操作完成后,所有表達式應顯示在下面,例如3 + 2 = 5,5-3 = 2等。 這是問題,到目前為止,我已經做到了。

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 op Y =結果”的以下框中打印操作。在這種情況下,您可以幫我嗎?

使用String類的trim()方法來修剪傳遞給Integer.parseInt()的文本

采用

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

代替

Integer.parseInt(xTextBox.getText())

即在將字符串內容傳遞給parseInt()方法的任何地方都對其進行修剪。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM