diff --git a/src/java2022spring/GoCaculator.java b/src/java2022spring/GoCaculator.java new file mode 100644 index 0000000000000000000000000000000000000000..c6422f9e3962cc3db37deb739a7e49be32d8a6f8 --- /dev/null +++ b/src/java2022spring/GoCaculator.java @@ -0,0 +1,162 @@ +package java2022spring; +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import java.math.*; + +public class GoCaculator implements ActionListener{ + JTextField f1;//定义文本框 + JButton b1;//定义 + String btName[]= {"del","CE","sqrt","^2", + "7","8","9","÷", + "4","5","6","×", + "1","2","3","-", + "0",".","=","+"}; + String str1=""; + String str2=""; + String a1=""; + String b2=""; + char operate='~'; + double c; + boolean place; + + public void go() { + //创建窗口 + JFrame frame = new JFrame("计算器"); + //设置frame的大小、标题 + frame.setSize(300,450); + frame.setTitle("Smin's Calculator"); + frame.setResizable(false);//禁用窗口大小调整 + + //创建文本框 + f1=new JTextField("0"); + f1.setPreferredSize(new Dimension(400,60));//调整输入框的大小 + //设置文本框字体、颜色 + f1.setFont(new Font("黑体",Font.BOLD,25)); + f1.setBackground(new Color(200,200,200)); + frame.add(f1,BorderLayout.NORTH); + + //创建面板 + JPanel p = new JPanel(); + p.setLayout(new GridLayout(5,4));//5行4列 + + //创建按键 + b1=new JButton(); + frame.add(b1,BorderLayout.CENTER); + for(int i=0;i='0' && m <= '9') || m=='.') { + //字符串处理 + if(operate == '~') + { place = true; + str1 += action;//实现 每次点的数字可以保留下来 + f1.setText(str1);//放入文本框中 + } + else { + place = false; + str2 += action;//切换下一个数 + f1.setText(str2);//显示下一个数 + } + } + else if(m=='+' || m =='-' || m=='×' || m=='÷' ) + { operate = m; + } + else if(m=='=') { + double a = Double.parseDouble(str1); + double b = Double.parseDouble(str2); + if(operate=='+') { + c=Math1.add(a,b); + f1.setText(c+""); + + } + else if(operate=='-') { + c=Math1.subtract(a,b); + f1.setText(c+""); + + } + else if(operate=='×') { + c= Math1.multiply(a,b); + f1.setText(c+""); + + } + else if(operate=='÷') { + c=Math1.divide(a,b); + f1.setText(c+""); + + } + str1=(c+""); + str2=""; + } + + //其他按键,字符串 + else if(action =="del" || action == "CE" ||action =="sqrt"||action == "^2") { + //清空 + if(action=="CE") { + f1.setText("0"); + str1=""; + str2=""; + operate='~';//为下一次计算做准备 + } + //回删 + else if(action=="del") { + if(place) { + str1=str1.substring(0,str1.length()-1); + f1.setText(str1); + } + else + {str2=str2.substring(0,str2.length()-1); + f1.setText(str2);} + } + + //平方 + else if(action=="^2") { + if(place) { + double a = Double.parseDouble(str1); + double result=a*a; + str1=result+""; + f1.setText(str1); + } + else + {double a = Double.parseDouble(str2); + double result=a*a; + str2=result+""; + f1.setText(str2);} + } + //开方 + else if(action=="sqrt") { + if(place) { + double a = Double.parseDouble(str1); + str1=Math.sqrt(a)+""; + f1.setText(str1); + } + else + { + double a = Double.parseDouble(str2); + str2=Math.sqrt(a)+""; + f1.setText(str2); + } + } + } + } + } + + + + diff --git a/src/java2022spring/Math1.java b/src/java2022spring/Math1.java new file mode 100644 index 0000000000000000000000000000000000000000..1b605d37dc44bcc8e93b8692659024b03e467086 --- /dev/null +++ b/src/java2022spring/Math1.java @@ -0,0 +1,35 @@ +package java2022spring; +import java.math.*; +//进行加减乘除计算 + +public class Math1 { + //为一个double类型创建对象,精准小数 + private static BigDecimal getBigDecimal(double number) { + return new BigDecimal(number); + } + //加法 + public static double add(double a,double b) { + BigDecimal left = getBigDecimal(a); + BigDecimal right = getBigDecimal(b); + return left.add(right).doubleValue(); + } + //减法 + public static double subtract(double a,double b) { + BigDecimal left = getBigDecimal(a); + BigDecimal right = getBigDecimal(b); + return left.subtract(right).doubleValue(); + } + //乘法 + public static double multiply(double a,double b) { + BigDecimal left = getBigDecimal(a); + BigDecimal right = getBigDecimal(b); + return left.multiply(right).doubleValue(); + } + //除法 + public static double divide(double a,double b) { + BigDecimal left = getBigDecimal(a); + BigDecimal right = getBigDecimal(b); + return left.divide(right).doubleValue(); + } + } + diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29bfc0e5f50fdedcd21b504e77b529d48b6..de58acfb17e209f2227f4eedb447672e9f1ea9f7 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,7 +1,10 @@ package java2022spring; +import java.awt.*; +import javax.swing.*; public class Test { public static void main(String[] args) { - System.out.println("Hello world!"); + GoCaculator use = new GoCaculator(); + use.go(); } }