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(); } }
...全文
66 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
代码下载链接: https://pan.quark.cn/s/a8fbca3925b4 《鸿蒙OS开发环境构建》指南系统性地阐述了配置和筹备鸿蒙OS开发所需的各种工具和条件的具体方法。鸿蒙OS,亦称HarmonyOS,是由华为研发的一款面向全场景的分布式操作系统,其目标是提供跨平台、多设备间无缝协作的使用体验。本指南涉及了从Linux服务器到Windows工作站的完整开发流程。指南提及了MobaXterm,这是一款用于连接Linux源码服务器的软件,使得开发人员能够在Windows环境远程访问Linux服务器。同,HiTool作为烧录工具,用于将编译后的系统镜像写入开发板。IPOP.EXE则是一款串口终端软件,用于执行串行通信和调试任务。Embedded Studio用于开发设备驱动程序,而DevEco Studio是华为提供的图形化应用程序开发平台,支持C/C++语言,拥有代码编辑、编译、烧录和调试功能,被视为OpenHarmony智能设备开发者的首选集成开发环境。在硬件配置方面,指南列出了必需的设备,包括Linux服务器(推荐Ubuntu 16.04及以上版本),Windows工作台(兼容XP/7/10),以及Hi3518EV300 IoT Camera单板。开发期间,Windows工作台通过USB线与单板相连接,以实现数据传输。此外,为了开展开发工作,还需要安装putty、IPOP、tftp服务器等辅助软件,以及HiTool用于烧录操作。在软件系统要求方面,Linux服务器需要安装bash、Python3.7+、gn、ninja、LLVM等构建工具,这些工具对于生成和执行编译脚本具有关键作用。在Windows工作台上,建议采用Visual Studi...

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧