Java简易计算器V1.0-第一个Java GUI项目
来源:互联网 发布:淘宝只能换绑不能解绑 编辑:程序博客网 时间:2024/05/16 16:57
先上效果图:
刚接触Java GUI编程,第一个完成的项目就是这个简易计算器,废话不多说,放上笔者写的代码
/*** Calculator.java* Java简易计算器* @author muyangren907* @Time 2017年4月8日**/import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.BorderLayout;import javax.swing.JButton;import java.awt.Font;import java.awt.Color;import javax.swing.JLabel;import java.awt.event.ActionListener;import java.math.BigDecimal;import java.text.DecimalFormat;import java.awt.event.ActionEvent;import javax.swing.SwingConstants;import java.awt.Dialog.ModalExclusionType;import java.awt.Window.Type;public class Calculator { private JFrame frmJavav; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Calculator window = new Calculator(); window.frmJavav.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Calculator() { initialize(); } /** * Initialize the contents of the frame. */ public class newans { private double ans = 0,num=0; private int operation = 0, decimal = 0,ifequals=0,equals=0; public String formatDecimal(double d, int newScale) { String pattern = "#."; for (int i = 0; i < newScale; i++) { pattern += "0"; } DecimalFormat df = new DecimalFormat(pattern); return df.format(d); } public newans() { } public int getIfequals() { return ifequals; } public void setIfequals(int ifequals) { this.ifequals = ifequals; } public double getNum() { return num; } public void setNum(double num) { this.num = num; } public void setEuqals(int equ){ equals=equ; } public int getEquals(){ return equals; } public void setAns(double num) { ans = num; } public double getAns() { return ans; } public void setOperation(int op) { operation = op; } public int getOperation() { return operation; } public void setDecimal(int xs) { decimal = xs; } public int getDecimal() { return decimal; } } private void initialize() { newans ans = new newans(); frmJavav = new JFrame(); frmJavav.setType(Type.UTILITY); frmJavav.setForeground(Color.DARK_GRAY); frmJavav.setTitle("Java\u8BA1\u7B97\u5668V1.0"); frmJavav.setResizable(false); frmJavav.setBounds(100, 100, 307, 393); frmJavav.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); frmJavav.getContentPane().add(panel, BorderLayout.CENTER); panel.setLayout(null); JLabel lblNewLabel = new JLabel("0"); lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT); lblNewLabel.setBounds(50, 20, 200, 50); panel.add(lblNewLabel); JButton btnNewButton = new JButton("7"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (ans.getOperation() != 0) { if (ans.getDecimal() == 0) { ans.setNum(ans.getNum() * 10 + 7); lblNewLabel.setText("" + (int) ans.getNum()); } else { if (ans.getDecimal() < 16) { ans.setNum(Double.valueOf(ans.formatDecimal( ans.getNum() + 7.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getNum()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getNum()); } } } else { if (ans.getDecimal() == 0) { ans.setAns(ans.getAns() * 10 + 7); lblNewLabel.setText("" + (int) ans.getAns()); } else { if (ans.getDecimal() < 16) { ans.setAns(Double.valueOf(ans.formatDecimal( ans.getAns() + 7.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getAns()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getAns()); } } } } }); btnNewButton.setBackground(Color.LIGHT_GRAY); btnNewButton.setBounds(50, 120, 50, 50); panel.add(btnNewButton); JButton btnNewButton_1 = new JButton("8"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (ans.getOperation() != 0) { if (ans.getDecimal() == 0) { ans.setNum(ans.getNum() * 10 + 8); lblNewLabel.setText("" + (int) ans.getNum()); } else { if (ans.getDecimal() < 16) { ans.setNum(Double.valueOf(ans.formatDecimal( ans.getNum() + 8.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getNum()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getNum()); } } } else { if (ans.getDecimal() == 0) { ans.setAns(ans.getAns() * 10 + 8); lblNewLabel.setText("" + (int) ans.getAns()); } else { if (ans.getDecimal() < 16) { ans.setAns(Double.valueOf(ans.formatDecimal( ans.getAns() + 8.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getAns()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getAns()); } } } } }); btnNewButton_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1.setBounds(100, 120, 50, 50); panel.add(btnNewButton_1); JButton button = new JButton("9"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (ans.getOperation() != 0) { if (ans.getDecimal() == 0) { ans.setNum(ans.getNum() * 10 + 9); lblNewLabel.setText("" + (int) ans.getNum()); } else { if (ans.getDecimal() < 16) { ans.setNum(Double.valueOf(ans.formatDecimal( ans.getNum() + 9.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getNum()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getNum()); } } } else { if (ans.getDecimal() == 0) { ans.setAns(ans.getAns() * 10 + 9); lblNewLabel.setText("" + (int) ans.getAns()); } else { if (ans.getDecimal() < 16) { ans.setAns(Double.valueOf(ans.formatDecimal( ans.getAns() + 9.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getAns()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getAns()); } } } } }); button.setBackground(Color.LIGHT_GRAY); button.setBounds(150, 120, 50, 50); panel.add(button); JButton button_1 = new JButton("4"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (ans.getOperation() != 0) { if (ans.getDecimal() == 0) { ans.setNum(ans.getNum() * 10 + 4); lblNewLabel.setText("" + (int) ans.getNum()); } else { if (ans.getDecimal() < 16) { ans.setNum(Double.valueOf(ans.formatDecimal( ans.getNum() + 4.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getNum()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getNum()); } } } else { if (ans.getDecimal() == 0) { ans.setAns(ans.getAns() * 10 + 4); lblNewLabel.setText("" + (int) ans.getAns()); } else { if (ans.getDecimal() < 16) { ans.setAns(Double.valueOf(ans.formatDecimal( ans.getAns() + 4.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getAns()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getAns()); } } } } }); button_1.setBackground(Color.LIGHT_GRAY); button_1.setBounds(50, 170, 50, 50); panel.add(button_1); JButton button_2 = new JButton("5"); button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (ans.getOperation() != 0) { if (ans.getDecimal() == 0) { ans.setNum(ans.getNum() * 10 + 5); lblNewLabel.setText("" + (int) ans.getNum()); } else { if (ans.getDecimal() < 16) { ans.setNum(Double.valueOf(ans.formatDecimal( ans.getNum() + 5.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getNum()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getNum()); } } } else { if (ans.getDecimal() == 0) { ans.setAns(ans.getAns() * 10 + 5); lblNewLabel.setText("" + (int) ans.getAns()); } else { if (ans.getDecimal() < 16) { ans.setAns(Double.valueOf(ans.formatDecimal( ans.getAns() + 5.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getAns()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getAns()); } } } } }); button_2.setBackground(Color.LIGHT_GRAY); button_2.setBounds(100, 170, 50, 50); panel.add(button_2); JButton button_3 = new JButton("6"); button_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (ans.getOperation() != 0) { if (ans.getDecimal() == 0) { ans.setNum(ans.getNum() * 10 + 6); lblNewLabel.setText("" + (int) ans.getNum()); } else { if (ans.getDecimal() < 16) { ans.setNum(Double.valueOf(ans.formatDecimal( ans.getNum() + 6.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getNum()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getNum()); } } } else { if (ans.getDecimal() == 0) { ans.setAns(ans.getAns() * 10 + 6); lblNewLabel.setText("" + (int) ans.getAns()); } else { if (ans.getDecimal() < 16) { ans.setAns(Double.valueOf(ans.formatDecimal( ans.getAns() + 6.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getAns()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getAns()); } } } } }); button_3.setBackground(Color.LIGHT_GRAY); button_3.setBounds(150, 170, 50, 50); panel.add(button_3); JButton button_4 = new JButton("1"); button_4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (ans.getOperation() != 0) { if (ans.getDecimal() == 0) { ans.setNum(ans.getNum() * 10 + 1); lblNewLabel.setText("" + (int) ans.getNum()); } else { if (ans.getDecimal() < 16) { ans.setNum(Double.valueOf(ans.formatDecimal( ans.getNum() + 1.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getNum()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getNum()); } } } else { if (ans.getDecimal() == 0) { ans.setAns(ans.getAns() * 10 + 1); lblNewLabel.setText("" + (int) ans.getAns()); } else { if (ans.getDecimal() < 16) { ans.setAns(Double.valueOf(ans.formatDecimal( ans.getAns() + 1.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getAns()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getAns()); } } } } }); button_4.setBackground(Color.LIGHT_GRAY); button_4.setBounds(50, 220, 50, 50); panel.add(button_4); JButton button_5 = new JButton("2"); button_5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (ans.getOperation() != 0) { if (ans.getDecimal() == 0) { ans.setNum(ans.getNum() * 10 + 2); lblNewLabel.setText("" + (int) ans.getNum()); } else { if (ans.getDecimal() < 16) { ans.setNum(Double.valueOf(ans.formatDecimal( ans.getNum() + 2.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getNum()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getNum()); } } } else { if (ans.getDecimal() == 0) { ans.setAns(ans.getAns() * 10 + 2); lblNewLabel.setText("" + (int) ans.getAns()); } else { if (ans.getDecimal() < 16) { ans.setAns(Double.valueOf(ans.formatDecimal( ans.getAns() + 2.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getAns()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getAns()); } } } } }); button_5.setBackground(Color.LIGHT_GRAY); button_5.setBounds(100, 220, 50, 50); panel.add(button_5); JButton button_6 = new JButton("3"); button_6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (ans.getOperation() != 0) { if (ans.getDecimal() == 0) { ans.setNum(ans.getNum() * 10 + 3); lblNewLabel.setText("" + (int) ans.getNum()); } else { if (ans.getDecimal() < 16) { ans.setNum(Double.valueOf(ans.formatDecimal( ans.getNum() + 3.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getNum()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getNum()); } } } else { if (ans.getDecimal() == 0) { ans.setAns(ans.getAns() * 10 + 3); lblNewLabel.setText("" + (int) ans.getAns()); } else { if (ans.getDecimal() < 16) { ans.setAns(Double.valueOf(ans.formatDecimal( ans.getAns() + 3.0 / Math.pow(10, ans.getDecimal()), ans.getDecimal()))); lblNewLabel.setText("" + ans.getAns()); ans.setDecimal(ans.getDecimal() + 1); } else { lblNewLabel.setText("" + ans.getAns()); } } } } }); button_6.setBackground(Color.LIGHT_GRAY); button_6.setBounds(150, 220, 50, 50); panel.add(button_6); JButton button_7 = new JButton("0"); button_7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (ans.getOperation() == 0) { if (ans.getDecimal() == 0) { ans.setAns(0); } else { if (ans.getAns() != 0) { ans.setDecimal(ans.getDecimal() + 1); if (Math.round(ans.getAns()) != 0) lblNewLabel.setText(ans.formatDecimal(ans.getAns(), ans.getDecimal() - 1)); else lblNewLabel.setText("0" + ans.formatDecimal(ans.getAns(), ans.getDecimal() - 1)); } else { lblNewLabel.setText("0."); } } } else { if (ans.getDecimal() == 0) { ans.setNum(0); lblNewLabel.setText("0"); } else { if (ans.getNum() != 0) { ans.setDecimal(ans.getDecimal() + 1); if (Math.round(ans.getNum()) != 0) lblNewLabel.setText(ans.formatDecimal(ans.getNum(), ans.getDecimal() - 1)); else lblNewLabel.setText("0" + ans.formatDecimal(ans.getNum(), ans.getDecimal() - 1)); } else { lblNewLabel.setText("0."); } } } } }); button_7.setBackground(Color.LIGHT_GRAY); button_7.setBounds(50, 270, 100, 50); panel.add(button_7); JButton button_8 = new JButton("."); button_8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ans.setDecimal(1); if(ans.getEquals()!=0) { ans.setEuqals(0); ans.setOperation(3); ans.setNum(0); ans.setIfequals(0); if (ans.getNum() != 0) { ans.setDecimal(ans.getDecimal() + 1); if (Math.round(ans.getNum()) != 0) lblNewLabel.setText(ans.formatDecimal(ans.getNum(), ans.getDecimal() - 1)); else lblNewLabel.setText("0" + ans.formatDecimal(ans.getNum(), ans.getDecimal() - 1)); } else { lblNewLabel.setText("0."); } } else{ if (ans.getOperation() == 0) lblNewLabel.setText("" + (int) ans.getAns() + "."); else lblNewLabel.setText("" + (int) ans.getNum() + "."); } } }); button_8.setBackground(Color.LIGHT_GRAY); button_8.setBounds(150, 270, 50, 50); panel.add(button_8); JButton btnAc = new JButton("AC"); btnAc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ans.setAns(0); ans.setDecimal(0); ans.setEuqals(0); ans.setOperation(0); ans.setNum(0); lblNewLabel.setText("0"); ans.setIfequals(0); } }); btnAc.setBackground(Color.LIGHT_GRAY); btnAc.setBounds(50, 70, 50, 50); panel.add(btnAc); JButton button_10 = new JButton("+/-"); button_10.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ans.setAns(-ans.getAns()); ans.setOperation(0); } }); button_10.setBackground(Color.LIGHT_GRAY); button_10.setFont(new Font("宋体", Font.PLAIN, 10)); button_10.setBounds(100, 70, 50, 50); panel.add(button_10); JButton button_11 = new JButton("%"); button_11.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ans.setAns(BigDecimal.valueOf(ans.getAns()).divide(BigDecimal.valueOf(100)).doubleValue()); ans.setOperation(0); } }); button_11.setBackground(Color.LIGHT_GRAY); button_11.setBounds(150, 70, 50, 50); panel.add(button_11); JButton button_9 = new JButton("\u00F7"); button_9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ans.setDecimal(0); ans.setNum(0); ans.setOperation(4); } }); button_9.setBackground(Color.LIGHT_GRAY); button_9.setBounds(200, 70, 50, 50); panel.add(button_9); JButton button_12 = new JButton("\u00D7"); button_12.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ans.setDecimal(0); ans.setNum(0); ans.setOperation(3); } }); button_12.setBackground(Color.LIGHT_GRAY); button_12.setBounds(200, 120, 50, 50); panel.add(button_12); JButton button_13 = new JButton("\u2014"); button_13.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ans.setDecimal(0); ans.setNum(0); ans.setOperation(2); } }); button_13.setBackground(Color.LIGHT_GRAY); button_13.setBounds(200, 170, 50, 50); panel.add(button_13); JButton button_14 = new JButton("+"); button_14.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ans.setDecimal(0); ans.setNum(0); ans.setOperation(1); } }); button_14.setBackground(Color.LIGHT_GRAY); button_14.setBounds(200, 220, 50, 50); panel.add(button_14); JButton button_15 = new JButton("="); button_15.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ans.setEuqals(1); // 精确计算 switch (ans.getOperation()) { case 1: ans.setAns(BigDecimal.valueOf(ans.getAns()).add(BigDecimal.valueOf(ans.getNum())).doubleValue()); break; case 2: ans.setAns( BigDecimal.valueOf(ans.getAns()).subtract(BigDecimal.valueOf(ans.getNum())).doubleValue()); break; case 3: ans.setAns( BigDecimal.valueOf(ans.getAns()).multiply(BigDecimal.valueOf(ans.getNum())).doubleValue()); break; case 4:{ if(ans.getNum()==0){ ans.setAns(0); ans.setDecimal(0); ans.setEuqals(0); ans.setIfequals(1); }else{ ans.setAns(BigDecimal.valueOf(ans.getAns()).divide(BigDecimal.valueOf(ans.getNum()),16,BigDecimal.ROUND_HALF_UP).doubleValue()); } };break; default: break; } if (ans.getAns() == Math.round(ans.getAns())) { ans.setDecimal(0); } else { } if(ans.getIfequals()!=0){ lblNewLabel.setText("错误"); } else{ if (ans.getAns() == Math.round(ans.getAns())) lblNewLabel.setText("" + (int) ans.getAns()); else lblNewLabel.setText("" + ans.getAns()); } } }); button_15.setBackground(Color.LIGHT_GRAY); button_15.setBounds(200, 270, 50, 50); panel.add(button_15); }}
源码及打包生成的jar及exe文件均已上传至网盘(1.0为本计算器)
链接: https://pan.baidu.com/s/1skQnqhf
密码: 牛B
仅供交流,引用请注明作者
0 0
- Java简易计算器V1.0-第一个Java GUI项目
- Java GUI做简易计算器
- java学习的第一个成果 一 简易的计算器
- 第一个Android项目--简易计算器的设计与实现
- Java实战项目__简易计算器
- 第一个项目-计算器
- java简单计算器Gui
- java GUI计算器
- Java GUI计算器实现
- 我的第一个JAVA计算器
- 简易Java控制台计算器
- Java简易计算器
- Java 简易计算器程序
- 一个Java简易计算器
- Java—简易计算器
- JAVA编写简易计算器
- 简易计算器(JAVA)
- JAVA实现简易计算器
- 统计整数位数
- win10环境下在android studio的模拟器中运行react native。
- sizeof(C# 参考)
- 重写、覆盖、重载、多态几个概念的区别分析
- 微软2017年预科生计划在线编程笔试第二场B题Diligent Robots
- Java简易计算器V1.0-第一个Java GUI项目
- 探索文件格式的权威指南(01)
- Android Studio添加ShadowSocks代理
- cookie和session两种会话技术
- 线程的介绍和创建方式
- spring、springmvc和mybaites在maven整合
- 外部中断计数器
- 用Python绘制七段数码管
- Android 视频播放器SurfaceView+Mediaplayer