From 1f19aaa71cc640f65d85c527dc7560c7efb921ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=B3=E5=B0=8F=E5=A6=8D?= <瓒呰秴@LAPTOP-B0DH1RJ6> Date: Thu, 10 Jun 2021 17:59:55 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator.java | 283 ++++++++++++++++++++++++++++++++++++++++++++ DateComparison.java | 96 +++++++++++++++ Function.java | 51 ++++++++ MainClass.java | 7 ++ 4 files changed, 437 insertions(+) create mode 100644 Calculator.java create mode 100644 DateComparison.java create mode 100644 Function.java create mode 100644 MainClass.java diff --git a/Calculator.java b/Calculator.java new file mode 100644 index 0000000..57f6505 --- /dev/null +++ b/Calculator.java @@ -0,0 +1,283 @@ +package jisuanji; + +import javax.swing.*; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.awt.*; +import java.awt.event.*; +import java.io.IOException; +public class Calculator extends JFrame implements ActionListener{ + JMenuBar menubar; + JMenu menu; + JMenuItem item1,item2; + String[] keys = {"π","%","√x","X^2","1/x","n!","10^x","|x|","+","-","X","÷","7","8","9","DEL","4","5","6","C","1","2","3","CE","±","0",".","="}; + JButton buttons[] = new JButton[keys.length]; + JTextField resultText = new JTextField("0"); + String operator = "="; + public double resultNum = 0.0; + private boolean firstDigit = true; + private boolean Flag = true; + + public Calculator(){ + init(); + setTitle("Calculator"); + setBounds(500, 300, 420, 520); + setVisible(true); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + public void makeKeys(){ + JPanel keysPanel = new JPanel(); + keysPanel.setLayout(new GridLayout(8, 4, 2, 3)); + for(int i = 0; i < keys.length; i++) { + buttons[i] = new JButton(keys[i]); + keysPanel.add(buttons[i]); + buttons[i].setBackground(new Color(232, 232, 232)); + buttons[i].setFont(new Font("Arial", Font.PLAIN, 17)); + buttons[i].setBorderPainted(false); + buttons[i].addActionListener(this); + } + for(int i = 0; i <12 ; i++) { + buttons[i].setForeground(Color.PINK);} + buttons[11].setForeground(Color.PINK); + buttons[15].setForeground(Color.PINK); + buttons[19].setForeground(Color.PINK); + buttons[23].setForeground(Color.PINK); + buttons[24].setForeground(Color.PINK); + buttons[15].setForeground(Color.BLUE); + buttons[19].setForeground(Color.BLUE); + buttons[23].setForeground(Color.BLUE); + buttons[26].setForeground(Color.RED); + buttons[27].setBackground(Color.BLUE); + for(int i = 12; i < 15; i++) { + buttons[i].setBackground(Color.YELLOW); + } + for(int i = 16; i < 19; i++) { + buttons[i].setBackground(Color.YELLOW); + } + for(int i = 20; i < 23; i++) { + buttons[i].setBackground(Color.YELLOW); + } + buttons[25].setBackground(Color.YELLOW); + keysPanel.setBackground(new Color(181, 181, 181)); + add(keysPanel,BorderLayout.CENTER); + } + public void makeResult(){ + JPanel resultPanel = new JPanel(); + resultPanel.setLayout(new BorderLayout()); + resultPanel.add(resultText); + resultText.setFont(new Font("宋体", Font.PLAIN, 42)); + resultText.setBorder(null); + resultText.setEditable(false); + resultText.setHorizontalAlignment(JTextField.RIGHT); + resultText.setBackground(new Color(181, 181, 181)); + add(resultPanel,BorderLayout.NORTH); + } + public void actionPerformed(ActionEvent e){ + Object key = e.getSource(); + String command =e.getActionCommand(); + String regex="[1234567890.]+"; + if (key==buttons[15]) { + Del(); + } + else if (key==buttons[19]) { + CE(); + } + else if (key==buttons[23]) { + resultText.setText("0"); + firstDigit = true; + String operator = "="; + } + else if(key==buttons[0]){ + resultText.setText(resultText.getText() + command); + } + else if (command.matches(regex)){ + if (firstDigit) { + resultText.setText(command); + } + else if ((command.equals(".")) && (resultText.getText().indexOf(".") < 0)) { + resultText.setText(resultText.getText() + "."); + } + else if (!command.equals(".")) { + resultText.setText(resultText.getText() + command); + } + firstDigit = false; + } + + else if(key==buttons[0] || key==buttons[1] || key==buttons[2] || + key==buttons[3] || key==buttons[4] || key==buttons[5] || + key==buttons[6] || key==buttons[7] || key==buttons[15]|| + key==buttons[19]|| key==buttons[23]|| key==buttons[24]|| key==buttons[25] ) { + Operator1(command); + } + else if(key==buttons[8] || key==buttons[9] || key==buttons[10] || + key==buttons[11]|| key==buttons[27]) { + Operator2(command); + } + else if(key==item1){ + Function function=new Function(); + } + else if(key==item2){ + DateComparison dateComparison=new DateComparison(); + } + } + + public void Del(){ + String content=resultText.getText(); + int k =content.length(); + content=content.substring(0,k-1); + if(k>=0){ + if(k==0){ + resultText.setText("0"); + firstDigit = true; + operator = "="; + } + else { + resultText.setText(content); + } + } + } + public void CE(){ + resultText.setText("0"); + firstDigit = true; + operator = "="; + } + + public double getNum(){ + if(resultText.getText().indexOf("π")>0){ + if(resultText.getText().indexOf("π")==0){ + return Math.PI; + } + else{ + String temp=resultText.getText(); + temp=temp.substring(0,temp.length()-1); + return Double.parseDouble(temp)*Math.PI; + } + } + else{ + return Double.parseDouble(resultText.getText()); + } + } + public void Operator1(String order){ + operator =order; + double n=getNum(); + if (operator.equals("1/x")) { + if (n == 0) { + resultText.setText("0不能做除数"); + } + else{ + resultNum = 1.0 / n; + resultText.setText(String.valueOf(resultNum)); + } + } + else if (operator.equals("√x")) { + + if ((resultNum < 0)||(n<0)) { + resultText.setText("负数不能开方"); + } + else { + resultNum = Math.sqrt(n); + resultText.setText(String.valueOf(resultNum)); + } + } + else if (operator.equals("X^2")) { + resultNum = n*n; + resultText.setText(String.valueOf(resultNum)); + } + else if (operator.equals("%")) { + resultNum = n / 100; + resultText.setText(String.valueOf(resultNum)); + } + else if (operator.equals("±")) { + resultNum = n * (-1); + resultText.setText(String.valueOf(resultNum)); + if (n == 0) { + resultText.setText("0"); + } + } + else if (operator.equals("10^x")){ + double i = getNum(); + double sum=1; + if(i==0){ + resultText.setText(String.valueOf(sum)); + } + else{ + for(int k=1;k<=(int)i;k++){ + sum *=10; + resultText.setText(String.valueOf(sum)); + } + } + } + else if (operator.equals("|x|")){ + double i = getNum(); + resultText.setText(String.valueOf(Math.abs(i))); + } + else if (operator.equals("n!")){ + double i = getNum(); + if ((i%2==0)||(i%2==1)){ + int j =(int)i; + int result =1; + for (int k =1;k<=j;k++){ + result *=k; + } + resultText.setText(String.valueOf(result)); + } + else{ + resultText.setText("无法进行阶乘") ; + } + } + firstDigit = true; + } + public void Operator2(String command) { + if (operator.equals("÷")) { + if (getNum() == 0.0) { + resultText.setText("fault"); + Flag=false; + } + else { + resultNum /=getNum(); + } + } + else if (operator.equals("+")) { + + resultNum += getNum(); + } + else if (operator.equals("-")) { + + resultNum -= getNum(); + } + else if (operator.equals("X")) { + + resultNum *= getNum(); + } + else if (operator.equals("=")) { + resultNum=getNum(); + } + if (Flag) { + resultText.setText(String.valueOf(resultNum)); + } + operator = command; + firstDigit = true; + Flag = true; + } + public void init(){ + setLayout(new BorderLayout()); + Icon icon=new ImageIcon("菜单.png"); + menubar=new JMenuBar(); + menu=new JMenu("其他"); + menu.setIcon(icon); + item1=new JMenuItem("功能介绍"); + item2=new JMenuItem("比较日期大小"); + item1.addActionListener(this); + item2.addActionListener(this); + item1.setAccelerator(KeyStroke.getKeyStroke("KeyEvent.VK_b, InputEvent.CTRL_MASK")); + item2.setAccelerator(KeyStroke.getKeyStroke("KeyEvent.VK_S, InputEvent.CTRL_MASK")); + menu.add(item1); + menu.add(item2); + menubar.add(menu); + setJMenuBar(menubar); + makeKeys(); + makeResult(); + } +} + diff --git a/DateComparison.java b/DateComparison.java new file mode 100644 index 0000000..4d28588 --- /dev/null +++ b/DateComparison.java @@ -0,0 +1,96 @@ +package jisuanji; + +import javax.swing.*; +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.*; +public class DateComparison extends JFrame implements ActionListener{ + int []a=new int[3]; + int []b=new int[3]; + JTextField field1=new JTextField(10); + JTextField field2=new JTextField(10); + JButton button=new JButton("开始比较"); + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(); + JLabel label1=new JLabel(); + JLabel label2=new JLabel(); + JLabel label3=new JLabel(); + DateComparison(){ + init(); + setTitle("DateComparison"); + setBounds(600, 360, 480, 220); + setVisible(true); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);} + public void init(){ + setLayout(new BorderLayout()); + panel1.setLayout(new FlowLayout()); + panel2.setLayout(new FlowLayout()); + label1.setText("and"); + label2.setText(null); + label3.setText("请在文本框输入需要比较的日期,格式:2021.6.11"); + label1.setFont(new Font("宋体", Font.PLAIN, 20)); + label2.setFont(new Font("宋体", Font.PLAIN, 20)); + label3.setFont(new Font("宋体", Font.PLAIN, 18)); + button.addActionListener(this); + panel1.add(field1); + panel1.add(label1); + panel1.add(field2); + panel1.add(button); + panel2.add(label2); + add(label3,BorderLayout.NORTH); + add(panel1,BorderLayout.CENTER); + add(panel2,BorderLayout.SOUTH); + } + public void actionPerformed(ActionEvent e) { + if(e.getSource()==button){ + a=getDate(field1.getText()); + b=getDate(field2.getText()); + String x=work(a,b); + label2.setText(x); + } + } + public int[] getDate(String str){ + int []c=new int[3]; + Scanner scanner=new Scanner(str); + scanner.useDelimiter("[.]+"); + int k=0; + while(scanner.hasNext()){ + c[k++]=scanner.nextInt(); + } + return c; + } + public String work(int []a,int []b){ + String re = null; + String rx; + int yearOne=a[0]; + int monthOne=a[1]; + int dayOne=a[2]; + int yearTwo=b[0]; + int monthTwo=b[1]; + int dayTwo=b[2]; + Calendar calendar=Calendar.getInstance(); + calendar.set(yearOne, monthOne-1, dayOne); + long time1=calendar.getTimeInMillis(); + calendar.set(yearTwo, monthTwo-1, dayTwo); + long time2=calendar.getTimeInMillis(); + Date date1=new Date(time1); + Date date2=new Date(time2); + if(date2.equals(date1)){ + re="两个日期相同,"; + } + else if(date2.after(date1)){ + re="您输入的第二个日期大于第一个日期,"; + } + else if(date2.before(date1)){ + re="您输入的第二个日期小于第一个日期,"; + } + long day=Math.abs(time1-time2)/(1000*60*60*24); + rx= re+"相差"+String.valueOf(day)+"天"; + return rx; + } +} + + diff --git a/Function.java b/Function.java new file mode 100644 index 0000000..70a2a47 --- /dev/null +++ b/Function.java @@ -0,0 +1,51 @@ +package jisuanji; + +import javax.swing.*; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Component; +import java.awt.FlowLayout; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.*; +public class Function extends JFrame implements ActionListener{ + public void actionPerformed(ActionEvent e) { + Function function=new Function(); + } + Box box; + JPanel panel=new JPanel(); + JLabel label1=new JLabel(); + JLabel label2=new JLabel(); + JLabel label3=new JLabel(); + Function(){ + setLayout(new BorderLayout()); + box=Box.createVerticalBox(); + label1=new JLabel("可进行加、减、乘、除等简单的四则简单运算; "); + label2=new JLabel("还能进行乘方、开方、倒数、求绝对值、求阶乘、百分号运算、正负数运算等方面的复杂运算。"); + label3=new JLabel("祝您使用愉快!"); + box.add(label1); + box.add(label2); + box.add(label3); + label1.setFont(new Font("仿宋",Font.BOLD,20)); + label2.setFont(new Font("仿宋", Font.BOLD, 20)); + label3.setFont(new Font("仿宋", Font.BOLD, 20)); + add(box,BorderLayout.CENTER); + setTitle("Function"); + setBounds(200, 200,1000, 200); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + setVisible(true); + } +} + + + + + + + + + + + + diff --git a/MainClass.java b/MainClass.java new file mode 100644 index 0000000..0eb7898 --- /dev/null +++ b/MainClass.java @@ -0,0 +1,7 @@ +package jisuanji; + +public class MainClass { + public static void main(String[] args) { + Calculator calculator=new Calculator(); + } +} -- Gitee From c046a123402c9c555cd90e09c77256d26a200a83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=B3=E5=B0=8F=E5=A6=8D?= <瓒呰秴@LAPTOP-B0DH1RJ6> Date: Thu, 10 Jun 2021 18:29:38 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator.java | 2 +- DateComparison.java | 2 +- Function.java | 2 +- MainClass.java | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Calculator.java b/Calculator.java index 57f6505..a7dd5fe 100644 --- a/Calculator.java +++ b/Calculator.java @@ -28,7 +28,7 @@ public class Calculator extends JFrame implements ActionListener{ } public void makeKeys(){ JPanel keysPanel = new JPanel(); - keysPanel.setLayout(new GridLayout(8, 4, 2, 3)); + keysPanel.setLayout(new GridLayout(8, 4, 2, 4)); for(int i = 0; i < keys.length; i++) { buttons[i] = new JButton(keys[i]); keysPanel.add(buttons[i]); diff --git a/DateComparison.java b/DateComparison.java index 4d28588..36ab45b 100644 --- a/DateComparison.java +++ b/DateComparison.java @@ -92,5 +92,5 @@ public class DateComparison extends JFrame implements ActionListener{ return rx; } } - +// diff --git a/Function.java b/Function.java index 70a2a47..b12d40b 100644 --- a/Function.java +++ b/Function.java @@ -29,7 +29,7 @@ public class Function extends JFrame implements ActionListener{ box.add(label3); label1.setFont(new Font("仿宋",Font.BOLD,20)); label2.setFont(new Font("仿宋", Font.BOLD, 20)); - label3.setFont(new Font("仿宋", Font.BOLD, 20)); + label3.setFont(new Font("仿宋", Font.BOLD, 18)); add(box,BorderLayout.CENTER); setTitle("Function"); setBounds(200, 200,1000, 200); diff --git a/MainClass.java b/MainClass.java index 0eb7898..228bab0 100644 --- a/MainClass.java +++ b/MainClass.java @@ -5,3 +5,4 @@ public class MainClass { Calculator calculator=new Calculator(); } } +// \ No newline at end of file -- Gitee From 1060edf48bc1ed92c3ff4d3c8d03fa15180e5a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=B3=E5=B0=8F=E5=A6=8D?= <瓒呰秴@LAPTOP-B0DH1RJ6> Date: Thu, 10 Jun 2021 18:33:12 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Calculator.java | 2 +- DateComparison.java | 2 +- Function.java | 2 +- MainClass.java | 3 +-- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Calculator.java b/Calculator.java index a7dd5fe..5053d1e 100644 --- a/Calculator.java +++ b/Calculator.java @@ -61,7 +61,7 @@ public class Calculator extends JFrame implements ActionListener{ buttons[25].setBackground(Color.YELLOW); keysPanel.setBackground(new Color(181, 181, 181)); add(keysPanel,BorderLayout.CENTER); - } + } public void makeResult(){ JPanel resultPanel = new JPanel(); resultPanel.setLayout(new BorderLayout()); diff --git a/DateComparison.java b/DateComparison.java index 36ab45b..26af4f6 100644 --- a/DateComparison.java +++ b/DateComparison.java @@ -90,7 +90,7 @@ public class DateComparison extends JFrame implements ActionListener{ long day=Math.abs(time1-time2)/(1000*60*60*24); rx= re+"相差"+String.valueOf(day)+"天"; return rx; - } + } } // diff --git a/Function.java b/Function.java index b12d40b..327b660 100644 --- a/Function.java +++ b/Function.java @@ -12,7 +12,7 @@ import java.util.*; public class Function extends JFrame implements ActionListener{ public void actionPerformed(ActionEvent e) { Function function=new Function(); - } + } Box box; JPanel panel=new JPanel(); JLabel label1=new JLabel(); diff --git a/MainClass.java b/MainClass.java index 228bab0..75c065a 100644 --- a/MainClass.java +++ b/MainClass.java @@ -3,6 +3,5 @@ package jisuanji; public class MainClass { public static void main(String[] args) { Calculator calculator=new Calculator(); - } + } } -// \ No newline at end of file -- Gitee