Java语言编写计算器(简单的计算器)
2015-07-18 15:29
696 查看
Java编写的一个简单计算器,本人还比较菜,只能这样了,有点代码冗余,不能连续计算。import javax.swing.*;import java.awt.*;import java.awt.event.*;public class calculate extends JFrame {JButton[] b = new JButton[10];JPanel panel1 = new JPanel(); // 实例化面板JPanel panel2 = new JPanel();JPanel panel3 = new JPanel();JTextField answer=new JTextField(10);JButton clear=new JButton("清除");JButton equal=new JButton("=");JButton point=new JButton(".");JButton plus=new JButton("+");JButton minus=new JButton("-");JButton multi=new JButton("×");JButton division=new JButton("÷");String foreText=new String("");String backText=new String("");String s=new String();String s1=new String();Object op=new String();Object o=new String();double d, dFore, dBack;int i=9;public static void main(String[] args) {// TODO Auto-generated method stubJFrame calculator =new calculate();calculator.setVisible(true);}public calculate(){setTitle("计算器");setSize(300,300);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Toolkit kit = Toolkit.getDefaultToolkit();Dimension screenSize = kit.getScreenSize();//获取屏幕分辨率setLocation(screenSize.width/4,screenSize.height/4);//位置for ( i =9; i>=0; i--) {b[i] = new JButton(Integer.toString(i));// 实例化数字按钮b[i].addActionListener(new MyListener1());panel2.add(b[i]); // 增加按钮到面板}setLayout(new FlowLayout()); // 设置布局管理器panel1.setLayout(new FlowLayout());panel2.setLayout(new GridLayout(4,3,10,8));panel3.setLayout(new GridLayout(4,1,8,12));clear.setFont(new Font("隶书",Font.PLAIN,15));clear.setForeground(Color.RED);clear.addActionListener(new MyListener1());equal.setForeground(Color.RED);equal.addActionListener(new MyListener1());plus.setForeground(Color.RED);plus.addActionListener(new MyListener1());minus.setForeground(Color.RED);minus.addActionListener(new MyListener1());multi.setForeground(Color.RED);multi.addActionListener(new MyListener1());division.setForeground(Color.RED);division.addActionListener(new MyListener1());equal.setFont(new Font("隶书",Font.PLAIN,20));plus.setFont(new Font("隶书",Font.PLAIN,18));minus.setFont(new Font("隶书",Font.PLAIN,18));multi.setFont(new Font("隶书",Font.PLAIN,18));division.setFont(new Font("隶书",Font.PLAIN,18));point.addActionListener(new MyListener1());panel1.add(answer);panel1.add(clear);panel2.add(point);panel2.add(equal);panel3.add(plus);panel3.add(minus);panel3.add(multi);panel3.add(division);add(panel1);add(panel2);add(panel3);}class MyListener1 implements ActionListener{public void actionPerformed(ActionEvent e){for(int i=0;i<=9;i++) {if(e.getSource()==b[i]||e.getSource()==point){answer.setText("");if(e.getSource()==point){s=foreText+".";answer.setText(s);}else{if(s!=""){answer.setText(s+b[i].getText());foreText=s+b[i].getText();//此处应当修改,}else{answer.setText(foreText+b[i].getText());foreText=foreText+b[i].getText();}}}}if(e.getSource()==clear){foreText="";s="";answer.setText("");}if(e.getSource()==plus||e.getSource()==minus||e.getSource()==multi||e.getSource()==division){dFore=Double.parseDouble(foreText);s="";foreText="";answer.setText("");op=e.getSource();}if(e.getSource()==equal){dBack=Double.parseDouble(foreText);foreText="";answer.setText("");if(op==plus){d=dFore+dBack;}if(op==minus){d=dFore-dBack;}if(op==multi){d=dFore*dBack;}if(op==division){d=dFore/dBack;}answer.setText(""+d);}}}}运行截图
相关文章推荐
- Java语言编写计算器(简单的计算器)
- struts拦截器
- ubuntu 下解决“no java virtual machine was found after searching the following locations:” 方法
- Java NIO 系列教程
- Java中关键字final、static使用总结
- Java基础---Proxy 代理
- MyEclipse10搭建Strust2开发环境(2)
- Java 文件操作
- 分布式消息队列中间件系列研究之阿堂教程(基础篇-Local模式)
- Struts2输入校验之validate输入校验方式
- [LeetCode][Java] Search in Rotated Sorted Array II
- java interface的两个经典用法
- java我的总结——一些基础知识和代码
- Spring的注解配置和XML配置的比较
- Java上转型对象
- [LeetCode][Java] Remove Duplicates from Sorted Array II
- MyEclipse10搭建Strust2开发环境
- java疑问:?? 线程
- windows 下构建 Eclipse gdb 调试平台调试arm程序
- java错误提示------No enclosing instance of type Demo10_1 is accessible