好友
阅读权限 25
听众
最后登录 1970-1-1
本帖最后由 cary_云飞扬 于 2019-4-21 15:23 编辑
时光飞逝,一晃三年过去了,2016年的时候初学java,略有小成,每天下班之后,喝着小酒,敲着Java代码,乐此不疲。现在因为一些其他原因,不再研究java了,改学C++了。闲言少叙,我把当年花一个月时间写的一个计算器的源码贴出来,给初学编程的朋友当作一个小例子吧,大神们就请绕道吧。
1、计算器主界面代码:
[Java] 纯文本查看 复制代码
package com.cary.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import com.cary.service.Operation;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.Toolkit;
public class CalculatorUI extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JButton button_gen;
private JButton button_left;
private JButton button_right;
private JButton button_xy;
private JButton btnCe;
private JButton button_seven;
private JButton button_four;
private JButton button_one;
private JButton button_LF;
private JButton btnC;
private JButton button_eight;
private JButton button_five;
private JButton button_two;
private JButton button_zero;
private JButton button_PF;
private JButton button_nine;
private JButton button_six;
private JButton button_three;
private JButton button_point;
private JButton button_chu;
private JButton button_cheng;
private JButton button_jian;
private JButton button_jia;
private JButton button_deng;
private boolean flag = false;
private JTextField textField;
private JTextField textField_1;
private JMenuItem menuItem_0;
private JMenuItem menuItem_2;
private JMenuItem menuItem_1;
//添加监听函数
private void addActionListeners() {
btnCe.addActionListener(this);
button_cheng.addActionListener(this);
button_chu.addActionListener(this);
btnC.addActionListener(this);
button_deng.addActionListener(this);
button_eight.addActionListener(this);
button_five.addActionListener(this);
button_four.addActionListener(this);
button_gen.addActionListener(this);
button_jia.addActionListener(this);
button_jian.addActionListener(this);
button_left.addActionListener(this);
button_LF.addActionListener(this);
button_nine.addActionListener(this);
button_one.addActionListener(this);
button_PF.addActionListener(this);
button_point.addActionListener(this);
button_right.addActionListener(this);
button_seven.addActionListener(this);
button_six.addActionListener(this);
button_three.addActionListener(this);
button_two.addActionListener(this);
button_xy.addActionListener(this);
button_zero.addActionListener(this);
}
/**
* Create the frame.
*/
@SuppressWarnings("static-access")
public CalculatorUI() {
setTitle("飞扬计算器--普通型");
setIconImage(Toolkit.getDefaultToolkit().getImage("image\\jsq1.png"));
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 580, 800);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu_0 = new JMenu("计算器");
menuBar.add(menu_0);
menuItem_0 = new JMenuItem("计算器(工程经济)");
menuItem_0.addActionListener(this);
menu_0.add(menuItem_0);
JMenu menu_2 = new JMenu("帮助");
menuBar.add(menu_2);
menuItem_1 = new JMenuItem("使用说明");
menuItem_1.addActionListener(this);
menu_2.add(menuItem_1);
JMenu menu_3 = new JMenu("关于");
menuBar.add(menu_3);
menuItem_2 = new JMenuItem("关于计算器");
menuItem_2.addActionListener(this);
menu_3.add(menuItem_2);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel_01 = new JPanel();
panel_01.setBorder(new TitledBorder(null, "\u663E\u793A\u533A", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_01.setBounds(14, 13, 534, 151);
contentPane.add(panel_01);
panel_01.setLayout(null);
textField = new JTextField();
textField.setFont(new Font("楷体", Font.PLAIN, 23));
textField.setEditable(false);
textField.setBounds(14, 25, 506, 58);
textField.setColumns(10);
textField.setHorizontalAlignment(textField.RIGHT);
textField.setText("0");
flag = true;
panel_01.add(textField);
textField_1 = new JTextField();
textField_1.setFont(new Font("楷体", Font.PLAIN, 25));
textField_1.setEditable(false);
textField_1.setColumns(10);
textField_1.setBounds(14, 80, 506, 58);
textField_1.setHorizontalAlignment(textField_1.RIGHT);
textField_1.setText("0");
panel_01.add(textField_1);
JPanel panel_02 = new JPanel();
panel_02.setBorder(new TitledBorder(null, "\u529F\u80FD\u533A", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_02.setBounds(14, 177, 534, 563);
contentPane.add(panel_02);
panel_02.setLayout(null);
button_gen = new JButton("√ ̄");
button_gen.setFont(new Font("宋体", Font.PLAIN, 15));
button_gen.setBounds(51, 36, 69, 51);
panel_02.add(button_gen);
button_left = new JButton("(");
button_left.setFont(new Font("宋体", Font.PLAIN, 25));
button_left.setBounds(171, 36, 69, 51);
panel_02.add(button_left);
button_right = new JButton(")");
button_right.setFont(new Font("宋体", Font.PLAIN, 25));
button_right.setBounds(291, 36, 69, 51);
panel_02.add(button_right);
button_xy = new JButton("x^y");
button_xy.setFont(new Font("宋体", Font.PLAIN, 22));
button_xy.setBounds(411, 36, 69, 51);
panel_02.add(button_xy);
btnCe = new JButton("CE");
btnCe.setFont(new Font("宋体", Font.PLAIN, 25));
btnCe.setBounds(51, 123, 69, 51);
panel_02.add(btnCe);
button_seven = new JButton("7");
button_seven.setFont(new Font("宋体", Font.PLAIN, 25));
button_seven.setBounds(51, 210, 69, 51);
panel_02.add(button_seven);
button_four = new JButton("4");
button_four.setFont(new Font("宋体", Font.PLAIN, 25));
button_four.setBounds(51, 297, 69, 51);
panel_02.add(button_four);
button_one = new JButton("1");
button_one.setFont(new Font("宋体", Font.PLAIN, 25));
button_one.setBounds(51, 384, 69, 51);
panel_02.add(button_one);
button_LF = new JButton("x^3");
button_LF.setFont(new Font("宋体", Font.PLAIN, 22));
button_LF.setBounds(51, 471, 69, 51);
panel_02.add(button_LF);
btnC = new JButton("C");
btnC.setFont(new Font("宋体", Font.PLAIN, 24));
btnC.setBounds(171, 123, 69, 51);
panel_02.add(btnC);
button_eight = new JButton("8");
button_eight.setFont(new Font("宋体", Font.PLAIN, 25));
button_eight.setBounds(171, 210, 69, 51);
panel_02.add(button_eight);
button_five = new JButton("5");
button_five.setFont(new Font("宋体", Font.PLAIN, 25));
button_five.setBounds(171, 297, 69, 51);
panel_02.add(button_five);
button_two = new JButton("2");
button_two.setFont(new Font("宋体", Font.PLAIN, 25));
button_two.setBounds(171, 384, 69, 51);
panel_02.add(button_two);
button_zero = new JButton("0");
button_zero.setFont(new Font("宋体", Font.PLAIN, 25));
button_zero.setBounds(171, 471, 69, 51);
panel_02.add(button_zero);
button_PF = new JButton("x^2");
button_PF.setFont(new Font("宋体", Font.PLAIN, 22));
button_PF.setBounds(291, 123, 69, 51);
panel_02.add(button_PF);
button_nine = new JButton("9");
button_nine.setFont(new Font("宋体", Font.PLAIN, 25));
button_nine.setBounds(291, 210, 69, 51);
panel_02.add(button_nine);
button_six = new JButton("6");
button_six.setFont(new Font("宋体", Font.PLAIN, 25));
button_six.setBounds(291, 297, 69, 51);
panel_02.add(button_six);
button_three = new JButton("3");
button_three.setFont(new Font("宋体", Font.PLAIN, 25));
button_three.setBounds(291, 384, 69, 51);
panel_02.add(button_three);
button_point = new JButton(".");
button_point.setFont(new Font("宋体", Font.PLAIN, 25));
button_point.setBounds(291, 471, 69, 51);
panel_02.add(button_point);
button_chu = new JButton("÷");
button_chu.setFont(new Font("宋体", Font.PLAIN, 25));
button_chu.setBounds(411, 123, 69, 51);
panel_02.add(button_chu);
button_cheng = new JButton("×");
button_cheng.setFont(new Font("宋体", Font.PLAIN, 25));
button_cheng.setBounds(411, 210, 69, 51);
panel_02.add(button_cheng);
button_jian = new JButton("-");
button_jian.setFont(new Font("宋体", Font.PLAIN, 25));
button_jian.setBounds(411, 297, 69, 51);
panel_02.add(button_jian);
button_jia = new JButton("+");
button_jia.setFont(new Font("宋体", Font.PLAIN, 25));
button_jia.setBounds(411, 384, 69, 51);
panel_02.add(button_jia);
button_deng = new JButton("=");
button_deng.setFont(new Font("宋体", Font.PLAIN, 25));
button_deng.setBounds(411, 471, 69, 51);
panel_02.add(button_deng);
this.addActionListeners();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==btnCe) {
textField.setText("");//清空
}else if (e.getSource()==button_cheng) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"×");
}else if (e.getSource()==button_chu) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"÷");
}else if (e.getSource()==btnC) {
if (!textField.getText().isEmpty()) {
textField.setText(textField.getText().substring(0, textField.getText().length()-1));
}
}else if (e.getSource()==button_deng) {
String expression = textField.getText().replace("√ ̄", "√").replace("×", "*").replace("÷", "/");
Operation op = new Operation();
String result = null;
try {
result = op.getResult(expression);
} catch (Exception e1) {
if (e1.toString().contains("java.lang.NumberFormatException")) {
result = "输入有误,无法计算";
}
else if (e1.toString().equals("java.lang.ArithmeticException: / by zero")) {
result = "除数不能为0";
}else if (e1.toString().contains("java.lang.IndexOutOfBoundsException")) {
result = "输入有误,无法计算";
}else {
result = "错误,无法计算";
}
e1.printStackTrace();
}
textField_1.setText(result);
flag = true;
}else if (e.getSource()==button_eight) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"8");
}else if (e.getSource()==button_five) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"5");
}else if (e.getSource()==button_four) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"4");
}else if (e.getSource()==button_gen) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"√ ̄");
}else if (e.getSource()==button_jia) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"+");
}else if (e.getSource()==button_jian) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"-");
}else if (e.getSource()==button_left) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"(");
}else if (e.getSource()==button_LF) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"^3");
if (textField.getText().startsWith("^")) {
textField.setText("0^");
}
}else if (e.getSource()==button_nine) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"9");
}else if (e.getSource()==button_one) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"1");
}else if (e.getSource()==button_PF) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"^2");
if (textField.getText().startsWith("^")) {
textField.setText("0^");
}
}else if (e.getSource()==button_point) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+".");
if (textField.getText().startsWith(".")) {
textField.setText("0.");
}
}else if (e.getSource()==button_right) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+")");
}else if (e.getSource()==button_seven) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"7");
}else if (e.getSource()==button_six) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"6");
}else if (e.getSource()==button_three) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"3");
}else if (e.getSource()==button_two) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"2");
}else if (e.getSource()==button_xy) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"^");
if (textField.getText().startsWith("^")) {
textField.setText("0^");
}
}else if (e.getSource()==button_zero) {
if (flag) {
textField.setText("");
flag = false;
}
textField.setText(textField.getText()+"0");
}
else if (e.getSource()==menuItem_0) {
this.dispose();
FormulaUI frame = new FormulaUI();
frame.setLocation(this.getX(), this.getY());
frame.setVisible(true);
}
else if (e.getSource()==menuItem_1) {
// this.dispose();
Help help = new Help();
help.setLocation(this.getX(), this.getY()+50);
help.setVisible(true);
}
else if (e.getSource()==menuItem_2) {
// this.dispose();
About frame = new About();
frame.setLocation(this.getX(), this.getY()+50);
frame.setVisible(true);
}
}
}
2、工程经济计算器界面
[Java] 纯文本查看 复制代码
package com.cary.ui;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.cary.service.Operation;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.Toolkit;
public class FormulaUI extends JFrame implements ItemListener,ActionListener,MouseListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField_0;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JButton button_0;
private JLabel label_4;
private JLabel label_3;
private JLabel label_2;
private JLabel label_1;
private JComboBox<String> comboBox_0;
private JComboBox<String> comboBox_1;
private String a;
private String b;
private String c;
private Operation op;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
private JButton button_1;
private JLabel label_7;
private JLabel label_8;
private JLabel label_9;
private String d;
private String f;
private String g;
private JComboBox<String> comboBox_2;
private JLabel label_11;
private JTextField textField_8;
private JTextField textField_9;
private JTextField textField_10;
private JTextField textField_11;
private JTextField textField_12;
private JLabel label_10;
private JLabel label_12;
private JLabel label_13;
private JLabel label_14;
private JLabel label_15;
private JButton button_2;
private JLabel label_6;
String[] lixi = {"请选择相应公式进行计算","一次支付终值计算(已知P求F)","一次支付现值计算(已知F求P)","等额支付终值计算(已知A求F)"
,"等额支付现值计算(已知A求P)","偿债年金计算公式(已知F求A)","资金回收计算公式(已知P求A)","实际利率计算公式(已知r求i)"};
String[] jingji = {"请选择相应公式进行计算","总投资收益率","资本金净利润率","利息备付率","偿债备付率","资产负债率","流动比率",
"速动比率","净资产收益率","总资产净利率","总资产周转率","流动资产周转率","资产负债率","资金成本率","经济订货量","低劣化值法经济寿命"};
String[] caiwu = {"请选择相应公式进行计算","附加率法求租金","经营成本计算(1)","经营成本计算(2)","产销量表示的盈亏平衡点","敏感度系数计算","基本预备费",
"材料基价"};
private boolean flag = false;
private String m;
private String n;
private String o;
private String p;
private JMenuItem menuItem_00;
private JMenuItem menuItem_02;
private JMenuItem menuItem_01;
/**
* Create the frame.
*/
public FormulaUI() {
setTitle("飞扬计算器--工程经济");
setIconImage(Toolkit.getDefaultToolkit().getImage("image\\jsq1.png"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 590, 800);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("计算器");
menuBar.add(menu);
menuItem_00 = new JMenuItem("计算器(普通)");
menuItem_00.addActionListener(this);
menu.add(menuItem_00);
JMenu menu_1 = new JMenu("帮助");
menuBar.add(menu_1);
menuItem_01 = new JMenuItem("使用说明");
menuItem_01.addActionListener(this);
menu_1.add(menuItem_01);
JMenu menu_2 = new JMenu("关于");
menuBar.add(menu_2);
menuItem_02 = new JMenuItem("关于计算器");
menuItem_02.addActionListener(this);
menu_2.add(menuItem_02);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label_0 = new JLabel(" 工程经济常见公式计算器");
label_0.setFont(new Font("楷体", Font.BOLD, 30));
label_0.setBounds(14, 13, 534, 46);
contentPane.add(label_0);
JPanel panel_0 = new JPanel();
panel_0.setBorder(new TitledBorder(null, "现值与终值及实际利率计算", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_0.setBounds(14, 57, 534, 189);
contentPane.add(panel_0);
panel_0.setLayout(null);
JLabel label = new JLabel("现值与终值计算:");
label.setBounds(42, 23, 169, 24);
panel_0.add(label);
label.setFont(new Font("楷体", Font.PLAIN, 20));
comboBox_0 = new JComboBox<String>();
comboBox_0.setBounds(208, 23, 261, 24);
for (int i = 0; i < lixi.length; i++) {
comboBox_0.addItem(lixi[i]);
}
// comboBox_0.addItemListener(this);
comboBox_0.addMouseListener(this);
panel_0.add(comboBox_0);
comboBox_0.setFont(new Font("楷体", Font.PLAIN, 18));
label_1 = new JLabel(" 公式:");
label_1.setBounds(0, 50, 534, 24);
panel_0.add(label_1);
label_1.setFont(new Font("楷体", Font.PLAIN, 18));
label_2 = new JLabel("F=");
label_2.setBounds(61, 87, 72, 33);
panel_0.add(label_2);
label_2.setFont(new Font("楷体", Font.PLAIN, 20));
textField_0 = new JTextField();
textField_0.setBounds(81, 87, 90, 33);
panel_0.add(textField_0);
textField_0.setFont(new Font("宋体", Font.PLAIN, 20));
textField_0.setColumns(10);
label_3 = new JLabel("i=");
label_3.setBounds(208, 87, 72, 33);
panel_0.add(label_3);
label_3.setFont(new Font("楷体", Font.PLAIN, 20));
textField_1 = new JTextField();
textField_1.setBounds(229, 87, 90, 33);
panel_0.add(textField_1);
textField_1.setFont(new Font("宋体", Font.PLAIN, 20));
textField_1.setColumns(10);
label_4 = new JLabel("n=");
label_4.setBounds(351, 87, 72, 33);
panel_0.add(label_4);
label_4.setFont(new Font("楷体", Font.PLAIN, 20));
textField_2 = new JTextField();
textField_2.setBounds(373, 87, 90, 33);
panel_0.add(textField_2);
textField_2.setFont(new Font("宋体", Font.PLAIN, 20));
textField_2.setColumns(10);
button_0 = new JButton("计算结果");
button_0.setBounds(71, 133, 123, 33);
button_0.addActionListener(this);
panel_0.add(button_0);
button_0.setFont(new Font("楷体", Font.PLAIN, 20));
textField_3 = new JTextField();
textField_3.setBounds(199, 133, 251, 33);
panel_0.add(textField_3);
textField_3.setFont(new Font("楷体", Font.PLAIN, 20));
textField_3.setEditable(false);
textField_3.setColumns(10);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u7ECF\u6D4E\u6548\u679C\u8BC4\u4EF7\u53CA\u5DE5\u7A0B\u8D22\u52A1", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
panel_1.setBounds(14, 259, 534, 197);
contentPane.add(panel_1);
panel_1.setLayout(null);
JLabel label_5 = new JLabel("经济效果评价及工程财务:");
label_5.setFont(new Font("楷体", Font.PLAIN, 18));
label_5.setBounds(0, 27, 234, 24);
panel_1.add(label_5);
comboBox_1 = new JComboBox<String>();
comboBox_1.setFont(new Font("楷体", Font.PLAIN, 18));
comboBox_1.setBounds(208, 27, 261, 24);
for (int i = 0; i < jingji.length; i++) {
comboBox_1.addItem(jingji[i]);
}
// comboBox_1.addItemListener(this);
comboBox_1.addMouseListener(this);
panel_1.add(comboBox_1);
label_6 = new JLabel(" 公式:");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_6.setBounds(0, 57, 534, 24);
panel_1.add(label_6);
label_7 = new JLabel("F=");
label_7.setFont(new Font("楷体", Font.PLAIN, 20));
label_7.setBounds(0, 94, 106, 33);
panel_1.add(label_7);
textField_4 = new JTextField();
textField_4.setFont(new Font("宋体", Font.PLAIN, 20));
textField_4.setColumns(10);
textField_4.setBounds(88, 94, 72, 33);
panel_1.add(textField_4);
label_8 = new JLabel("i=");
label_8.setFont(new Font("楷体", Font.PLAIN, 20));
label_8.setBounds(162, 94, 114, 33);
panel_1.add(label_8);
textField_5 = new JTextField();
textField_5.setFont(new Font("宋体", Font.PLAIN, 20));
textField_5.setColumns(10);
textField_5.setBounds(255, 94, 72, 33);
panel_1.add(textField_5);
label_9 = new JLabel("n=");
label_9.setFont(new Font("楷体", Font.PLAIN, 20));
label_9.setBounds(341, 94, 100, 33);
panel_1.add(label_9);
textField_6 = new JTextField();
textField_6.setFont(new Font("宋体", Font.PLAIN, 20));
textField_6.setColumns(10);
textField_6.setBounds(434, 94, 72, 33);
panel_1.add(textField_6);
button_1 = new JButton("计算结果");
button_1.setFont(new Font("楷体", Font.PLAIN, 20));
button_1.setBounds(71, 140, 123, 33);
button_1.addActionListener(this);
panel_1.add(button_1);
textField_7 = new JTextField();
textField_7.setFont(new Font("楷体", Font.PLAIN, 20));
textField_7.setEditable(false);
textField_7.setColumns(10);
textField_7.setBounds(199, 140, 251, 33);
panel_1.add(textField_7);
JPanel panel_2 = new JPanel();
panel_2.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u5DE5\u7A0B\u8D22\u52A1\u53CA\u5DE5\u7A0B\u4F30\u4EF7", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
panel_2.setBounds(14, 469, 534, 245);
contentPane.add(panel_2);
panel_2.setLayout(null);
label_10 = new JLabel("工程财务相关计算:");
label_10.setFont(new Font("楷体", Font.PLAIN, 20));
label_10.setBounds(14, 26, 196, 24);
panel_2.add(label_10);
comboBox_2 = new JComboBox<String>();
comboBox_2.setFont(new Font("楷体", Font.PLAIN, 18));
comboBox_2.setBounds(208, 26, 261, 24);
for (int i = 0; i < caiwu.length; i++) {
comboBox_2.addItem(caiwu[i]);
}
// comboBox_2.addItemListener(this);
// comboBox_2.addActionListener(this);
comboBox_2.addMouseListener(this);
panel_2.add(comboBox_2);
label_11 = new JLabel(" 公式:");
label_11.setFont(new Font("楷体", Font.PLAIN, 18));
label_11.setBounds(0, 63, 534, 24);
panel_2.add(label_11);
label_12 = new JLabel("F=");
label_12.setFont(new Font("楷体", Font.PLAIN, 20));
label_12.setBounds(14, 100, 113, 33);
panel_2.add(label_12);
textField_8 = new JTextField();
textField_8.setFont(new Font("宋体", Font.PLAIN, 20));
textField_8.setColumns(10);
textField_8.setBounds(129, 100, 106, 33);
panel_2.add(textField_8);
label_13 = new JLabel("i=");
label_13.setFont(new Font("楷体", Font.PLAIN, 20));
label_13.setBounds(235, 100, 132, 33);
panel_2.add(label_13);
textField_9 = new JTextField();
textField_9.setFont(new Font("宋体", Font.PLAIN, 20));
textField_9.setColumns(10);
textField_9.setBounds(365, 100, 96, 33);
panel_2.add(textField_9);
label_14 = new JLabel("F=");
label_14.setFont(new Font("楷体", Font.PLAIN, 20));
label_14.setBounds(14, 139, 113, 33);
panel_2.add(label_14);
textField_10 = new JTextField();
textField_10.setFont(new Font("宋体", Font.PLAIN, 20));
textField_10.setColumns(10);
textField_10.setBounds(129, 139, 106, 33);
panel_2.add(textField_10);
label_15 = new JLabel("i=");
label_15.setFont(new Font("楷体", Font.PLAIN, 20));
label_15.setBounds(235, 139, 132, 33);
panel_2.add(label_15);
textField_11 = new JTextField();
textField_11.setFont(new Font("宋体", Font.PLAIN, 20));
textField_11.setColumns(10);
textField_11.setBounds(365, 146, 96, 33);
panel_2.add(textField_11);
button_2 = new JButton("计算结果");
button_2.setFont(new Font("楷体", Font.PLAIN, 20));
button_2.setBounds(71, 185, 123, 33);
button_2.addActionListener(this);
panel_2.add(button_2);
textField_12 = new JTextField();
textField_12.setFont(new Font("楷体", Font.PLAIN, 20));
textField_12.setEditable(false);
textField_12.setColumns(10);
textField_12.setBounds(199, 187, 251, 33);
panel_2.add(textField_12);
op = new Operation();
this.setUnit1Visible(false);
this.setUnit2Visible(false);
this.setUnit3Visible(false);
}
/**
* 设置组件显隐性函数
* [url=home.php?mod=space&uid=952169]@Param[/url] b 传入true为显现,传入false为隐藏
*/
private void setUnit1Visible(boolean b){
label_1.setVisible(b);
label_2.setVisible(b);
label_3.setVisible(b);
label_4.setVisible(b);
textField_0.setVisible(b);
textField_1.setVisible(b);
textField_2.setVisible(b);
textField_3.setVisible(b);
button_0.setVisible(b);
}
private void setUnit2Visible(boolean b){
label_6.setVisible(b);
label_7.setVisible(b);
label_8.setVisible(b);
label_9.setVisible(b);
textField_4.setVisible(b);
textField_5.setVisible(b);
textField_6.setVisible(b);
textField_7.setVisible(b);
button_1.setVisible(b);
}
private void setUnit3Visible(boolean b){
label_11.setVisible(b);
label_12.setVisible(b);
label_13.setVisible(b);
label_14.setVisible(b);
label_15.setVisible(b);
textField_8.setVisible(b);
textField_9.setVisible(b);
textField_10.setVisible(b);
textField_11.setVisible(b);
textField_12.setVisible(b);
button_2.setVisible(b);
}
/**
* 清空文本框函数
*/
private void removeUnit1TextField(){
textField_0.setText("");
textField_1.setText("");
textField_2.setText("");
textField_3.setText("");
}
private void removeUnit2TextField(){
textField_4.setText("");
textField_5.setText("");
textField_6.setText("");
textField_7.setText("");
}
private void removeUnit3TextField(){
textField_8.setText("");
textField_9.setText("");
textField_10.setText("");
textField_11.setText("");
textField_12.setText("");
}
/*
* (non-Javadoc)下拉框事件函数
* [url=home.php?mod=space&uid=2660]@see[/url] java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
*/
@SuppressWarnings("static-access")
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange()== e.DESELECTED) {
if (comboBox_0.getSelectedIndex()==1) {
this.setUnit1Visible(true);
this.removeUnit1TextField();
label_1.setText(" 公式:F = P(1+i)^n 或 F = P(F/P,i,n)");
label_1.setFont(new Font("楷体", Font.PLAIN, 20));
label_2.setText("P=");
label_3.setText("i=");
label_4.setText("n=");
}
else if (comboBox_0.getSelectedIndex()==2) {
this.setUnit1Visible(true);
this.removeUnit1TextField();
label_1.setText(" 公式:P = F(1+i)^(-n) 或 P = F(P/F,i,n)");
label_1.setFont(new Font("楷体", Font.PLAIN, 18));
label_2.setText("F=");
label_3.setText("i=");
label_4.setText("n=");
}
else if (comboBox_0.getSelectedIndex()==3) {
this.setUnit1Visible(true);
this.removeUnit1TextField();
label_1.setText(" 公式:F = A((1+i)^n-1)/i 或 F = A(F/A,i,n)");
label_1.setFont(new Font("楷体", Font.PLAIN, 18));
label_2.setText("A=");
label_3.setText("i=");
label_4.setText("n=");
}
else if (comboBox_0.getSelectedIndex()==4) {
this.setUnit1Visible(true);
this.removeUnit1TextField();
label_1.setText(" 公式:P = A((1+i)^n-1)/(i*(1+i)^n) 或 P = A(P/A,i,n)");
label_1.setFont(new Font("楷体", Font.PLAIN, 18));
label_2.setText("A=");
label_3.setText("i=");
label_4.setText("n=");
}
else if (comboBox_0.getSelectedIndex()==5) {
this.setUnit1Visible(true);
this.removeUnit1TextField();
label_1.setText(" 公式:A = F*i/((1+i)^n-1) 或 A = F(A/F,i,n)");
label_1.setFont(new Font("楷体", Font.PLAIN, 18));
label_2.setText("F=");
label_3.setText("i=");
label_4.setText("n=");
}
else if (comboBox_0.getSelectedIndex()==6) {
this.setUnit1Visible(true);
this.removeUnit1TextField();
label_1.setText(" 公式:A = P*i*(1+i)^n/((1+i)^n-1) 或 A = P(A/P,i,n)");
label_1.setFont(new Font("楷体", Font.PLAIN, 18));
label_2.setText("P=");
label_3.setText("i=");
label_4.setText("n=");
}
else if (comboBox_0.getSelectedIndex()==7) {
this.setUnit1Visible(true);
this.removeUnit1TextField();
label_1.setText(" 公式:i = (1+r/m)^n-1"+" "+"注:r为名义利率,m为计息周期,n为收付周期");
label_1.setFont(new Font("楷体", Font.PLAIN, 16));
label_2.setText("r=");
label_3.setText("m=");
label_4.setText("n=");
}
}
else if (e.getStateChange()==e.SELECTED) {
if (flag) {
if (comboBox_1.getSelectedIndex()==1) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:总投资收益率(ROI)=息税前利润(EBIT)/总投资(TI)");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_7.setText(" EBIT=");
label_8.setText(" TI=");
label_9.setText("");
label_9.setVisible(false);
textField_6.setVisible(false);
}
else if (comboBox_1.getSelectedIndex()==2) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:资本金净利润率(ROE)=净利润(NP)/资本金(EC)");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_7.setText(" NP=");
label_8.setText(" EC=");
label_9.setText("");
label_9.setVisible(false);
textField_6.setVisible(false);
}
else if (comboBox_1.getSelectedIndex()==3) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:利息备付率=息税前利润(EBIT)/当期应付利息(PI)");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_7.setText(" EBIT=");
label_8.setText(" PI=");
label_9.setText("");
label_9.setVisible(false);
textField_6.setVisible(false);
}
else if (comboBox_1.getSelectedIndex()==4) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:偿债备付率=可用于还本付息的资金(EBITDA-TAx)/当期应还本付息的金额(PD)");
label_6.setFont(new Font("楷体", Font.PLAIN, 14));
label_7.setText(" EBITDA=");
label_8.setText(" TAx=");
label_9.setText(" PD=");
label_9.setVisible(true);
textField_6.setVisible(true);
}
else if (comboBox_1.getSelectedIndex()==5) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:资产负债率=负债总额/资产总额");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_7.setText(" 总负债=");
label_8.setText(" 总资产=");
label_9.setText("");
label_9.setVisible(false);
textField_6.setVisible(false);
}
else if (comboBox_1.getSelectedIndex()==6) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:流动比率=流动资产/流动负债");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_7.setText("流动资产=");
label_8.setText("流动负债=");
label_9.setText("");
label_9.setVisible(false);
textField_6.setVisible(false);
}
else if (comboBox_1.getSelectedIndex()==7) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:速动比率=速动资产/流动负债");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_7.setText("速动资产=");
label_8.setText("流动负债=");
label_9.setText("");
label_9.setVisible(false);
textField_6.setVisible(false);
}
else if (comboBox_1.getSelectedIndex()==8) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:净资产收益率=净利润/平均净资产");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_7.setText(" 净利润=");
label_8.setText(" 净资产=");
label_9.setText("");
label_9.setVisible(false);
textField_6.setVisible(false);
}
else if (comboBox_1.getSelectedIndex()==9) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:总资产净利率=息税前利润/平均资产总额");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_7.setText(" EBIT=");
label_8.setText(" 总资产=");
label_9.setText("");
label_9.setVisible(false);
textField_6.setVisible(false);
}
else if (comboBox_1.getSelectedIndex()==10) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:总资产周转率=主营业务收入净额/平均资产总额");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_7.setText("主营收入=");
label_8.setText("资产总额=");
label_9.setText("");
label_9.setVisible(false);
textField_6.setVisible(false);
}
else if (comboBox_1.getSelectedIndex()==11) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:流动资产周转率=流动资产周转额/平均流动资产余额");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_7.setText(" 周转额=");
label_8.setText("资产余额=");
label_9.setText("");
label_9.setVisible(false);
textField_6.setVisible(false);
}
else if (comboBox_1.getSelectedIndex()==12) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:资产负债率=负债总额/资产总额");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_7.setText("负债总额=");
label_8.setText("资产总额=");
label_9.setText("");
label_9.setVisible(false);
textField_6.setVisible(false);
}
else if (comboBox_1.getSelectedIndex()==13) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 公式:资金成本率=年利率*(1-企业所得税率)/(1-借款手续费率)");
label_6.setFont(new Font("楷体", Font.PLAIN, 18));
label_7.setText(" 年利率=");
label_8.setText("所得税率=");
label_9.setText("手续费率=");
// label_9.setVisible(true);
// textField_6.setVisible(true);
}
else if (comboBox_1.getSelectedIndex()==14) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 经济订货量=√(2KD/K2),D为存货需要量,Q为每次进货量,K为每次订货变动成本,K2为单位存储成本");
label_6.setFont(new Font("楷体", Font.PLAIN, 12));
label_7.setText(" K=");
label_8.setText(" D=");
label_9.setText(" K2=");
// label_9.setVisible(true);
// textField_6.setVisible(true);
}
else if (comboBox_1.getSelectedIndex()==15) {
this.setUnit2Visible(true);
this.removeUnit2TextField();
label_6.setText(" 经济寿命(N0)=√(2(P-Ln)λ),P为设备的实际价值,Ln为第n年末的设备净残值,λ为设备的低劣化值");
label_6.setFont(new Font("楷体", Font.PLAIN, 12));
label_7.setText(" P=");
label_8.setText(" Ln=");
label_9.setText(" λ=");
// label_9.setVisible(true);
// textField_6.setVisible(true);
}
}
else {
if (comboBox_2.getSelectedIndex()==1) {
this.setUnit3Visible(true);
this.removeUnit3TextField();
label_11.setText(" 公式:租金(R)=P*(1+N*i)/N+P*r,P为资产的价格,N为还款期数,i为折现率,r为附加率");
label_11.setFont(new Font("楷体", Font.PLAIN, 14));
label_12.setText(" P=");
label_13.setText(" N=");
label_14.setText(" i=");
label_15.setText(" r=");
}
else if (comboBox_2.getSelectedIndex()==2) {
this.setUnit3Visible(true);
this.removeUnit3TextField();
label_11.setText(" 公式1:经营成本=总成本费用-折旧费-摊销费-利息支出");
label_11.setFont(new Font("楷体", Font.PLAIN, 18));
label_12.setText("总成本费用=");
label_13.setText(" 折旧费=");
label_14.setText(" 摊销费=");
label_15.setText(" 利息支出=");
}
else if (comboBox_2.getSelectedIndex()==3) {
this.setUnit3Visible(true);
this.removeUnit3TextField();
label_11.setText(" 公式2:经营成本=外购原材料、燃料动力费+工资福利费+修理费+其他费用");
label_11.setFont(new Font("楷体", Font.PLAIN, 16));
label_12.setText("燃料动力费=");
label_13.setText(" 工资福利费=");
label_14.setText(" 修理费=");
label_15.setText(" 其他费用=");
}
else if (comboBox_2.getSelectedIndex()==4) {
this.setUnit3Visible(true);
this.removeUnit3TextField();
label_11.setText(" 产销量=年固定总成本/(单位产品销售单价-单位产品变动成本-单位产品销售税金及附加)");
label_11.setFont(new Font("楷体", Font.PLAIN, 13));
label_12.setText(" 固定成本=");
label_13.setText(" 销售单价=");
label_14.setText(" 变动成本=");
label_15.setText(" 税金及附加=");
}
else if (comboBox_2.getSelectedIndex()==5) {
this.setUnit3Visible(true);
this.removeUnit3TextField();
label_11.setText(" 敏感度系数(Saf)=(△A/A)/(△F/F),△F/F为不确定因素变化率,△A/A为评价指标变化率");
label_11.setFont(new Font("楷体", Font.PLAIN, 13));
label_12.setText(" △A=");
label_13.setText(" A=");
label_14.setText(" △F=");
label_15.setText(" F=");
}
else if (comboBox_2.getSelectedIndex()==6) {
this.setUnit3Visible(true);
this.removeUnit3TextField();
label_11.setText(" 基本预备费=(设备工器具购置费+建筑安装工程费+工程建设其他费)*基本预备费率");
label_11.setFont(new Font("楷体", Font.PLAIN, 14));
label_12.setText(" 购置费=");
label_13.setText(" 建安费=");
label_14.setText(" 其他费=");
label_15.setText(" 预备费率=");
}
else if (comboBox_2.getSelectedIndex()==7) {
this.setUnit3Visible(true);
this.removeUnit3TextField();
label_11.setText(" 材料基价=[(供应价格+运杂费)*(1+运输损耗率)]*(1+采购保管费率)");
label_11.setFont(new Font("楷体", Font.PLAIN, 17));
label_12.setText(" 供应价格=");
label_13.setText(" 运杂费=");
label_14.setText("运输损耗率=");
label_15.setText("采购保管费率=");
}
}
}
}
/*
* (non-Javadoc)动作事件函数
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
a = textField_0.getText().trim();
b = textField_1.getText().trim();
c = textField_2.getText().trim();
d = textField_4.getText().trim();
f = textField_5.getText().trim();
g = textField_6.getText().trim();
m = textField_8.getText().trim();
n = textField_9.getText().trim();
o = textField_10.getText().trim();
p = textField_11.getText().trim();
if (e.getSource()== button_0) {
if (comboBox_0.getSelectedIndex()==1) {
String gongshi = a+"*"+"("+"1"+"+"+b+")"+"^"+c;
String result = "";
try {
result = op.getResult(gongshi);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_3.setText(result);
}
else if (comboBox_0.getSelectedIndex()==2) {
String zhongzhi_0 = a+"/"+"("+"1"+"+"+b+")"+"^"+c;
String result = "";
try {
result = op.getResult(zhongzhi_0);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_3.setText(result);
}
else if (comboBox_0.getSelectedIndex()==3) {
String zhongzhi_0 = a+"*"+"("+"("+"1"+"+"+b+")"+"^"+c+"-"+"1"+")"+"/"+b;
String result = "";
try {
result = op.getResult(zhongzhi_0);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_3.setText(result);
}
else if (comboBox_0.getSelectedIndex()==4) {
String zhongzhi_0 = a+"*"+"("+"("+"1"+"+"+b+")"+"^"+c+"-"+"1"+")"+"/"+"("+b+"*"+"("+"1"+"+"+b+")"+"^"+c+")";
String result = "";
try {
result = op.getResult(zhongzhi_0);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_3.setText(result);
}
else if (comboBox_0.getSelectedIndex()==5) {
String zhongzhi_0 = a+"*"+b+"/"+"("+"("+"1"+"+"+b+")"+"^"+c+"-"+"1"+")";
String result = "";
try {
result = op.getResult(zhongzhi_0);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_3.setText(result);
}
else if (comboBox_0.getSelectedIndex()==6) {
String zhongzhi_0 = a+"*"+b+"*"+"("+"1"+"+"+b+")"+"^"+c+"/"+"("+"("+"1"+"+"+b+")"+"^"+c+"-"+"1"+")";
String result = "";
try {
result = op.getResult(zhongzhi_0);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_3.setText(result);
}
else if (comboBox_0.getSelectedIndex()==7) {
String zhongzhi_0 = "("+"1"+"+"+a+"/"+b+")"+"^"+c+"-"+"1";
String result = "";
try {
result = op.getResult(zhongzhi_0);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_3.setText(result);
}
}
else if (e.getSource()==button_1) {
if (comboBox_1.getSelectedIndex()==1||comboBox_1.getSelectedIndex()==2||comboBox_1.getSelectedIndex()==3
||comboBox_1.getSelectedIndex()==5||comboBox_1.getSelectedIndex()==6||comboBox_1.getSelectedIndex()==7
||comboBox_1.getSelectedIndex()==8||comboBox_1.getSelectedIndex()==9||comboBox_1.getSelectedIndex()==10
||comboBox_1.getSelectedIndex()==11||comboBox_1.getSelectedIndex()==12) {
String gongshi = d+"/"+f;
String result = "";
try {
result = op.getResult(gongshi);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_7.setText(result);
}
else if (comboBox_1.getSelectedIndex()==4) {
String gongshi = "("+d+"-"+f+")"+"/"+g;
String result = "";
try {
result = op.getResult(gongshi);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_7.setText(result);
}
else if (comboBox_1.getSelectedIndex()==13) {
String gongshi = d+"*"+"("+"1"+"-"+f+")"+"/"+"("+"1"+"-"+g+")";
String result = "";
try {
result = op.getResult(gongshi);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_7.setText(result);
}
else if (comboBox_1.getSelectedIndex()==14) {
String gongshi = "√"+"("+"2"+"*"+d+"*"+f+"/"+g+")";
String result = "";
try {
result = op.getResult(gongshi);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_7.setText(result);
}
}
else if (e.getSource()==button_2) {
if (comboBox_2.getSelectedIndex()==1) {
String gongshi = m+"*"+"("+"1"+"+"+n+"*"+o+")"+"/"+n+"+"+m+"*"+p;
String result = "";
try {
result = op.getResult(gongshi);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_12.setText(result);
}
else if (comboBox_2.getSelectedIndex()==2) {
String gongshi = m+"-"+n+"-"+o+"-"+p;
String result = "";
try {
result = op.getResult(gongshi);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_12.setText(result);
}
else if (comboBox_2.getSelectedIndex()==3) {
String gongshi = m+"+"+n+"+"+o+"+"+p;
String result = "";
try {
result = op.getResult(gongshi);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_12.setText(result);
}
else if (comboBox_2.getSelectedIndex()==4) {
String gongshi = m+"/"+"("+n+"-"+o+"-"+p+")";
String result = "";
try {
result = op.getResult(gongshi);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_12.setText(result);
}
else if (comboBox_2.getSelectedIndex()==5) {
String gongshi = "("+m+"/"+n+")"+"/"+"("+o+"/"+p+")";
String result = "";
try {
result = op.getResult(gongshi);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_12.setText(result);
}
else if (comboBox_2.getSelectedIndex()==6) {
String gongshi = "("+m+"+"+n+"+"+o+")"+"*"+p;
String result = "";
try {
result = op.getResult(gongshi);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_12.setText(result);
}
else if (comboBox_2.getSelectedIndex()==7) {
String gongshi = "("+"("+m+"+"+n+")"+"*"+"("+"1"+"+"+o+")"+")"+"*"+"("+"1"+"+"+p+")";
String result = "";
try {
result = op.getResult(gongshi);
} catch (Exception e1) {
e1.printStackTrace();
}
textField_12.setText(result);
}
}
else if (e.getSource()==menuItem_00) {
this.dispose();
CalculatorUI c = new CalculatorUI();
c.setVisible(true);
c.setLocation(this.getX(), this.getY());
}
else if (e.getSource()==menuItem_01) {
// this.dispose();
Help help = new Help();
help.setVisible(true);
help.setLocation(this.getX(), this.getY()+60);
}
else if (e.getSource()==menuItem_02) {
// this.dispose();
About frame = new About();
frame.setLocation(this.getX(), this.getY()+50);
frame.setVisible(true);
}
}
/*
* (non-Javadoc)鼠标事件函数
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
*/
@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource()==comboBox_0) {
comboBox_0.addItemListener(this);
comboBox_1.removeItemListener(this);
comboBox_2.removeItemListener(this);
}
else if (e.getSource()==comboBox_1) {
comboBox_1.addItemListener(this);
comboBox_0.removeItemListener(this);
comboBox_2.removeItemListener(this);
flag = true;
}
else if (e.getSource()==comboBox_2) {
comboBox_2.addItemListener(this);
comboBox_0.removeItemListener(this);
comboBox_1.removeItemListener(this);
flag = false;
}
}
@Override
public void mouseEntered(MouseEvent e) {
if (e.getSource()==comboBox_0) {
comboBox_0.addItemListener(this);
comboBox_1.removeItemListener(this);
comboBox_2.removeItemListener(this);
}
else if (e.getSource()==comboBox_1) {
comboBox_1.addItemListener(this);
comboBox_0.removeItemListener(this);
comboBox_2.removeItemListener(this);
flag = true;
}
else if (e.getSource()==comboBox_2) {
comboBox_2.addItemListener(this);
comboBox_0.removeItemListener(this);
comboBox_1.removeItemListener(this);
flag = false;
}
}
@Override
public void mouseExited(MouseEvent e) {
if (e.getSource()==comboBox_0) {
comboBox_0.addItemListener(this);
comboBox_1.removeItemListener(this);
comboBox_2.removeItemListener(this);
}
else if (e.getSource()==comboBox_1) {
comboBox_1.addItemListener(this);
comboBox_0.removeItemListener(this);
comboBox_2.removeItemListener(this);
flag = true;
}
else if (e.getSource()==comboBox_2) {
comboBox_2.addItemListener(this);
comboBox_0.removeItemListener(this);
comboBox_1.removeItemListener(this);
flag = false;
}
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getSource()==comboBox_0) {
comboBox_0.addItemListener(this);
comboBox_1.removeItemListener(this);
comboBox_2.removeItemListener(this);
}
else if (e.getSource()==comboBox_1) {
comboBox_1.addItemListener(this);
comboBox_0.removeItemListener(this);
comboBox_2.removeItemListener(this);
flag = true;
}
else if (e.getSource()==comboBox_2) {
comboBox_2.addItemListener(this);
comboBox_0.removeItemListener(this);
comboBox_1.removeItemListener(this);
flag = false;
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.getSource()==comboBox_0) {
comboBox_0.addItemListener(this);
comboBox_1.removeItemListener(this);
comboBox_2.removeItemListener(this);
}
else if (e.getSource()==comboBox_1) {
comboBox_1.addItemListener(this);
comboBox_0.removeItemListener(this);
comboBox_2.removeItemListener(this);
flag = true;
}
else if (e.getSource()==comboBox_2) {
comboBox_2.addItemListener(this);
comboBox_0.removeItemListener(this);
comboBox_1.removeItemListener(this);
flag = false;
}
}
}
3、关于界面
[Java] 纯文本查看 复制代码
package com.cary.ui;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.JScrollPane;
public class About extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane_0;
/**
* Create the frame.
*/
public About() {
setResizable(false);
setIconImage(Toolkit.getDefaultToolkit().getImage("image\\jsq1.png"));
setTitle("关于\"计算器\"");
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 580, 600);
contentPane_0 = new JPanel();
contentPane_0.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane_0);
contentPane_0.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(14, 13, 534, 527);
contentPane_0.add(scrollPane);
JTextArea text_0 = new JTextArea();
scrollPane.setViewportView(text_0);
text_0.setEditable(false);
text_0.setFont(new Font("楷体", Font.PLAIN, 18));
text_0.setText(" \r\n “飞扬计算器”是一款采用JAVA语言编写,根据四则运算原理开\r\n\r\n 发而成的计算器软件,属于半科学型计算器。利用本计算器可完\r\n\r\n 成常规四则运算、乘方、根式及带括号运算式的运算,可以满足\r\n\r\n 日常生活计算需求以及工程经济科目计算题的计算。由于实际中\r\n\r\n 三角函数运算运用极少,本计算器未添加三角函数运算功能。\r\n\r\n 为答谢江山老师及群内各考友对本人在考取建造师证书一路之\r\n\r\n 上的帮助,本人将“飞扬计算器”分享给各考友使用,并祝江山\r\n\r\n 老师生意兴隆,祝各位考友顺利通过一建考试!!!\r\n\r\n \r\n 飞扬计算器\r\n\r\n Copyright 2016 Cary-云飞扬。保留所有权利。");
}
}
4、帮助界面
[Java] 纯文本查看 复制代码
package com.cary.ui;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import java.awt.Font;
import java.awt.Toolkit;
public class Help extends JFrame {
/**
*
*/
private static final long serialVersionUID = -7693131609176277698L;
private JPanel contentPane;
/**
* Create the frame.
*/
public Help() {
setIconImage(Toolkit.getDefaultToolkit().getImage("image\\jsq1.png"));
setTitle("使用说明");
setResizable(false);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 580, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTextArea txtrxxy = new JTextArea();
txtrxxy.setEditable(false);
txtrxxy.setFont(new Font("楷体", Font.PLAIN, 20));
txtrxxy.setText("\r\n普通型计算器输入示例:\r\n\r\n 1、乘方:5的平方,先输入5,然后点击x^2按钮,再点击\r\n\r\n “=”即可显示结果为25。如果计算5的4次方,则先输入5,\r\n\r\n 然后点击x^y按钮,再点击“=”即可。\r\n\r\n 2、开方:计算根号下2,先输入根号,再输入2,再点击\r\n\r\n “=”即可。\r\n\r\n 3、连续运算:5+3*2-1+3^2/2,依次输入即可,运算结果\r\n\r\n 为:14.5\r\n\r\n 4、带括号运算式:3*(2+3*(4-2))+(3+1)^2-1,依\r\n\r\n 次输入,计算结果为:39。\r\n");
txtrxxy.setBounds(14, 13, 546, 527);
contentPane.add(txtrxxy);
}
}
5、算法代码
[Java] 纯文本查看 复制代码
package com.cary.service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack;
public class Operation {
private static ArrayList<String> list;
// public static void main(String[] args) {
// // TODO Auto-generated method stub
// // String d = "2+(√4.0*(4.0/(3-1)+4*(3-1.0)))+2";
// // String d = "6-4+1+4*3/2*2+1";
// // String d = "2*8+3-1+8/2+3*4-5+8.2/2";
// // String d = "33.25*3/2+1.09+√8*2^2+1";
// // String d = "2*3+2*(√16+(3-1)*2)-6/(3-1)+1";
// // String d = "(9^2+2)*(3-1)^2*√(2+2)";
//// String d = "20*((1+0.06)^5-1)/0.06";
// String d = "√4";
//
// Operation test = new Operation();
// String t = test.getResult(d);
// System.out.println(t);
//
// }
// 四则运算函数(加减乘除)
public String getResult(String s) throws Exception {
// 对取得的字符串式子进行分割并存入集合中
list = new ArrayList<String>();
// 将字符串进行分割,并存入集合中
splitCharacter(s, list);
// 1、分割之后开始运算,第一步判断是否有括号,如果有括号,先算括号内的表达式
getResultOfBracket(list);
// 2、如果分割之后没有括号,直接进行四则运算
String temp = fourArithmeticOperation(list);
//对结果进行处理,如果是小数并且以0结尾,则去除结尾的0
String lastResult = removeLastZero(temp);
//返回最终结果
return lastResult;
}
/**
* 去除小数结尾的零函数
*
* @param s
* 数值类型字符串
* [url=home.php?mod=space&uid=155549]@Return[/url] 如果数值是非零结尾的小数或者整数,则返回原值;如果是以零结尾的小数,则去除结尾的零
*/
public String removeLastZero(String s) {
if (s.contains(".") && s.endsWith("0")) {
int index = s.indexOf(".");
String s1 = "";
String temp = s.substring(index + 1, s.length());
for (int i = temp.length() - 1; i > 0; i--) {
if (temp.endsWith("0")) {
temp = temp.substring(0, temp.length() - 1);
}
}
if (temp.equals("0")) {
s1 = s.substring(0, index + 1);
} else {
s1 = s.substring(0, index + 1) + temp;
}
if (s1.endsWith(".")) {
s1 = s1.substring(0, s1.length() - 1);
}
return s1;
}
else {
return s;
}
}
/**
* 计算括号内表达式函数(计算带有括号的表达式)
*
* @param list
* 含有表达式元素的集合
* @throws Exception
*/
public void getResultOfBracket(ArrayList<String> list) throws Exception {
// 左括号存入的堆栈
Stack<String> sk = new Stack<String>();
// 左括号角标存入的堆栈
Stack<Integer> index = new Stack<Integer>();
// 括号内表达式存入的临时运算集合
ArrayList<String> temp = new ArrayList<String>();
// 将左括号与对应的角标存入对应的堆栈中
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals("(")) {
sk.push(list.get(i));
index.push(i);
}
// 检测到右括号时,将与之匹配的左括号内的表达式存入临时运算集合,并计算
else if (list.get(i).equals(")")) {
if (!sk.isEmpty() && !index.isEmpty()) {
if (sk.peek().equals("(")) {
for (int j = index.peek() + 1; j < i; j++) {
temp.add(list.get(j));
}
String tempResult = this.fourArithmeticOperation(temp);
temp.clear();
list.set(i, tempResult);
for (int j = index.peek(); j < i; j++) {
list.set(j, "");
}
sk.pop();
index.pop();
}
}
}
}
// 去除集合中的空字符
removeNullCharacter(list);
}
/**
* 分割字符函数
*
* @param s
* 被分割的字符串
* @param list
* 分割之后存入的集合
*/
public void splitCharacter(String s, ArrayList<String> list) {
int index = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '√' || c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '^') {
list.add(s.substring(index, i));
list.add(c + "");
index = i + 1;
}
}
list.add(s.substring(index, s.length()));
// 去除集合中空字符元素
removeNullCharacter(list);
}
/**
* 四则运算函数
*
* @param list
* 运算符与数值的集合
* @return 返回运算结果的字符串形式
* @throws Exception
*/
public String fourArithmeticOperation(ArrayList<String> list) throws Exception {
// 不带括号的表达式运算方法
if (list.size() < 2 && list.size() > 0) {
return list.get(0);
} else {
/*
* 计算根式与乘方运算
*/
// 临时存入带有根号或乘方运算符的集合
ArrayList<Integer> cl = new ArrayList<Integer>();
// 向角标集合中添加运算符的角标
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals("√")) {
cl.add(i);
} else if (list.get(i).equals("^")) {
cl.add(i);
}
}
// 排序
Collections.sort(cl);
// 运算
for (int i = 0; i < cl.size(); i++) {
if (list.get(cl.get(i)).equals("√")) {
// 开方运算,平方根
String gen = Double.toString(Math.sqrt(changeDouble(list.get(cl.get(i) + 1))));
// 替换
list.set(cl.get(i) + 1, gen);
list.set(cl.get(i), "");
} else if (list.get(cl.get(i)).equals("^")) {
// 乘方运算
String chengFang = Double.toString(
Math.pow(changeDouble(list.get(cl.get(i) - 1)), changeDouble(list.get(cl.get(i) + 1))));
// 替换
list.set(cl.get(i) + 1, chengFang);
list.set(cl.get(i), "");
list.set(cl.get(i) - 1, "");
}
}
// 清空角标集合
cl.clear();
// 去除集合中空字符元素
removeNullCharacter(list);// 至此,根式与乘方运算结束!
/*
* 乘法与除法的运算
*/
// 临时存入数值角标的集合,式中无括号与根号和乘方运算符的情况
ArrayList<Integer> al = new ArrayList<Integer>();
if (cl.isEmpty()) {
// 计算乘法与除法
for (int i = 0; i < list.size(); i++) {
// 式子中有乘除运算符
if (list.get(i).equals("*")) {
al.add(i);
} else if (list.get(i).equals("/")) {
al.add(i);
}
}
// 排序
Collections.sort(al);
// 运算
for (int i = 0; i < al.size(); i++) {
if (list.get(al.get(i)).equals("*")) {
// 运算
String cheng = changeBigDecimal(list.get(al.get(i) - 1))
.multiply(changeBigDecimal(list.get(al.get(i) + 1))).toString();
// 交换
list.set(al.get(i) + 1, cheng);
list.set(al.get(i), "");
list.set(al.get(i) - 1, "");
} else if (list.get(al.get(i)).equals("/")) {
String chu = changeBigDecimal(list.get(al.get(i) - 1))
.divide(changeBigDecimal(list.get(al.get(i) + 1)), 10, RoundingMode.HALF_UP).toString();
list.set(al.get(i) + 1, chu);
list.set(al.get(i), "");
list.set(al.get(i) - 1, "");
}
}
// 清空集合
al.clear();
// 去除集合中空字符元素
removeNullCharacter(list);// 至此,乘法与除法计算完毕!
}
/*
* 加法与减法运算
*/
// 计算加法与减法,保证根式与乘方运算完成,并且乘除法计算完成之后才进行加减法,判断al与cl集合是否为空
if (al.isEmpty() && cl.isEmpty()) {
// 存放加号角标与减号角标的集合
ArrayList<Integer> bl = new ArrayList<Integer>();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals("+")) {
bl.add(i);
} else if (list.get(i).equals("-")) {
bl.add(i);
}
}
// 排序
Collections.sort(bl);
// 运算加减法
for (int i = 0; i < bl.size(); i++) {
if (list.get(bl.get(i)).equals("+")) {
String jia = changeBigDecimal(list.get(bl.get(i) - 1))
.add(changeBigDecimal(list.get(bl.get(i) + 1))).toString();
// 替换
list.set(bl.get(i) + 1, jia);
list.set(bl.get(i), "");
list.set(bl.get(i) - 1, "");
} else if (list.get(bl.get(i)).equals("-")) {
// 如果减号是集合中角标为0的元素,那么将集合中角标为1的元素取反,即取负数
if (bl.get(i) == 0) {
String temp = changeBigDecimal(list.get(bl.get(i) + 1)).negate().toString();
list.set(bl.get(i) + 1, temp);
list.set(bl.get(i), "");
// 如果减号不是集合中角标为0的元素,则可正常运算
} else {
String jian = changeBigDecimal(list.get(bl.get(i) - 1))
.subtract(changeBigDecimal(list.get(bl.get(i) + 1))).toString();
// 替换
list.set(bl.get(i) + 1, jian);
list.set(bl.get(i), "");
list.set(bl.get(i) - 1, "");
}
}
}
// 清空集合
bl.clear();
// 去除集合中空字符元素
removeNullCharacter(list);// 到此为止,加减法计算完毕,此时集合中应该只有一个元素,即:第0个元素
}
return list.get(0);
}
}
// 清空集合中空字符元素函数
private void removeNullCharacter(ArrayList<String> list) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals("")) {
list.remove(i);
i = i - 1;
}
}
}
// 将字符串转换为BigDecimal类型数值
public BigDecimal changeBigDecimal(String s)throws Exception {
BigDecimal bd = new BigDecimal(s);
return bd;
}
// 将字符串转换为double类型数值
public double changeDouble(String s) {
double d = Double.parseDouble(s);
return d;
}
}
6、主程序入口
[Java] 纯文本查看 复制代码
package com.cary.app;
import com.cary.ui.CalculatorUI;
public class MainApp {
public static void main(String[] args) {
CalculatorUI c = new CalculatorUI();
c.setVisible(true);
c.setLocation(200, 100);
}
}
忘了说明,工程经济的计算是因为当年我考一建,做题的时候为了计算省时间,就写了这么个界面,用不到的朋友直接忽略。现在我开学学C++呢,用Qt重写飞扬计算器,功能更强,写完我也分享出来,目前界面写完了,还差算法。这是我学C++的第一个程序。
最后,我将整个工程放在上面,给需要的朋友下载。链接:https://pan.baidu.com/s/1gYori-vx0_HTDx-4YP-Fzg 提取码:79yu
免费评分
参与人数 1 吾爱币 +5
热心值 +1
收起
理由
苏紫方璇
+ 5
+ 1
欢迎分析讨论交流,吾爱破解论坛有你更精彩!
查看全部评分