JAVA学习笔记---简易计算器完整功能
来源:互联网 发布:社保报盘软件下载 编辑:程序博客网 时间:2024/06/07 08:34
初学Java,通过学习布局和事件监听做出一个简易计算器。实现过程还不算太难,但是有的细节需要注意,放上来希望和大家一起学习进步。
package example;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class ui {public String str1="",opt;private JTextField display;private double first,last,answer;public ui(){init();}public void init(){ //计算器布局JFrame f=new JFrame("计算器");f.setLayout(new BorderLayout()); //边框布局f.add(creatpane1(),BorderLayout.NORTH);//布局1f.add(creatpane2(),BorderLayout.CENTER);//布局2f.setSize(300,300); f.setLocation(400,200); f.setVisible(true);}public JPanel creatpane1(){JPanel jp1=new JPanel();jp1.setLayout(new GridLayout());display = new JTextField("0");jp1.add(display);return jp1;}private JPanel creatpane2(){JPanel jp2=new JPanel();jp2.setLayout(new GridLayout(4,4));//网格布局JButton button1=new JButton("1");JButton button2=new JButton("2");JButton button3=new JButton("3");JButton add=new JButton("+");JButton button4=new JButton("4");JButton button5=new JButton("5");JButton button6=new JButton("6");JButton sub=new JButton("-");JButton button7=new JButton("7");JButton button8=new JButton("8");JButton button9=new JButton("9");JButton mul=new JButton("*");JButton floa=new JButton(".");JButton button0=new JButton("0");JButton equal=new JButton("=");JButton div=new JButton("/");jp2.add(button1);jp2.add(button2);jp2.add(button3);jp2.add(add);jp2.add(button4);jp2.add(button5);jp2.add(button6);jp2.add(sub);jp2.add(button7);jp2.add(button8);jp2.add(button9);jp2.add(mul);jp2.add(floa);jp2.add(button0);jp2.add(equal);jp2.add(div);button1.addActionListener(new ActionListener(){ //为按钮添加监听事件 , 下同。public void actionPerformed(ActionEvent arg0) {str1=str1+button1.getText();display.setText(str1);}});button2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {str1=str1+button2.getText();display.setText(str1);}});button3.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {str1=str1+button3.getText();display.setText(str1);}});button4.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {str1=str1+button4.getText();display.setText(str1);}});button5.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {str1=str1+button5.getText();display.setText(str1);}});button6.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {str1=str1+button6.getText();display.setText(str1);}});button7.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {str1=str1+button7.getText();display.setText(str1);}});button8.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {str1=str1+button8.getText();display.setText(str1);}});button9.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {str1=str1+button9.getText();display.setText(str1);}});button0.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {str1=str1+button0.getText();display.setText(str1);}});floa.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {str1=str1+".";display.setText(str1);}});add.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {opt="+";setopt();setshow("");str1="";getopt();}});sub.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {opt="-"; //当前输入的操作setopt();//将操作符之前的数保存起来setshow("");//清空显示屏str1="";//清空字符串为了输入下一个数字getopt();//保存操作符}});mul.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {opt="*";setopt();setshow("");str1="";getopt();}});div.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {opt="/";setopt();setshow("");str1="";getopt();}});equal.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) {setanswer();//进行运算str1="";//清空字符串,为下次运算准备}});return jp2;}public Double getshow(String str) { //将显示屏上的字符串返回成double类型return Double.valueOf(str);}public void setshow(String str){ //将数据发送到显示屏display.setText(str);}public String getopt(){ return this.opt;}public void setopt(){first=getshow(str1);}public void setanswer(){ last=getshow(str1);if("+".equals(getopt()) ) { answer=first+last; setshow(String.valueOf(answer)); first=answer;//为下次输入操作符做准备 } else if("-".equals(getopt()) ){ answer=first-last; setshow(String.valueOf(answer)); first=answer;//为下次输入操作符做准备 } else if("*".equals(getopt()) ){ answer=first*last; setshow(String.valueOf(answer)); first=answer;//为下次输入操作符做准备 } else if("/".equals(getopt())){ if(last==0) setshow("输入有误"); else { answer=first/last; setshow(String.valueOf(answer)); first=answer;//为下次输入操作符做准备 } }}public static void main(String[] args){new ui();}}
0 0
- JAVA学习笔记---简易计算器完整功能
- JAVA学习笔记---简易计算器之图形界面
- java实现简易计算器完整代码
- Java 实现简易计算器完整代码
- js实现简易计算器功能
- 简易Java控制台计算器
- Java简易计算器
- Java 简易计算器程序
- 一个Java简易计算器
- Java—简易计算器
- JAVA编写简易计算器
- 简易计算器(JAVA)
- JAVA实现简易计算器
- java 简易计算器代码
- java简易计算器
- java实现简易计算器
- java简易计算器
- java简易计算器
- android fragment_main.xml 注意问题
- CSS设置字体和文字样式
- HGE引擎的粒子系统
- 第十二周项目二 OJ平台-刑警的射击成绩
- java实践源码--哈弗曼树
- JAVA学习笔记---简易计算器完整功能
- Delphi中ShellExecute的妙用
- java接口 实例化
- Cordic 算法入门
- @CL之家caoliu之家dizhi
- MongoDB(二)——安装配置了解
- 深入理解指针—>指针函数与函数指针的区别
- java 实践源码--并查集
- HDU 3812