myactionlistener实现监听器接口,然后实现计算器中的加减乘除时为什么在这一个if(e.getSource()==bnte[27]){}整体中不行
_um_ 2020-11-23 11:43:17
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
import java.sql.Statement;
public class ApiHomework extends JFrame {
public String jbn [] = {"MC","MR","MS","M+","M-","←","CE","C","±",
"√","7","8","9","/","%","4","5","6","*"
,".","1","2","3","-","+","0","1/x","="};
JButton []bn=new JButton[jbn.length];
public String str="0";
public JTextArea jt;
public ApiHomework() {
super("计算机" );
ImageIcon icon=new ImageIcon("./src/homework/icon.png");
ImageIcon iconIts=new ImageIcon("./src/homework/iconIts.png");
this.setIconImage(icon.getImage());
this.setBounds(100, 100, 390, 450);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar menubar=new JMenuBar();
this.setJMenuBar(menubar);
JMenu menu=new JMenu("查看(V)");
JMenu menu1=new JMenu("编辑(E)");
JMenu menu2=new JMenu("帮助(H)");
menubar.add(menu);
menubar.add(menu1);
menubar.add(menu2);
JTextArea jt=new JTextArea( "");
jt.setBounds(30, 5, 305, 50);
jt.setEditable(false);
//----------------------------------------------------------
MyActionListener ma=new MyActionListener(bn,jt);
JPanel jp=new JPanel();
jp.setLayout(null);
for(int i=0;i<jbn.length;i++)
{
bn[i]=new JButton(jbn[i]);
bn[i].addActionListener(ma);
}
//----------------------------------------------------------
bn[0].setBounds(5, 70, 60, 40);
bn[1].setBounds(80, 70, 60, 40);
bn[2].setBounds(155, 70, 60, 40);
bn[3].setBounds(230, 70, 60, 40);
bn[4].setBounds(305, 70, 60, 40);
bn[5].setBounds(5, 125, 60, 40);
bn[6].setBounds(80, 125, 60, 40);
bn[7].setBounds(155, 125, 60, 40);
bn[8].setBounds(230, 125, 60, 40);
bn[9].setBounds(305, 125, 60, 40);
bn[10].setBounds(5, 180, 60, 40);
bn[11].setBounds(80, 180, 60, 40);
bn[12].setBounds(155, 180, 60, 40);
bn[13].setBounds(230, 180, 60, 40);
bn[14].setBounds(305, 180, 60, 40);
bn[15].setBounds(5, 235, 60, 40);
bn[16].setBounds(80, 235, 60, 40);
bn[17].setBounds(155, 235, 60, 40);
bn[18].setBounds(230, 235, 60, 40);
bn[26].setBounds(305, 235, 60, 40);
bn[20].setBounds(5, 290, 60, 40);
bn[21].setBounds(80, 290, 60, 40);
bn[22].setBounds(155, 290, 60, 40);
bn[23].setBounds(230, 290, 60,40);
bn[27].setBounds(305, 290, 60, 95);
bn[25].setBounds(5, 345, 135, 40);
bn[19].setBounds(155, 345, 60, 40);
bn[24].setBounds(230, 345, 60, 40);
for(int i1=0;i1<bn.length;i1++)
{
jp.add(bn[i1]);
}
jp.add(jt);
this.add(jp);
this.setResizable(false);
this.setVisible(true);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import javax.swing.JButton;
import javax.swing.JTextArea;
public class MyActionListener implements ActionListener {
JButton[] bnte;
JTextArea jte;
String str="0";
public MyActionListener(JButton[] bnte, JTextArea jte) {
this.bnte=bnte;
this.jte=jte;
}
//-------------------------------------------------
public void actionPerformed(ActionEvent e) {
for(int i=5;i<bnte.length;i++) {
if(e.getSource()==bnte[i]) {
str+=bnte[i].getText();
}
}
if(str.charAt(0)=='0'){
String temp="";
for(int i=0;i<str.length();i++) {
if(str.charAt(i)!='0'&&Character.isDigit(str.charAt(i))) {
for(int j=i;j<str.length();j++)
temp+=str.charAt(j);
break;
} else if(str.charAt(i)!='0'&&str.charAt(i)=='.') {
String temp1="0";
temp+=temp1;
for(int j=i;j<str.length();j++)
temp+=str.charAt(j);
break;
}
}
//-----------------------------------------------------
if(temp.length()==0) {
temp+=" 0";
jte.setText(temp);
}else {
while(temp.length()<15)
temp=" "+temp;
temp=temp.substring(0, 15);
jte.setText("\n\n"+temp);
}
}else {
jte.setText("\n\n"+str);
}
//-----------------------------------------------------------
if(e.getSource()==bnte[6]||e.getSource()==bnte[7]) {
str="0";
jte.setText("\n\n0");
}
//-------------------------------------------------------------
if(e.getSource()==bnte[26]) {
double x=Double.parseDouble(str);
if(x!=0) {
String temp=""+(1/x);
while(temp.length()<15)
temp=" "+temp;
temp=temp.substring(0, 15);
jte.setText(temp);
}
else
jte.setText("null");
}
//--------------------------------------------------------------------------
String et="0";
if(e.getSource()==bnte[5]&&!str.equals(et)) {
str=str.substring(1, str.length()-1);
jte.setText(" \n\n"+str);
}
//-----------------------------------------------------------
if(e.getSource()==bnte[27]){
str.substring(1, str.length()-1);
if(str.indexOf("+")!=-1) {
int i=str.indexOf("+");
String s1=str.substring(0, i);
String s2=str.substring(i+1, str.length());
Double d=Double.parseDouble(s1.trim());
Double d1=Double.parseDouble(s2.trim());
double d2=d1+d;
jte.setText("\n\n"+d2);
}
else
if(str.indexOf("-")!=-1) {
int i=str.indexOf("-");
String s1=str.substring(0, i);
String s2=str.substring(i+1,str.length());
Double d=Double.parseDouble(s1.trim());
Double d1=Double.parseDouble(s2.trim());
double d2=d-d1;
jte.setText("\n\n "+d2);
}
else
if(str.indexOf("*")!=-1) {
int i=str.indexOf("*");
String s1=str.substring(0, i);
String s2=str.substring(i+1,str.length());
Double d=Double.parseDouble(s1.trim());
Double d1=Double.parseDouble(s2.trim());
double d2=d*d1;
jte.setText("\n\n "+d2);
}
else
if(str.indexOf("/")!=-1) {
int i=str.indexOf("/");
String s1=str.substring(0, i);
String s2=str.substring(i+1,str.length());
Double d=Double.parseDouble(s1.trim());
Double d1=Double.parseDouble(s2.trim());
double d2=d/d1;
jte.setText("\n\n "+d2);
}
}
}
//---------------------------------------------------------
public static void main(String[] args) {
new ApiHomework();
}
}