简体   繁体   English

“ WindowListener”`; 窗口的右上角X不能帮助关闭我的窗口

[英]“WindowListener”`; the up right X of the window does not help to close my window

I am trying to write my calculator, it's not done yet. 我正在尝试编写计算器,但尚未完成。 However, I tried so many times to get the windowClosing event in WindowListener right, but it seems it does not work at all, and I have discussed with a couple friends, but we haven't figured it out yet. 但是,我尝试了很多次才使WindowListener中的windowClosing事件正确,但似乎根本不起作用,我与几个朋友讨论过,但我们还没有弄清楚。 Please kindly help us for this problem. 请为我们解决这个问题。 below is my code. 下面是我的代码。 the first one is to set the frame. 第一个是设置框架。

package Calculator;

import java.awt.Frame;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.Panel;
import java.awt.GridLayout;





public class CalculatorFrame extends Frame {

static GridLayout grid= new GridLayout(3,3);
static Panel panel1= new Panel();
static Font newfont=new Font("",Font.BOLD,32);
static Font newfont1=new Font("",Font.BOLD+Font.ITALIC,20);
static Font newfont2=new Font("",Font.BOLD+Font.ITALIC,38);
static TextArea txtarea=new TextArea("",20,15,TextArea.SCROLLBARS_VERTICAL_ONLY);

static Button B[]=new Button[9];
static Button zero=new Button();
static Button posnneg=new Button();
static Button equal=new Button();
static Button dot=new Button();
static Button divide=new Button();
static Button plus=new Button();
static Button minus=new Button();
static Button multi=new Button();
static Button clear=new Button();
static Button square=new Button();
static Button squareroot=new Button();

                            //virtuals
static int operand; //for switch case 1=+ 2=- 3=* 4=/
static boolean firstnumber=true;
static int continuecalculating;
static Frame myframe=new Frame();
static double tempNumber[]=new double[2];
static TextArea txtarea2=new TextArea("",20,15,TextArea.SCROLLBARS_VERTICAL_ONLY);
static boolean equalclicked=false;
static boolean operandclicked=false;
static boolean pnclicked=false;


public CalculatorFrame(String name){

    myframe.setLayout(null);
    myframe.setBounds(100, 100, 700, 700);
    myframe.setVisible(true);
    myframe.setBackground(Color.BLACK);
    myframe.setTitle(name);

    panel1.setBounds(50, 300, 240, 240);
    panel1.setLayout(grid);
    panel1.setBackground(Color.GRAY);
    zero.setLabel("0");
    zero.setBounds(50, 540, 120, 80);
    zero.setFont(newfont);
    posnneg.setLabel("±");
    posnneg.setBounds(230,540,60,80);
    posnneg.setFont(newfont);
    dot.setLabel(".");
    dot.setFont(newfont);
    dot.setBounds(170, 540, 60, 80);
    equal.setLabel("=");
    equal.setBounds(50,620,180,50);
    equal.setFont(newfont);
    squareroot.setBounds(230,620,60,50);
    squareroot.setLabel("√");
    squareroot.setFont(newfont);
    plus.setLabel("+");
    plus.setBounds(290, 300, 60, 60);
    plus.setFont(newfont);
    minus.setLabel("-");
    minus.setBounds(290, 360, 60, 60);
    minus.setFont(newfont);
    multi.setLabel("×");
    multi.setBounds(290, 420, 60, 60);
    multi.setFont(newfont);
    divide.setLabel("÷");
    divide.setBounds(290, 480, 60, 60);
    divide.setFont(newfont);
    square.setLabel("X²");
    square.setBounds(290, 540, 60, 60);
    square.setFont(newfont1);
    clear.setLabel("Clear");
    clear.setBounds(290, 600, 60, 70);
    clear.setFont(newfont1);
    txtarea.setBounds(50, 50, 300, 180);
    txtarea.setEditable(true);
    txtarea.setFont(newfont2);
    txtarea.setEditable(false);


    txtarea2.setVisible(true);              
    txtarea2.setBounds(50, 230, 300, 60);
    txtarea2.setFont(newfont1);
    txtarea2.setEditable(false);

    for(int i=0;i<9;i++)
    {
        B[i]=new Button(i+1+""); 
        B[i].setFont(newfont);
    }
    panel1.add(B[6]);
    panel1.add(B[7]);
    panel1.add(B[8]);
    panel1.add(B[3]);
    panel1.add(B[4]);
    panel1.add(B[5]);
    panel1.add(B[0]);
    panel1.add(B[1]);
    panel1.add(B[2]);

    myframe.add(panel1);
    myframe.add(zero);
    myframe.add(dot);
    myframe.add(equal);
    myframe.add(plus);
    myframe.add(minus);
    myframe.add(multi);
    myframe.add(divide);
    myframe.add(square);
    myframe.add(clear);
    myframe.add(txtarea);
    myframe.add(txtarea2);
    myframe.add(squareroot);
    myframe.add(posnneg);

}


static class MyActLis implements ActionListener{

public void actionPerformed(ActionEvent e) {
    Button btn=(Button) e.getSource();
    if(btn==B[0])
    {
    if(equalclicked)
    {}
    else{
    txtarea.append("1"); txtarea2.append("1");
    }
    }
    else if(btn==B[1])
    {
    if(equalclicked)
    {}
    else{
    txtarea.append("2"); txtarea2.append("2");}
    }
    else if(btn==B[2])
    {
    if(equalclicked){}else{
    txtarea.append("3");txtarea2.append("3");}
    }
    else if(btn==B[3])
    {
    if(equalclicked){}else{
    txtarea.append("4");txtarea2.append("4");}
    }
    else if(btn==B[4])
    {
    if(equalclicked){}else{
    txtarea.append("5");txtarea2.append("5");}
    }
    else if(btn==B[5])
    {
    if(equalclicked){}else{
    txtarea.append("6");txtarea2.append("6");}
    }
    else if(btn==B[6])
    {
    if(equalclicked){}else{
    txtarea.append("7");txtarea2.append("7");}
    }
    else if(btn==B[7])
    {
    if(equalclicked){}else{
    txtarea.append("8");txtarea2.append("8");}
    }
    else if(btn==B[8])
    {
    if(equalclicked){}else{
    txtarea.append("9");txtarea2.append("9");}
    }
    else if(btn==zero)
    {
    if(equalclicked){}else{
    txtarea.append("0");txtarea2.append("0");}              
    }
    else if(btn==dot)
    {
    if(equalclicked){}else{
    txtarea.append(".");txtarea2.append(".");}      
    }
    else if(btn==posnneg)
    {
            if(pnclicked)
            {
                //+
                pnclicked=false;

            }else
            {
                //-
                pnclicked=true;

            }
    }

    else if(btn==squareroot)
    {
        if(equalclicked)
        {
        tempNumber[0]= Math.sqrt(tempNumber[0]);                //good
        txtarea.append("√= "+Double.toString(tempNumber[0]));
        txtarea2.setText(Double.toString(tempNumber[0]));
        }else
        {
        if(operandclicked){}
        else
        {
        tempNumber[0]= Double.parseDouble(txtarea2.getText());
        tempNumber[0]= Math.sqrt(tempNumber[0]);                //good
        txtarea.append("√= "+Double.toString(tempNumber[0]));
        txtarea2.setText(Double.toString(tempNumber[0]));
        }
        }
    }
    else if(btn==square)
    {
        if(equalclicked)
        {
        tempNumber[0]=tempNumber[0]*tempNumber[0];
        txtarea.append("²= "+Double.toString(tempNumber[0])); //good   but how to continuously making calculation
        txtarea2.setText(Double.toString(tempNumber[0]));
        }
        else{
        if(operandclicked){}
        else
        {
        tempNumber[0]= Double.parseDouble(txtarea2.getText());  
        tempNumber[0]=tempNumber[0]*tempNumber[0];
        txtarea.append("²= "+Double.toString(tempNumber[0])); //good   but how to continuously making calculation
        txtarea2.setText(Double.toString(tempNumber[0]));
        }
        }
    }

    else if(btn==plus)
    {
    operandclicked=true;
    if(equalclicked){firstnumber=true; equalclicked=false;}
    if(firstnumber)
    {
    tempNumber[0]= Double.parseDouble(txtarea2.getText());
    txtarea.append("+"); 
    operand=1;      //set operand for execution
    txtarea2.setText(" ");
    txtarea2.setText("");
    firstnumber=false;
    }
        else
        {
        switch(operand)
        {
        case 1:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("+");
            tempNumber[0]=CalculatorMethods.addition(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=1;
            break;

        case 2:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("+");
            tempNumber[0]=CalculatorMethods.substraction(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=1;

            break;
        case 3: 
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("+");
            tempNumber[0]=CalculatorMethods.multiply(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=1;
            break;

        case 4: 
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("+");
            tempNumber[0]=CalculatorMethods.division(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=1;
            break;
        }
        }

    }
    else if(btn==minus)
    {
    operandclicked=true;
    if(equalclicked){firstnumber=true; equalclicked=false;}
    if(firstnumber)
    {
    tempNumber[0]= Double.parseDouble(txtarea2.getText());
    txtarea.append("-");
    operand=2;      
    txtarea2.setText(" ");
    txtarea2.setText("");
    firstnumber=false;
    }
        else
        {
            switch(operand)
            {
            case 1:
                tempNumber[1]= Double.parseDouble(txtarea2.getText());
                txtarea.append("-");
                tempNumber[0]=CalculatorMethods.addition(tempNumber[0], tempNumber[1]);
                txtarea2.setText(" ");
                txtarea2.setText("");
                operand=2;
                break;

            case 2:
                tempNumber[1]= Double.parseDouble(txtarea2.getText());
                txtarea.append("-");
                tempNumber[0]=CalculatorMethods.substraction(tempNumber[0], tempNumber[1]);
                txtarea2.setText(" ");
                txtarea2.setText("");
                operand=2;
                break;
            case 3: 
                tempNumber[1]= Double.parseDouble(txtarea2.getText());
                txtarea.append("-");
                tempNumber[0]=CalculatorMethods.multiply(tempNumber[0], tempNumber[1]);
                txtarea2.setText(" ");
                txtarea2.setText("");
                operand=2;
                break;

            case 4: 
                tempNumber[1]= Double.parseDouble(txtarea2.getText());
                txtarea.append("-");
                tempNumber[0]=CalculatorMethods.division(tempNumber[0], tempNumber[1]);
                txtarea2.setText(" ");
                txtarea2.setText("");
                operand=2;
                break;
            }
        }
    }


    else if(btn==multi)
    {
    operandclicked=true;
    if(equalclicked){firstnumber=true; equalclicked=false;}
    if(firstnumber)
    {
    tempNumber[0]= Double.parseDouble(txtarea2.getText());
    txtarea.append("×");
    operand=3;      
    txtarea2.setText(" ");
    txtarea2.setText("");
    firstnumber=false;

    }else
    {   
        switch(operand)
        {
        case 1:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("×");
            tempNumber[0]=CalculatorMethods.addition(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=3;
            break;

        case 2:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("×");
            tempNumber[0]=CalculatorMethods.substraction(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=3;
            break;
        case 3: 
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("×");
            tempNumber[0]=CalculatorMethods.multiply(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=3;
            break;

        case 4: 
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("×");
            tempNumber[0]=CalculatorMethods.division(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=3;
            break;
        }
    }
    }
    else if(btn==divide)
    {
    operandclicked=true;
    if(equalclicked){firstnumber=true; equalclicked=false;}
    if(firstnumber)
    {
    tempNumber[0]= Double.parseDouble(txtarea2.getText());
    txtarea.append("÷");//÷
    operand=4;
    txtarea2.setText(" ");
    txtarea2.setText("");
    firstnumber=false;
    }else
    {
        switch(operand)
        {
        case 1:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("÷");
            tempNumber[0]=CalculatorMethods.addition(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=4;
            break;

        case 2:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("÷");
            tempNumber[0]=CalculatorMethods.substraction(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=4;
            break;
        case 3: 
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("÷");
            tempNumber[0]=CalculatorMethods.multiply(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=4;
            break;

        case 4: 
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("÷");
            tempNumber[0]=CalculatorMethods.division(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=4;
            break;
        }
    }
    }

    else if(btn==equal)
    {
    if(equalclicked){}
    else{
    tempNumber[1]= Double.parseDouble(txtarea2.getText());
    txtarea.append("=");
    switch(operand)
        {
    case 1: 
        tempNumber[0]=CalculatorMethods.addition(tempNumber[0], tempNumber[1]);
        txtarea2.setText(" ");
        txtarea2.setText("");
        txtarea.append(Double.toString(tempNumber[0]));
        txtarea2.append(Double.toString(tempNumber[0]));
        break;

    case 2: 
        tempNumber[0]=CalculatorMethods.substraction(tempNumber[0], tempNumber[1]);
        txtarea2.setText(" ");
        txtarea2.setText("");
        txtarea.append(Double.toString(tempNumber[0]));
        txtarea2.append(Double.toString(tempNumber[0]));
        break;

    case 3: 
        tempNumber[0]=CalculatorMethods.multiply(tempNumber[0], tempNumber[1]);
        txtarea2.setText(" ");
        txtarea2.setText("");
        txtarea.append(Double.toString(tempNumber[0]));
        txtarea2.append(Double.toString(tempNumber[0]));
        break;

    case 4: 
        tempNumber[0]=CalculatorMethods.division(tempNumber[0], tempNumber[1]);
        txtarea2.setText(" ");
        txtarea2.setText("");
        txtarea.append(Double.toString(tempNumber[0]));
        txtarea2.append(Double.toString(tempNumber[0]));
        break;  
        }
    equalclicked=true;
    operandclicked=false;
    }
    }
    else if(btn==clear)
    {   equalclicked=false;
        firstnumber=true;
        operandclicked=false;
        txtarea.setText(" ");
        txtarea.setText("");
        txtarea2.setText(" ");
        txtarea2.setText("");
        tempNumber[0]=0.0;
        tempNumber[1]=0.0;
        tempNumber[2]=0.0;
    }

}

     //try to use textEvent in the action event or try use switch case and the +-*/are the operands

}
}

Here is the "main" file which contains windowListener 这是包含windowListener的“主”文件

package Calculator;


import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import Calculator.CalculatorFrame.MyActLis;


public class CalculatorMain  {

    static CalculatorFrame cal=new CalculatorFrame("Yu-Ming's Multi-function Calculator Challenge");
    static MyActLis buttonLis=new MyActLis();
    static MyWindowLis winlis=new MyWindowLis();


    public static void main(String[] args) {
        //Create Frame and windows
        //register buttons,and text field, keyevent etc


        for(int i=0;i<9;i++)
        {
        cal.B[i].addActionListener(buttonLis);
        }
        cal.dot.addActionListener(buttonLis);
        cal.zero.addActionListener(buttonLis);
        cal.plus.addActionListener(buttonLis);
        cal.minus.addActionListener(buttonLis);
        cal.multi.addActionListener(buttonLis);
        cal.divide.addActionListener(buttonLis);
        cal.equal.addActionListener(buttonLis);
        cal.squareroot.addActionListener(buttonLis);
        cal.square.addActionListener(buttonLis);
        cal.clear.addActionListener(buttonLis);
        cal.posnneg.addActionListener(buttonLis);
        cal.addWindowListener(winlis);


    }
    static class MyWindowLis extends WindowAdapter
    {
        public void windowClosing(WindowEvent e)
        {
        cal.dispose();
        System.exit(0);
        }


    }

}

Well there is a simple way to write your WindowListener implementation based on the oracle docs itself. 好了,有一种简单的方法可以基于oracle docs本身编写WindowListener实现。 I have taken my time to implement it for you since you've made some progress yourself. 自从您自己取得了一些进展以来,我已经花了一些时间为您实施它。 To get more insight about why I used the method that I have, just go to https://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html . 要了解为什么我使用现有方法,请访问https://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html In your CalculatorFrame class, I have made the correction. 在您的CalculatorFrame类中,我进行了更正。

import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.Panel;





public class CalculatorFrame extends Frame implements WindowListener {

static GridLayout grid= new GridLayout(3,3);
static Panel panel1= new Panel();
static Font newfont=new Font("",Font.BOLD,32);
static Font newfont1=new Font("",Font.BOLD+Font.ITALIC,20);
static Font newfont2=new Font("",Font.BOLD+Font.ITALIC,38);
static TextArea txtarea=new TextArea("",20,15,TextArea.SCROLLBARS_VERTICAL_ONLY);

static Button B[]=new Button[9];
static Button zero=new Button();
static Button posnneg=new Button();
static Button equal=new Button();
static Button dot=new Button();
static Button divide=new Button();
static Button plus=new Button();
static Button minus=new Button();
static Button multi=new Button();
static Button clear=new Button();
static Button square=new Button();
static Button squareroot=new Button();

                            //virtuals
static int operand; //for switch case 1=+ 2=- 3=* 4=/
static boolean firstnumber=true;
static int continuecalculating;
static Frame myframe=new Frame();
static double tempNumber[]=new double[2];
static TextArea txtarea2=new TextArea("",20,15,TextArea.SCROLLBARS_VERTICAL_ONLY);
static boolean equalclicked=false;
static boolean operandclicked=false;
static boolean pnclicked=false;


public CalculatorFrame(String name){

    myframe.setLayout(null);
    myframe.setBounds(100, 100, 700, 700);
    myframe.setVisible(true);
    myframe.setBackground(Color.BLACK);
    myframe.setTitle(name);

    panel1.setBounds(50, 300, 240, 240);
    panel1.setLayout(grid);
    panel1.setBackground(Color.GRAY);
    zero.setLabel("0");
    zero.setBounds(50, 540, 120, 80);
    zero.setFont(newfont);
    posnneg.setLabel("±");
    posnneg.setBounds(230,540,60,80);
    posnneg.setFont(newfont);
    dot.setLabel(".");
    dot.setFont(newfont);
    dot.setBounds(170, 540, 60, 80);
    equal.setLabel("=");
    equal.setBounds(50,620,180,50);
    equal.setFont(newfont);
    squareroot.setBounds(230,620,60,50);
    squareroot.setLabel("√");
    squareroot.setFont(newfont);
    plus.setLabel("+");
    plus.setBounds(290, 300, 60, 60);
    plus.setFont(newfont);
    minus.setLabel("-");
    minus.setBounds(290, 360, 60, 60);
    minus.setFont(newfont);
    multi.setLabel("×");
    multi.setBounds(290, 420, 60, 60);
    multi.setFont(newfont);
    divide.setLabel("÷");
    divide.setBounds(290, 480, 60, 60);
    divide.setFont(newfont);
    square.setLabel("X²");
    square.setBounds(290, 540, 60, 60);
    square.setFont(newfont1);
    clear.setLabel("Clear");
    clear.setBounds(290, 600, 60, 70);
    clear.setFont(newfont1);
    txtarea.setBounds(50, 50, 300, 180);
    txtarea.setEditable(true);
    txtarea.setFont(newfont2);
    txtarea.setEditable(false);


    txtarea2.setVisible(true);
    txtarea2.setBounds(50, 230, 300, 60);
    txtarea2.setFont(newfont1);
    txtarea2.setEditable(false);

    for(int i=0;i<9;i++)
    {
        B[i]=new Button(i+1+"");
        B[i].setFont(newfont);
    }
    panel1.add(B[6]);
    panel1.add(B[7]);
    panel1.add(B[8]);
    panel1.add(B[3]);
    panel1.add(B[4]);
    panel1.add(B[5]);
    panel1.add(B[0]);
    panel1.add(B[1]);
    panel1.add(B[2]);

    myframe.add(panel1);
    myframe.add(zero);
    myframe.add(dot);
    myframe.add(equal);
    myframe.add(plus);
    myframe.add(minus);
    myframe.add(multi);
    myframe.add(divide);
    myframe.add(square);
    myframe.add(clear);
    myframe.add(txtarea);
    myframe.add(txtarea2);
    myframe.add(squareroot);
    myframe.add(posnneg);
    myframe.addWindowListener( this );

}

//public void addListenerForClose(WindowListener windowListener) {
    //addWindowListener( this );
//}


static class MyActLis implements ActionListener{

public void actionPerformed(ActionEvent e) {
    Button btn=(Button) e.getSource();
    if(btn==B[0])
    {
    if(equalclicked)
    {}
    else{
    txtarea.append("1"); txtarea2.append("1");
    }
    }
    else if(btn==B[1])
    {
    if(equalclicked)
    {}
    else{
    txtarea.append("2"); txtarea2.append("2");}
    }
    else if(btn==B[2])
    {
    if(equalclicked){}else{
    txtarea.append("3");txtarea2.append("3");}
    }
    else if(btn==B[3])
    {
    if(equalclicked){}else{
    txtarea.append("4");txtarea2.append("4");}
    }
    else if(btn==B[4])
    {
    if(equalclicked){}else{
    txtarea.append("5");txtarea2.append("5");}
    }
    else if(btn==B[5])
    {
    if(equalclicked){}else{
    txtarea.append("6");txtarea2.append("6");}
    }
    else if(btn==B[6])
    {
    if(equalclicked){}else{
    txtarea.append("7");txtarea2.append("7");}
    }
    else if(btn==B[7])
    {
    if(equalclicked){}else{
    txtarea.append("8");txtarea2.append("8");}
    }
    else if(btn==B[8])
    {
    if(equalclicked){}else{
    txtarea.append("9");txtarea2.append("9");}
    }
    else if(btn==zero)
    {
    if(equalclicked){}else{
    txtarea.append("0");txtarea2.append("0");}
    }
    else if(btn==dot)
    {
    if(equalclicked){}else{
    txtarea.append(".");txtarea2.append(".");}
    }
    else if(btn==posnneg)
    {
            if(pnclicked)
            {
                //+
                pnclicked=false;

            }else
            {
                //-
                pnclicked=true;

            }
    }

    else if(btn==squareroot)
    {
        if(equalclicked)
        {
        tempNumber[0]= Math.sqrt(tempNumber[0]);                //good
        txtarea.append("√= "+Double.toString(tempNumber[0]));
        txtarea2.setText(Double.toString(tempNumber[0]));
        }else
        {
        if(operandclicked){}
        else
        {
        tempNumber[0]= Double.parseDouble(txtarea2.getText());
        tempNumber[0]= Math.sqrt(tempNumber[0]);                //good
        txtarea.append("√= "+Double.toString(tempNumber[0]));
        txtarea2.setText(Double.toString(tempNumber[0]));
        }
        }
    }
    else if(btn==square)
    {
        if(equalclicked)
        {
        tempNumber[0]=tempNumber[0]*tempNumber[0];
        txtarea.append("²= "+Double.toString(tempNumber[0])); //good   but how to continuously making calculation
        txtarea2.setText(Double.toString(tempNumber[0]));
        }
        else{
        if(operandclicked){}
        else
        {
        tempNumber[0]= Double.parseDouble(txtarea2.getText());
        tempNumber[0]=tempNumber[0]*tempNumber[0];
        txtarea.append("²= "+Double.toString(tempNumber[0])); //good   but how to continuously making calculation
        txtarea2.setText(Double.toString(tempNumber[0]));
        }
        }
    }

    else if(btn==plus)
    {
    operandclicked=true;
    if(equalclicked){firstnumber=true; equalclicked=false;}
    if(firstnumber)
    {
    tempNumber[0]= Double.parseDouble(txtarea2.getText());
    txtarea.append("+");
    operand=1;      //set operand for execution
    txtarea2.setText(" ");
    txtarea2.setText("");
    firstnumber=false;
    }
        else
        {
        switch(operand)
        {
        case 1:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("+");
            //tempNumber[0]=CalculatorMethods.addition(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=1;
            break;

        case 2:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("+");
            //tempNumber[0]=CalculatorMethods.substraction(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=1;

            break;
        case 3:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("+");
            //tempNumber[0]=CalculatorMethods.multiply(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=1;
            break;

        case 4:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("+");
            //tempNumber[0]=CalculatorMethods.division(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=1;
            break;
        }
        }

    }
    else if(btn==minus)
    {
    operandclicked=true;
    if(equalclicked){firstnumber=true; equalclicked=false;}
    if(firstnumber)
    {
    tempNumber[0]= Double.parseDouble(txtarea2.getText());
    txtarea.append("-");
    operand=2;
    txtarea2.setText(" ");
    txtarea2.setText("");
    firstnumber=false;
    }
        else
        {
            switch(operand)
            {
            case 1:
                tempNumber[1]= Double.parseDouble(txtarea2.getText());
                txtarea.append("-");
                //tempNumber[0]=CalculatorMethods.addition(tempNumber[0], tempNumber[1]);
                txtarea2.setText(" ");
                txtarea2.setText("");
                operand=2;
                break;

            case 2:
                tempNumber[1]= Double.parseDouble(txtarea2.getText());
                txtarea.append("-");
                //tempNumber[0]=CalculatorMethods.substraction(tempNumber[0], tempNumber[1]);
                txtarea2.setText(" ");
                txtarea2.setText("");
                operand=2;
                break;
            case 3:
                tempNumber[1]= Double.parseDouble(txtarea2.getText());
                txtarea.append("-");
                //tempNumber[0]=CalculatorMethods.multiply(tempNumber[0], tempNumber[1]);
                txtarea2.setText(" ");
                txtarea2.setText("");
                operand=2;
                break;

            case 4:
                tempNumber[1]= Double.parseDouble(txtarea2.getText());
                txtarea.append("-");
                //tempNumber[0]=CalculatorMethods.division(tempNumber[0], tempNumber[1]);
                txtarea2.setText(" ");
                txtarea2.setText("");
                operand=2;
                break;
            }
        }
    }


    else if(btn==multi)
    {
    operandclicked=true;
    if(equalclicked){firstnumber=true; equalclicked=false;}
    if(firstnumber)
    {
    tempNumber[0]= Double.parseDouble(txtarea2.getText());
    txtarea.append("×");
    operand=3;
    txtarea2.setText(" ");
    txtarea2.setText("");
    firstnumber=false;

    }else
    {
        switch(operand)
        {
        case 1:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("×");
            //tempNumber[0]=CalculatorMethods.addition(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=3;
            break;

        case 2:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("×");
            //tempNumber[0]=CalculatorMethods.substraction(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=3;
            break;
        case 3:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("×");
            //tempNumber[0]=CalculatorMethods.multiply(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=3;
            break;

        case 4:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("×");
            //tempNumber[0]=CalculatorMethods.division(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=3;
            break;
        }
    }
    }
    else if(btn==divide)
    {
    operandclicked=true;
    if(equalclicked){firstnumber=true; equalclicked=false;}
    if(firstnumber)
    {
    tempNumber[0]= Double.parseDouble(txtarea2.getText());
    txtarea.append("÷");//÷
    operand=4;
    txtarea2.setText(" ");
    txtarea2.setText("");
    firstnumber=false;
    }else
    {
        switch(operand)
        {
        case 1:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("÷");
            //tempNumber[0]=CalculatorMethods.addition(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=4;
            break;

        case 2:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("÷");
            //tempNumber[0]=CalculatorMethods.substraction(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=4;
            break;
        case 3:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("÷");
            //tempNumber[0]=CalculatorMethods.multiply(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=4;
            break;

        case 4:
            tempNumber[1]= Double.parseDouble(txtarea2.getText());
            txtarea.append("÷");
            //tempNumber[0]=CalculatorMethods.division(tempNumber[0], tempNumber[1]);
            txtarea2.setText(" ");
            txtarea2.setText("");
            operand=4;
            break;
        }
    }
    }

    else if(btn==equal)
    {
    if(equalclicked){}
    else{
    tempNumber[1]= Double.parseDouble(txtarea2.getText());
    txtarea.append("=");
    switch(operand)
        {
    case 1:
        //tempNumber[0]=CalculatorMethods.addition(tempNumber[0], tempNumber[1]);
        txtarea2.setText(" ");
        txtarea2.setText("");
        txtarea.append(Double.toString(tempNumber[0]));
        txtarea2.append(Double.toString(tempNumber[0]));
        break;

    case 2:
        //tempNumber[0]=CalculatorMethods.substraction(tempNumber[0], tempNumber[1]);
        txtarea2.setText(" ");
        txtarea2.setText("");
        txtarea.append(Double.toString(tempNumber[0]));
        txtarea2.append(Double.toString(tempNumber[0]));
        break;

    case 3:
        //tempNumber[0]=CalculatorMethods.multiply(tempNumber[0], tempNumber[1]);
        txtarea2.setText(" ");
        txtarea2.setText("");
        txtarea.append(Double.toString(tempNumber[0]));
        txtarea2.append(Double.toString(tempNumber[0]));
        break;

    case 4:
        //tempNumber[0]=CalculatorMethods.division(tempNumber[0], tempNumber[1]);
        txtarea2.setText(" ");
        txtarea2.setText("");
        txtarea.append(Double.toString(tempNumber[0]));
        txtarea2.append(Double.toString(tempNumber[0]));
        break;
        }
    equalclicked=true;
    operandclicked=false;
    }
    }
    else if(btn==clear)
    {   equalclicked=false;
        firstnumber=true;
        operandclicked=false;
        txtarea.setText(" ");
        txtarea.setText("");
        txtarea2.setText(" ");
        txtarea2.setText("");
        tempNumber[0]=0.0;
        tempNumber[1]=0.0;
        tempNumber[2]=0.0;
    }

}

     //try to use textEvent in the action event or try use switch case and the +-*/are the operands

}


@Override
public void windowActivated(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowClosed(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);

}

@Override
public void windowDeactivated(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowDeiconified(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowIconified(WindowEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void windowOpened(WindowEvent e) {
    // TODO Auto-generated method stub

}

You may need to remove the WindowListener implementation in your CalculatorMain class. 您可能需要在CalculatorMain类中删除WindowListener实现。 Also know that I have commented some lines out to allow the code to compile. 也知道我已经注释了一些行以允许代码进行编译。

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

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