0
我正在嘗試編寫我的計算器,尚未完成。然而,我嘗試了很多次,纔在WindowListener中獲得windowClosing事件,但它看起來根本不起作用,而且我已經和幾個朋友討論過了,但我們還沒有弄明白。請善意幫助我們解決這個問題。下面是我的代碼。第一個是設置框架。「WindowListener」`;窗口右上方的X無助於關閉窗口
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
}
}
這裏是「主」文件,其中包含的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);
}
}
}
歡迎來到StackOverflow!我建議你看看如何爲你的問題創建一個[Minimal,Complete,and Verifiable example](http://stackoverflow.com/help/mcve)。 –