前言
因为这两天我们的Java实验课程需要做两个小的图形化界面,其中就有一个图形界面的计算器,所以稍微花了点时间做了一个,同时复习了一下Java的基础内容和GUI编程。因为代码中的注释特别详细,所以这里就不讲解怎么实现的了。
代码
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
package zuoye;
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Stack;
class MyException extends Exception{ public MyException() { super (); } public MyException(String message) { super (message); } }
public class MyCalculator extends JFrame{ /* * */
private JTextField textField; //输入文本框 private String input; //结果 private String operator; //操作符
public MyCalculator() { input = "" ; operator = "" ;
JPanel panel = new JPanel(); textField = new JTextField( 30 ); textField.setEditable( false ); //文本框禁止编辑 textField.setHorizontalAlignment(JTextField.LEFT); //textField.setBounds(100, 100, 20, 20); //在容器布局为空情况下生效 textField.setPreferredSize( new Dimension( 200 , 30 )); //设置该组件的初始大小
//将textField加入本JFrame中,布局为边界布局,位置为north this .add(textField, BorderLayout.NORTH);
String[] name= { "7" , "8" , "9" , "+" , "4" , "5" , "6" , "-" , "1" , "2" , "3" , "*" , "0" , "C" , "=" , "/" };
//将这个panel的布局设置为网格布局,有四行四列,行间距和列间距为1 panel.setLayout( new GridLayout( 4 , 4 , 1 , 1 ));
for ( int i= 0 ;i<name.length;i++) {
JButton button = new JButton(name[i]);
//设置按钮的时间监听 button.addActionListener( new MyActionListener()); //将按钮加入到panel中 panel.add(button); } //将panel加入到本JFrame中,布局为边界布局,位置为centre this .add(panel,BorderLayout.CENTER); }
class MyActionListener implements ActionListener{ //内部类实现按钮响应
@Override public void actionPerformed(ActionEvent e) { int cnt= 0 ; String actionCommand = e.getActionCommand(); //获取按钮上的字符串 if (actionCommand.equals( "+" ) || actionCommand.equals( "-" ) || actionCommand.equals( "*" ) || actionCommand.equals( "/" )) { input += " " + actionCommand + " " ; } else if (actionCommand.equals( "C" )) { //清除输入 input = "" ; } else if (actionCommand.equals( "=" )) { //按下等号 try { input+= "=" +calculate(input); } catch (MyException e1) { if (e1.getMessage().equals( "被除数不能为0" )) input = e1.getMessage(); else input = e1.getMessage(); } textField.setText(input); input= "" ; cnt = 1 ; } else input += actionCommand; //按下数字
//因为如果不按[=]按钮cnt一直未0,所以可以保证显示输入的数字和操作键 if (cnt == 0 ) textField.setText(input); } } //这里需要借助栈来完成表达式的计算,首先将字符串分割成字符串数组, //由中缀的定义知数组奇数位为运算符(从第0位开始),偶数位为操作数, // 因此可将偶数为操作数进栈,遇见+(-)运算符,则将下一个数以正(负)的形式压人栈中, // 遇见*或/运算符,则将栈顶元素出栈与数组后一元素进行计算,并将其结果重新压入栈中, // 直至遍历至数组最后一个元素。
private String calculate(String input) throws MyException{ //计算函数 String[] comput = input.split( " " ); //System.out.println(input); Stack<Double> stack = new Stack<>(); Double m = Double.parseDouble(comput[ 0 ]); stack.push(m); //第一个操作数入栈
for ( int i = 1 ; i < comput.length; i++) { if (i% 2 == 1 ) { if (comput[i].equals( "+" )) stack.push(Double.parseDouble(comput[i+ 1 ])); if (comput[i].equals( "-" )) stack.push(-Double.parseDouble(comput[i+ 1 ])); if (comput[i].equals( "*" )) { //将前一个数出栈做乘法再入栈 Double d = stack.peek(); //取栈顶元素 stack.pop(); stack.push(d*Double.parseDouble(comput[i+ 1 ])); } if (comput[i].equals( "/" )) { //将前一个数出栈做乘法再入栈 double help = Double.parseDouble(comput[i+ 1 ]); if (help == 0 ) throw new MyException( "被除数不能为0" ); //不会继续执行该函数 double d = stack.peek(); stack.pop(); stack.push(d/help); } } }
double d = 0d;
while (!stack.isEmpty()) { //求和 d += stack.peek(); stack.pop(); }
String result = String.valueOf(d); return result; }
public static void main(String[] args) { JFrame f = new MyCalculator(); f.setTitle(f.getClass().getSimpleName()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setBounds( 400 , 200 , 500 , 300 ); f.setVisible( true ); } } |
实现效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/q982151756/article/details/80647663