怎样用Java编写一个简单的计算器
来源:互联网 发布:adobe reader百度软件 编辑:程序博客网 时间:2024/05/16 10:13
我是自己纯手工用Java编写的计算器
//filename:JiShuanimport java.awt.Color;import java.awt.EventQueue;import java.awt.Image;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextField;import java.awt.BorderLayout;import javax.swing.JPanel;import java.awt.GridLayout;import javax.swing.SwingConstants;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.Toolkit;public class JiShuan {private JFrame frame;private JTextField textShow;private JButton[] btn = {new JButton("1"),new JButton("2"),new JButton("3"),new JButton("+"),new JButton("4"),new JButton("5"),new JButton("6"),new JButton("-"),new JButton("7"),new JButton("8"),new JButton("9"),new JButton("*"),new JButton("0"),new JButton("."),new JButton("="),new JButton("/")};/** * Launch the application. */public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {JiShuan window = new JiShuan();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/** * Create the application. */public JiShuan() {initialize();}/** * Initialize the contents of the frame. */private MyButtonActionListener listener = new MyButtonActionListener();private void initialize() {frame = new JFrame();frame.setIconImage(Toolkit.getDefaultToolkit().getImage(JiShuan.class.getResource("/image/1.jpg")));//Image imageIcon = new ImageIcon(getClass().getClassLoader.getResource(imagePath)).getImage();//Image image=new ImageIcon(Toolkit.getDefaultToolkit().getImage(JiShuan.class.getResource("/Images/Desert.jpg"))).getImage();//frame.setIconImage(imageIcon);//frame.setIconImage(Toolkit.getDefaultToolkit().getImage(JiShuan.class.getResource("/Image/1.jpg")));frame.setTitle("\u8BA1\u7B97\u5668");frame.setBounds(100, 120, 242, 323);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(null);textShow = new JTextField();textShow.setFont(new Font("宋体", Font.PLAIN, 40));textShow.setText("0");textShow.setBounds(10, 10, 205, 55);frame.getContentPane().add(textShow);textShow.setColumns(10);textShow.setBackground(Color.LIGHT_GRAY); //设置文本框颜色textShow.setHorizontalAlignment(SwingConstants.RIGHT);//设置文本的内容右对齐JPanel panel = new JPanel();panel.setBounds(10, 75, 205, 197);frame.getContentPane().add(panel);panel.setLayout(new GridLayout(4, 4, 0, 0));for(int i=0;i<16;i++){btn[i].addActionListener(listener);panel.add(btn[i]);}}String str="";double num,sum=0.0;char last;int f=1;class MyButtonActionListener implements ActionListener{public void actionPerformed(ActionEvent e) {JButton button = (JButton)e.getSource();char sign = button.getText().charAt(0);switch(sign){case '0':str = str+"0";textShow.setText(str);break;case '1':str = str+"1";textShow.setText(str);break;case '2':str = str+"2";textShow.setText(str);break;case '3':str = str+"3";textShow.setText(str);break;case '4':str = str+"4";textShow.setText(str);break;case '5':str = str+"5";textShow.setText(str);break;case '6':str = str+"6";textShow.setText(str);break;case '7':str = str+"7";textShow.setText(str);break;case '8':str = str+"8";textShow.setText(str);break;case '9':str = str+"9";textShow.setText(str);break;case '+':textShow.setText("");last='+';if(f!=0){f=0;sum=Double.parseDouble(str);}elsesum=sum+Double.parseDouble(str);textShow.setText(sum+"");str="";break;case '-':textShow.setText("");last='-';if(f!=0){f=0;sum=Double.parseDouble(str);}elsesum=sum-Double.parseDouble(str);textShow.setText(sum+"");str="";break;case '*':textShow.setText("");last='*';if(f!=0){f=0;sum=Double.parseDouble(str);}elsesum=sum*Double.parseDouble(str);textShow.setText(sum+"");str="";break;case '/':textShow.setText("");last='/';if(f!=0){f=0;sum=Double.parseDouble(str);}elsesum=sum/Double.parseDouble(str);textShow.setText(sum+"");str="";break;case '=':switch(last){case '+':sum=sum+Double.parseDouble(str);str="";break;case '-':sum=sum-Double.parseDouble(str);str="";break;case '*':sum=sum*Double.parseDouble(str);str="";break;case '/':sum=sum/Double.parseDouble(str);str="";break;}textShow.setText(sum+"");break;}}}private Color Color(int i, int j, int k) {// TODO 自动生成的方法存根return null;}}
阅读全文
0 0
- 怎样用Java编写一个简单的计算器
- 怎样用Java写一个简单的计算器?
- 用java编写一个简单的计算器
- 使用Java AWT编写一个简单的计算器
- 用java编写一个简单计算器
- 用java编写一个简单计算器
- 自己编写的一个简单的计算器
- java编写一个计算器
- Java编写的简单的计算器
- 用Java swing 编写简单的计算器
- 利用Java语言编写简单的计算器
- 用Java编写的一个小计算器
- Java语言编写计算器(简单的计算器)
- 怎样用java写一个简单的文件复制程序
- 用java编写一个计算器
- javaSE基础编程——编写一个简单的计算器
- 如何使用JS编写一个简单的计算器
- 用C语言编写一个简单的“+”“”-“”*“/”计算器
- 仿ios底部菜单栏
- 原生js调用json方法
- Android Material Design动画 Animated Vector Drawables|矢量图片动画
- Unity鼠标点击删除Mesh中的三角面
- [bigdata-108] spring-cloud-04 分布式服务的feign消费者
- 怎样用Java编写一个简单的计算器
- 大数据高效复制的处理案例分析总结
- 什么是数据库散列存储?
- 动态规划训练9 [Brackets POJ
- Libev 官方文档学习笔记
- android 调用网络api
- RSSI相关概念
- VPC最佳实践(六):业务如何从经典网络平滑迁移到VPC
- Exception setting properties value with CFLIB