我正在尝试编写我的计算器,但还没有完成。但是,我尝试了很多次才能在WindowListener中获取windowClosing事件,但它似乎根本不起作用,我和几个朋友讨论过,但我们还没有想到它。请帮助我们解决这个问题。下面是我的代码。第一个是设置框架。
NAudio.Wave.SampleProviders
这是包含windowListener
的“main”文件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
}
}
答案 0 :(得分:0)
有一种简单的方法可以根据oracle文档本身编写WindowListener实现。我已经花了我的时间为你实现它,因为你自己取得了一些进展。为了更深入地了解我使用该方法的原因,请转到https://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html。 在您的CalculatorFrame类中,我进行了更正。
$.ajax({
url: "https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=Robert&limit=6&suggest=1&redirects=return&callback=ttt",
jsonp: "callback",
dataType: "jsonp",
success: function( response ) {
console.log( response );
}
});
您可能需要删除CalculatorMain类中的WindowListener实现。还要知道我已经注释了一些行以允许代码编译。