java编写一个简易计算器界面,如图1所示,不需要添加任何事件。 - 爱问答

(爱问答)

java编写一个简易计算器界面,如图1所示,不需要添加任何事件。

import java.awt.*;import java.awt.event.*;class Calculate extends Frame implements ActionListener{ TextField t1=new TextField(5);  //第一个操作数文本框 TextField t2=new TextField(5);  //运算符文本框 TextField t3=new TextField(5);  //第一个操作数文本框 TextField t4=new TextField(5);  //结果文本框 Label L1=new Label("="); Button btn=new Button("计算"); public Calculate()  { setLayout(new FlowLayout());  add(t1);add(t2);add(t3);  add(L1);add(t4);add(btn);  btn.addActionListener(this);     //注册动作事件监听者为当前对象  addWindowListener(new WindowAdapter()   {//关闭窗口事件   public void windowClosing(WindowEvent e)    { dispose();         //释放窗口    System.exit(0);   //退出程序   }  }); } public void actionPerformed(ActionEvent e) {     float x,y;         //操作数变量  double result=0;  //结果变量  String op;  try  {                //异常捕获机制   x=Float.parseFloat(t1.getText());   //将字符串数据转换成浮点型数据   y=Float.parseFloat(t3.getText());   op=t2.getText();   if(op.equals("+"))                  //运算符为"+"    result=x+y;   else if(op.equals("-"))            //运算符为"-"    result=x-y;   else if(op.equals("*"))            //运算符为"*"    result=x*y;   else if(op.equals("/"))            //运算符为"/"    result=x/y;   t4.setText(Double.toString(result));  }catch(Exception ee){t4.setText("数据错误");}  //捕获异常,数据错误时,显示信息 }  public static void main(String args[])  {  Calculate mainFrame = new Calculate();  mainFrame.setSize(400, 400);  mainFrame.setTitle("两个数的计算程序");  mainFrame.setVisible(true); }}

import java.awt.*;import java.awt.event.*;class Calculate extends Frame implements ActionListener{ TextField t1=new TextField(5); //第一个操作数文本框 TextField t2=new TextField(5); //运算符文本框 TextField t3=new TextField(5); //第一个操作数文本框 TextField t4=new TextField(5); //结果文本框 Label L1=new Label("="); Button btn=new Button("计算"); public Calculate() { setLayout(new FlowLayout()); add(t1);add(t2);add(t3); add(L1);add(t4);add(btn); btn.addActionListener(this); //注册动作事件监听者为当前对象 addWindowListener(new WindowAdapter() {//关闭窗口事件 public void windowClosing(WindowEvent e) { dispose(); //释放窗口 System.exit(0); //退出程序 } }); } public void actionPerformed(ActionEvent e) { float x,y; //操作数变量 double result=0; //结果变量 String op; try { //异常捕获机制 x=Float.parseFloat(t1.getText()); //将字符串数据转换成浮点型数据 y=Float.parseFloat(t3.getText()); op=t2.getText(); if(op.equals("+")) //运算符为"+" result=x+y; else if(op.equals("-")) //运算符为"-" result=x-y; else if(op.equals("*")) //运算符为"*" result=x*y; else if(op.equals("/")) //运算符为"/" result=x/y; t4.setText(Double.toString(result)); }catch(Exception ee){t4.setText("数据错误");} //捕获异常,数据错误时,显示信息 } public static void main(String args[]) { Calculate mainFrame = new Calculate(); mainFrame.setSize(400, 400); mainFrame.setTitle("两个数的计算程序"); mainFrame.setVisible(true); }}

 

相关标签:java

下一篇:pycarm3.6.2中insrall包失败怎么解决

上一篇:java、编写一个正方形类是Square,

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?