From 45f5fa0d0aa43c86978ffa2ec1b19b4203e5b832 Mon Sep 17 00:00:00 2001 From: 86171 <86171@LAPTOP-IFQ8Q1G7> Date: Wed, 9 Jun 2021 16:27:33 +0800 Subject: [PATCH 1/5] =?UTF-8?q?202025710419=E7=A7=A6=E7=8E=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/CalculatorFrame.java | 191 ++++++++++++++++++++++++ src/java2020spring/NumberButton.java | 26 ++++ src/java2020spring/OperateBack.java | 52 +++++++ src/java2020spring/OperateClear.java | 18 +++ src/java2020spring/OperateDot.java | 18 +++ src/java2020spring/OperateEquality.java | 18 +++ src/java2020spring/OperateNumber.java | 39 +++++ src/java2020spring/OperateSin.java | 20 +++ src/java2020spring/OperateSymbol.java | 15 ++ src/java2020spring/OperateZhengfu.java | 18 +++ src/java2020spring/SymbolButton.java | 26 ++++ 11 files changed, 441 insertions(+) create mode 100644 src/java2020spring/CalculatorFrame.java create mode 100644 src/java2020spring/NumberButton.java create mode 100644 src/java2020spring/OperateBack.java create mode 100644 src/java2020spring/OperateClear.java create mode 100644 src/java2020spring/OperateDot.java create mode 100644 src/java2020spring/OperateEquality.java create mode 100644 src/java2020spring/OperateNumber.java create mode 100644 src/java2020spring/OperateSin.java create mode 100644 src/java2020spring/OperateSymbol.java create mode 100644 src/java2020spring/OperateZhengfu.java create mode 100644 src/java2020spring/SymbolButton.java diff --git a/src/java2020spring/CalculatorFrame.java b/src/java2020spring/CalculatorFrame.java new file mode 100644 index 0000000..c2f81c3 --- /dev/null +++ b/src/java2020spring/CalculatorFrame.java @@ -0,0 +1,191 @@ +package java2020spring; + +import java.io.*; +import java.util.LinkedList; +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.border.*; + +public class CalculatorFrame extends JFrame implements ActionListener { + JButton sin,Backspace,Equals,Zero,Flex,zhengfu; + JButton saveButton,clearButton,copyButton; + JTextField resultDisplay; //当前输入与运算结果显示。 + JTextField showProcess; // 当前计算过程显示。 + JTextArea showStep; //计算步骤显示。 + LinkedListlist; //存放第一个数,操作符号与第二个数。 + NumberButton numberButton[]; + SymbolButton symbolButton[]; + OperateNumber operateNumber; //处理ActionEvent事件。 + OperateSymbol operateSymbol; + OperateBack operateBack; + OperateClear operateClear; + OperateEquality operateEquality; + OperateDot operateDot; + OperateZhengfu operateZhengFu; + OperateSin operateSin; + + public CalculatorFrame() { //构造方法 + setTitle("简单计算器"); + setVisible(true); //窗口可见。 + setBounds(120,60,600,300); //窗口的初始位置与长与宽; + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击关闭后结束窗口所在的应用程序。 + JPanel panelLeft; + JPanel panelRight; //面板 + list=new LinkedList(); + resultDisplay=new JTextField(10); + resultDisplay.setHorizontalAlignment(JTextField.RIGHT); //对齐方式 + resultDisplay.setFont(new Font("New TimesRoman",Font.BOLD,16));//字体样式,字体格式 ,字体大小 + resultDisplay.setForeground(Color.black); //字体颜色 + resultDisplay.setBackground(Color.white); //背景色 + resultDisplay.setEditable(false); //选项不可用 + resultDisplay.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));//显示半个边框 + showProcess=new JTextField(); + showProcess.setHorizontalAlignment(JTextField.CENTER); //对齐方式 + showProcess.setFont(new Font("Arial",Font.BOLD,16));//字体样式,字体格式 ,字体大小 + showProcess.setBackground(Color.blue); + showProcess.setEditable(false); //选项不可用 + showStep=new JTextArea(); + showStep.setEditable(false); //选项不可用 + showStep.setFont(new Font("宋体",Font.PLAIN,16)); + showStep.setForeground(Color.black); //设置字体颜色 + numberButton=new NumberButton[10]; + operateNumber = new OperateNumber(list,resultDisplay,showProcess); + for(int i=0;i<=9;i++) { + numberButton[i]=new NumberButton(i); + numberButton[i].setFont(new Font("Arial",Font.BOLD,20)); //数字样式,格式,大小 + numberButton[i].addActionListener(operateNumber); //shuziButton将operateNumber注册为自己的监视器 + } + symbolButton=new SymbolButton[4]; + operateSymbol=new OperateSymbol(list,resultDisplay,showProcess,showStep); + String fuhao1[]= {"+","-","*","/"}; + for(int i=0;i<4;i++) { + symbolButton[i]=new SymbolButton(fuhao1[i]); + symbolButton[i].setFont(new Font("Arial",Font.BOLD,20)); + symbolButton[i].addActionListener(operateSymbol); //symbolButton将operateSymbol注册为自己的监视器 + } + + Flex=new JButton("."); + operateDot=new OperateDot(list,resultDisplay,showProcess); + Flex.addActionListener(operateDot); + + zhengfu=new JButton("+/-"); + operateZhengFu=new OperateZhengfu(list,resultDisplay,showProcess); + zhengfu.addActionListener(operateZhengFu); + + Zero=new JButton("归零"); + operateClear=new OperateClear(list,resultDisplay,showProcess); + Zero.addActionListener(operateClear); + + Backspace=new JButton("退格"); + operateBack=new OperateBack(list,resultDisplay,showProcess); + Backspace.addActionListener(operateBack); + + Equals=new JButton("="); + operateEquality=new OperateEquality(list,resultDisplay,showProcess,showStep); + Equals.addActionListener(operateEquality); + + sin=new JButton("sin"); + operateSin=new OperateSin(list,resultDisplay,showProcess,showStep); + sin.addActionListener(operateSin); + + /*Zero.setForeground(Color.black); //颜色 + Backspace.setForeground(Color.black); + Equals.setForeground(Color.black); + Flex.setForeground(Color.black); + zhengfu.setForeground(Color.black); + sin.setForeground(Color.black);*/ + panelLeft=new JPanel(); + panelRight=new JPanel(); + panelLeft.setLayout(new BorderLayout()); //panelZuo使用BorderLayout布局。 + JPanel centerLeft=new JPanel(); //建一个放在panelZuo中间部分的centerZuo容器放数字与符号。 + panelLeft.add(resultDisplay,BorderLayout.NORTH); //运算结果放在panelZuo容器的北边。 + panelLeft.add(centerLeft,BorderLayout.CENTER); + centerLeft.setLayout(new GridLayout(5,4)); //centerZuo使用GridLayout布局; + centerLeft.add(symbolButton[0]); + centerLeft.add(symbolButton[1]); + centerLeft.add(symbolButton[2]); + centerLeft.add(symbolButton[3]); + centerLeft.add(numberButton[1]); + centerLeft.add(numberButton[2]); + centerLeft.add(numberButton[3]); + centerLeft.add(zhengfu); + centerLeft.add(numberButton[4]); + centerLeft.add(numberButton[5]); + centerLeft.add(numberButton[6]); + centerLeft.add(sin); + centerLeft.add(numberButton[7]); + centerLeft.add(numberButton[8]); + centerLeft.add(numberButton[9]); + centerLeft.add(Backspace); + centerLeft.add(numberButton[0]); + centerLeft.add(Flex); + centerLeft.add(Equals); + centerLeft.add(Zero); + panelRight.setLayout(new BorderLayout()); //panelYou使用BorderLayout布局。 + panelRight.add(showProcess,BorderLayout.NORTH); // 运算过程放在panelYou的北边. + + saveButton=new JButton("保存"); + copyButton=new JButton("复制"); + clearButton=new JButton("清除"); + saveButton.setForeground(Color.blue); //颜色 + copyButton.setForeground(Color.red); + clearButton.setForeground(Color.gray); + saveButton.setToolTipText("保存计算过程到文件里"); //设置鼠标停留在saveButton上时显示的信息; + copyButton.setToolTipText("复制选中计算过程"); //设置鼠标停留在copyButton上时显示的信息; + clearButton.setToolTipText("清除计算过程"); //设置鼠标停留在clearButton上时显示的信息; + saveButton.addActionListener(this); + clearButton.addActionListener(this); + copyButton.addActionListener(this); + + panelRight.add(new JScrollPane(showStep),BorderLayout.CENTER);//滚动窗格 + JPanel southRight=new JPanel(); //建一个放在panelYou南边的southYou容器存放3个文字按钮。 + southRight.add(saveButton); //放入按钮 + southRight.add(copyButton); + southRight.add(clearButton); + panelRight.add(southRight,BorderLayout.SOUTH);//将southYou放在panelYou南边。 + JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,panelLeft,panelRight);//界面一分为二; + add(split,BorderLayout.CENTER); + validate(); + } + + public void actionPerformed(ActionEvent e) { + if(e.getSource()==copyButton) { //如果按下复制按钮 + showStep.copy(); //实现复制 + } + if(e.getSource()==clearButton) { //如果按下清除按钮 + showStep.setText(null); //实现清除 + } + if(e.getSource()==saveButton){ //如果按下保存按钮 + JFileChooser chooser=new JFileChooser(); //文件选择器 + int state = chooser.showSaveDialog(null); //显示文件保存对话框 + File file = chooser.getSelectedFile(); //输入要保存的文件名; + if(file!=null&&state==JFileChooser.APPROVE_OPTION) { //若选好了文件. + try { + String baoCun=showStep.getText(); //将运算步骤文本赋值给baoCun; + StringReader read = new StringReader(baoCun); //生成字符流。 + BufferedReader in=new BufferedReader(read); //生成缓冲输入流。 + FileWriter out=new FileWriter(file); //根据选好的文件,生成FileWriter对象; + BufferedWriter out1=new BufferedWriter(out); //生成缓冲输出流; + String str=null; + while((str=in.readLine())!=null) { //判断是否有字符 + out1.write(str); //把内容写到文件中 + out1.newLine(); //新的一行 + } + in.close(); //关闭文件; + out1.close(); + }catch(IOException el){ + + } + + } + } + + } + public static void main(String args[]) { + new CalculatorFrame(); + } + + + } + diff --git a/src/java2020spring/NumberButton.java b/src/java2020spring/NumberButton.java new file mode 100644 index 0000000..7fcb512 --- /dev/null +++ b/src/java2020spring/NumberButton.java @@ -0,0 +1,26 @@ +package java2020spring; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; + +public class NumberButton extends JButton{ + int number; + + public NumberButton(int number) { + super(""+number); + } + + /*public void setFont(Font font) { + // TODO 自动生成的方法存根 + + }*/ + public int getNumber() { + return number; + } + void addActionListener(OperateNumber operateNumber) { + // TODO 自动生成的方法存根 + + } + +} diff --git a/src/java2020spring/OperateBack.java b/src/java2020spring/OperateBack.java new file mode 100644 index 0000000..79f7fa2 --- /dev/null +++ b/src/java2020spring/OperateBack.java @@ -0,0 +1,52 @@ +package java2020spring; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.LinkedList; +import javax.swing.JTextField; + +public class OperateBack implements ActionListener{ + LinkedList list; + JTextField resultDisplay; + JTextField showProcess; + + public OperateBack(LinkedList list1, JTextField one, JTextField two) { + // TODO 自动生成的构造函数存根 + list=list1; + resultDisplay=one; + showProcess=two; + } + + public void actionPerformed(ActionEvent e) { + if(list.size()==1) { + String num=(String)list.getFirst();//获取 + if(num.length()>=1){ + num=num.substring(0,num.length()-1); + list.set(0, num); + resultDisplay.setText(""+num); + showProcess.setText(""+num); + } + else { + list.remove(); + resultDisplay.setText("0"); + showProcess.setText("0"); + } + } + else if(list.size()==3) { + String num2=(String)list.getLast(); + if(num2.length()>=1) { + num2=num2.substring(0,num2.length()-1); + list.set(0, num2); + resultDisplay.setText(""+num2); + showProcess.setText(""+num2); + } + else { + list.remove(); + resultDisplay.setText("0"); + showProcess.setText("0"); + } + + } + + } + +} diff --git a/src/java2020spring/OperateClear.java b/src/java2020spring/OperateClear.java new file mode 100644 index 0000000..2678c8b --- /dev/null +++ b/src/java2020spring/OperateClear.java @@ -0,0 +1,18 @@ +package java2020spring; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.LinkedList; +import javax.swing.JTextField; +public class OperateClear implements ActionListener{ + + public OperateClear(LinkedList list, JTextField resultDisplay, JTextField showProcess) { + // TODO 自动生成的构造函数存根 + } + + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + + } + +} diff --git a/src/java2020spring/OperateDot.java b/src/java2020spring/OperateDot.java new file mode 100644 index 0000000..b2fc8c6 --- /dev/null +++ b/src/java2020spring/OperateDot.java @@ -0,0 +1,18 @@ +package java2020spring; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.LinkedList; +import javax.swing.JTextField; +public class OperateDot implements ActionListener{ + + public OperateDot(LinkedList list, JTextField resultDisplay, JTextField showProcess) { + // TODO 自动生成的构造函数存根 + } + + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + + } + +} diff --git a/src/java2020spring/OperateEquality.java b/src/java2020spring/OperateEquality.java new file mode 100644 index 0000000..a3bcb50 --- /dev/null +++ b/src/java2020spring/OperateEquality.java @@ -0,0 +1,18 @@ +package java2020spring; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.LinkedList; + +import javax.swing.JTextArea; +import javax.swing.JTextField; +public class OperateEquality implements ActionListener{ + + public OperateEquality(LinkedList list, JTextField resultDisplay, JTextField showProcess, JTextArea showStep) { + // TODO 自动生成的构造函数存根 + } + + public void actionPerformed(ActionEvent e) { + + } + +} diff --git a/src/java2020spring/OperateNumber.java b/src/java2020spring/OperateNumber.java new file mode 100644 index 0000000..755ac3b --- /dev/null +++ b/src/java2020spring/OperateNumber.java @@ -0,0 +1,39 @@ +package java2020spring; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.LinkedList; +import javax.swing.JTextField; +public class OperateNumber implements ActionListener{ + LinkedList list; + JTextField resultDisplay;//显示输入、输出结果 + JTextField showProcess;//显示运算过程 + + OperateNumber(LinkedList list1, JTextField one, JTextField two) { + list=list1; + resultDisplay=one; + showProcess=two; + } + + public void actionPerformed(ActionEvent e) { + NumberButton n=(NumberButton)e.getSource(); + if(list.size()==0) { + int num=n.getNumber(); + list.add(""+num); + //resultDisplay.() + } + + else if(list.size()==1) { + + } + + else if(list.size()==2) { + + } + + else if(list.size()==3) { + + } + } + +} diff --git a/src/java2020spring/OperateSin.java b/src/java2020spring/OperateSin.java new file mode 100644 index 0000000..66117a3 --- /dev/null +++ b/src/java2020spring/OperateSin.java @@ -0,0 +1,20 @@ +package java2020spring; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.LinkedList; + +import javax.swing.JTextArea; +import javax.swing.JTextField; +public class OperateSin implements ActionListener{ + + public OperateSin(LinkedList list, JTextField resultDisplay, JTextField showProcess, JTextArea showStep) { + // TODO 自动生成的构造函数存根 + } + + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + + } + +} diff --git a/src/java2020spring/OperateSymbol.java b/src/java2020spring/OperateSymbol.java new file mode 100644 index 0000000..b6c1bd9 --- /dev/null +++ b/src/java2020spring/OperateSymbol.java @@ -0,0 +1,15 @@ +package java2020spring; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.LinkedList; + +import javax.swing.JTextArea; +import javax.swing.JTextField; +public class OperateSymbol { + + public OperateSymbol(LinkedList list, JTextField resultDisplay, JTextField showProcess, JTextArea showStep) { + // TODO 自动生成的构造函数存根 + } + +} diff --git a/src/java2020spring/OperateZhengfu.java b/src/java2020spring/OperateZhengfu.java new file mode 100644 index 0000000..758551a --- /dev/null +++ b/src/java2020spring/OperateZhengfu.java @@ -0,0 +1,18 @@ +package java2020spring; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.LinkedList; +import javax.swing.JTextField; +public class OperateZhengfu implements ActionListener{ + + public OperateZhengfu(LinkedList list, JTextField resultDisplay, JTextField showProcess) { + // TODO 自动生成的构造函数存根 + } + + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + + } + +} diff --git a/src/java2020spring/SymbolButton.java b/src/java2020spring/SymbolButton.java new file mode 100644 index 0000000..47ff683 --- /dev/null +++ b/src/java2020spring/SymbolButton.java @@ -0,0 +1,26 @@ +package java2020spring; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +public class SymbolButton extends JButton{ + + public SymbolButton(int i) { + // TODO 自动生成的构造函数存根 + } + + public SymbolButton(String string) { + // TODO 自动生成的构造函数存根 + } + + public void setFont(Font font) { + // TODO 自动生成的方法存根 + + } + + public void addActionListener(OperateSymbol operateSymbol) { + // TODO 自动生成的方法存根 + + } + +} -- Gitee From 3d0952076bc68f75164baa80c2087bf91d4947c2 Mon Sep 17 00:00:00 2001 From: 86171 <86171@LAPTOP-IFQ8Q1G7> Date: Wed, 9 Jun 2021 16:32:44 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E9=87=8C=E9=9D=A2=E6=9C=89=E9=94=99?= =?UTF-8?q?=E8=AF=AF=EF=BC=8C=E4=BC=9A=E7=BB=A7=E7=BB=AD=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/OperateNumber.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/java2020spring/OperateNumber.java b/src/java2020spring/OperateNumber.java index 755ac3b..f232d4d 100644 --- a/src/java2020spring/OperateNumber.java +++ b/src/java2020spring/OperateNumber.java @@ -20,10 +20,12 @@ public class OperateNumber implements ActionListener{ if(list.size()==0) { int num=n.getNumber(); list.add(""+num); - //resultDisplay.() + // resultDisplay.setText(""+num)e; } else if(list.size()==1) { + int num=n.getNumber(); + } -- Gitee From 56423fad6ab33a9bd249037b26d655fa9c05ea25 Mon Sep 17 00:00:00 2001 From: 86171 <86171@LAPTOP-IFQ8Q1G7> Date: Thu, 10 Jun 2021 14:42:18 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E8=BF=98=E6=B2=A1=E5=AE=8C=E6=88=90?= =?UTF-8?q?=EF=BC=8C=E4=BC=9A=E7=BB=A7=E7=BB=AD=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 164 ++++++++++++++++++++ src/java2020spring/CalculatorFrame.java | 191 ------------------------ src/java2020spring/MainClass.java | 13 ++ 3 files changed, 177 insertions(+), 191 deletions(-) create mode 100644 src/java2020spring/Calculator.java delete mode 100644 src/java2020spring/CalculatorFrame.java create mode 100644 src/java2020spring/MainClass.java diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java new file mode 100644 index 0000000..5003dea --- /dev/null +++ b/src/java2020spring/Calculator.java @@ -0,0 +1,164 @@ +package java2020spring; +import javax.swing.*; +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.text.NumberFormat; + + +public class Calculator { + JFrame frame; + JTextField textField; + JButton[] button; + JPanel panel; + JLabel label; + String data = "";//data:当前输入的数据 + boolean isLeftAvailable;// isLeftAvailable:判断数据应该向哪一个操作数中存储 + double left, right;//left, right:左右操作数 + String preOperator = ""; + + public void init(){//初始化 + + setMyFrame();//窗口 + setMyTextField();//输入框 + setMyButton();//按钮 + setMyLabel();//标签 + display();//显示窗口 + } + + public void display(){ //设置窗口 + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭 frame 窗口时退出程序 + frame.setVisible(true);// 设置 frame窗口可见 + } + + + private void setMyFrame(){//设置窗口 + frame = new JFrame(); + frame.setBounds(80,80,450,650);//设置面板位置和大小 + frame.setLayout(new BorderLayout()); + frame.setTitle("简单计算器");//设置标题 + frame.setVisible(true);//设置窗口为可视 + frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);//关闭窗口时退出程序 + frame.setResizable(false);//窗口不可调整大小 + + } + + + private void setMyTextField()//设置文本框 + { + textField = new JTextField("0"); + textField.setBounds(50,25,400,200);// 设置文本框的位置和大小 + textField.setFont(new Font("黑体", Font.BOLD, 35));// 设置文本框字体 + frame.add(textField,BorderLayout.NORTH);//将文本框放到窗口的北面 + } + + /** + * setMyButton: + * @description: 设置按键事件 + */ + private void setMyButton() // 设置按钮 + { + // 按钮文本 + String[] arr ={ "C","DEL","%","/", + "7","8","9","*", + "4","5","6","+", + "1","2","3","-", + "+/-","0",".","=", }; + button = new JButton[arr.length]; + panel = new JPanel(); + panel.setBounds(20, 90, 300, 280);//设置按钮的位置和大小 + panel.setLayout(new GridLayout(5, 4, 6, 6));//设置面板为网格布局 + + for(int i = 0; i < button.length; i++){ + button[i] = new JButton(arr[i]);// 创建按钮 + button[i].setFont(new Font("黑体", Font.BOLD, 20));// 设置按钮字体 + button[i].setBackground(Color.white); // 设置按钮背景颜色 + + if(button.equals("=")){// 设置 "="号的颜色 + button[i].setBackground(Color.BLUE); + } + + + int event = i;// 添加事件 + + /*设置鼠标监听*/ + + button[i].addMouseListener(new MouseAdapter() {// 点击按钮 + @Override + public void mouseClicked(MouseEvent e) {//获取按钮上的内容 + click(button[event].getText()); + } + + @Override + public void mouseEntered(MouseEvent e) { // 当鼠标进入组件事件时 + button[event].setFont(new Font("黑体", Font.CENTER_BASELINE, 35)); + button[event].setBackground(new Color(240, 255, 255)); + button[event].setForeground(Color.RED); + } + + @Override + public void mouseExited(MouseEvent e) {// 当鼠标离开组件事件时 + button[event].setFont(new Font("黑体", Font.CENTER_BASELINE, 20)); + button[event].setBackground(Color.WHITE); + button[event].setForeground(Color.BLACK); + } + }); + + panel.add(button[i]); // 将按钮添加到面板 + } + frame.add(panel,BorderLayout.CENTER); + } + + + private void setMyLabel(){// 设置标签 + + + } + + + + public void click(String option){//设置点击事件 + String operator = ""; + + if("1".equals(option)) { + data += "1"; + textField.setText(data); + } + else if("2".equals(option)) { + + } + + } + + + + + private void calculator(String operator){ + if("".equals(data)){// 如果 data 为空,不执行任何操作 + if("=".equals(operator)){// 用户只要输入 = ,无论前面的输入是否合法,结束此轮计算 + isLeftAvailable = false; + } + return; + } + + /* + * 第一次把 data 中的数据赋给 left + * 此后,每次运算都将 right 与 left 运算的结果放入 left 中,并显示 left + * 运算符 = 使用后可以重新向 left 中输入值 + * 每次获取到 data 中的值后清空 data + * + * 每次运算使用上一次传入的运算符,等号出现重新开始新的计算 + */ + + if(isLeftAvailable){ + right = Double.parseDouble(data); + data = "null"; + + + } + + + + } + +} diff --git a/src/java2020spring/CalculatorFrame.java b/src/java2020spring/CalculatorFrame.java deleted file mode 100644 index c2f81c3..0000000 --- a/src/java2020spring/CalculatorFrame.java +++ /dev/null @@ -1,191 +0,0 @@ -package java2020spring; - -import java.io.*; -import java.util.LinkedList; -import java.awt.*; -import java.awt.event.*; -import javax.swing.*; -import javax.swing.border.*; - -public class CalculatorFrame extends JFrame implements ActionListener { - JButton sin,Backspace,Equals,Zero,Flex,zhengfu; - JButton saveButton,clearButton,copyButton; - JTextField resultDisplay; //当前输入与运算结果显示。 - JTextField showProcess; // 当前计算过程显示。 - JTextArea showStep; //计算步骤显示。 - LinkedListlist; //存放第一个数,操作符号与第二个数。 - NumberButton numberButton[]; - SymbolButton symbolButton[]; - OperateNumber operateNumber; //处理ActionEvent事件。 - OperateSymbol operateSymbol; - OperateBack operateBack; - OperateClear operateClear; - OperateEquality operateEquality; - OperateDot operateDot; - OperateZhengfu operateZhengFu; - OperateSin operateSin; - - public CalculatorFrame() { //构造方法 - setTitle("简单计算器"); - setVisible(true); //窗口可见。 - setBounds(120,60,600,300); //窗口的初始位置与长与宽; - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击关闭后结束窗口所在的应用程序。 - JPanel panelLeft; - JPanel panelRight; //面板 - list=new LinkedList(); - resultDisplay=new JTextField(10); - resultDisplay.setHorizontalAlignment(JTextField.RIGHT); //对齐方式 - resultDisplay.setFont(new Font("New TimesRoman",Font.BOLD,16));//字体样式,字体格式 ,字体大小 - resultDisplay.setForeground(Color.black); //字体颜色 - resultDisplay.setBackground(Color.white); //背景色 - resultDisplay.setEditable(false); //选项不可用 - resultDisplay.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));//显示半个边框 - showProcess=new JTextField(); - showProcess.setHorizontalAlignment(JTextField.CENTER); //对齐方式 - showProcess.setFont(new Font("Arial",Font.BOLD,16));//字体样式,字体格式 ,字体大小 - showProcess.setBackground(Color.blue); - showProcess.setEditable(false); //选项不可用 - showStep=new JTextArea(); - showStep.setEditable(false); //选项不可用 - showStep.setFont(new Font("宋体",Font.PLAIN,16)); - showStep.setForeground(Color.black); //设置字体颜色 - numberButton=new NumberButton[10]; - operateNumber = new OperateNumber(list,resultDisplay,showProcess); - for(int i=0;i<=9;i++) { - numberButton[i]=new NumberButton(i); - numberButton[i].setFont(new Font("Arial",Font.BOLD,20)); //数字样式,格式,大小 - numberButton[i].addActionListener(operateNumber); //shuziButton将operateNumber注册为自己的监视器 - } - symbolButton=new SymbolButton[4]; - operateSymbol=new OperateSymbol(list,resultDisplay,showProcess,showStep); - String fuhao1[]= {"+","-","*","/"}; - for(int i=0;i<4;i++) { - symbolButton[i]=new SymbolButton(fuhao1[i]); - symbolButton[i].setFont(new Font("Arial",Font.BOLD,20)); - symbolButton[i].addActionListener(operateSymbol); //symbolButton将operateSymbol注册为自己的监视器 - } - - Flex=new JButton("."); - operateDot=new OperateDot(list,resultDisplay,showProcess); - Flex.addActionListener(operateDot); - - zhengfu=new JButton("+/-"); - operateZhengFu=new OperateZhengfu(list,resultDisplay,showProcess); - zhengfu.addActionListener(operateZhengFu); - - Zero=new JButton("归零"); - operateClear=new OperateClear(list,resultDisplay,showProcess); - Zero.addActionListener(operateClear); - - Backspace=new JButton("退格"); - operateBack=new OperateBack(list,resultDisplay,showProcess); - Backspace.addActionListener(operateBack); - - Equals=new JButton("="); - operateEquality=new OperateEquality(list,resultDisplay,showProcess,showStep); - Equals.addActionListener(operateEquality); - - sin=new JButton("sin"); - operateSin=new OperateSin(list,resultDisplay,showProcess,showStep); - sin.addActionListener(operateSin); - - /*Zero.setForeground(Color.black); //颜色 - Backspace.setForeground(Color.black); - Equals.setForeground(Color.black); - Flex.setForeground(Color.black); - zhengfu.setForeground(Color.black); - sin.setForeground(Color.black);*/ - panelLeft=new JPanel(); - panelRight=new JPanel(); - panelLeft.setLayout(new BorderLayout()); //panelZuo使用BorderLayout布局。 - JPanel centerLeft=new JPanel(); //建一个放在panelZuo中间部分的centerZuo容器放数字与符号。 - panelLeft.add(resultDisplay,BorderLayout.NORTH); //运算结果放在panelZuo容器的北边。 - panelLeft.add(centerLeft,BorderLayout.CENTER); - centerLeft.setLayout(new GridLayout(5,4)); //centerZuo使用GridLayout布局; - centerLeft.add(symbolButton[0]); - centerLeft.add(symbolButton[1]); - centerLeft.add(symbolButton[2]); - centerLeft.add(symbolButton[3]); - centerLeft.add(numberButton[1]); - centerLeft.add(numberButton[2]); - centerLeft.add(numberButton[3]); - centerLeft.add(zhengfu); - centerLeft.add(numberButton[4]); - centerLeft.add(numberButton[5]); - centerLeft.add(numberButton[6]); - centerLeft.add(sin); - centerLeft.add(numberButton[7]); - centerLeft.add(numberButton[8]); - centerLeft.add(numberButton[9]); - centerLeft.add(Backspace); - centerLeft.add(numberButton[0]); - centerLeft.add(Flex); - centerLeft.add(Equals); - centerLeft.add(Zero); - panelRight.setLayout(new BorderLayout()); //panelYou使用BorderLayout布局。 - panelRight.add(showProcess,BorderLayout.NORTH); // 运算过程放在panelYou的北边. - - saveButton=new JButton("保存"); - copyButton=new JButton("复制"); - clearButton=new JButton("清除"); - saveButton.setForeground(Color.blue); //颜色 - copyButton.setForeground(Color.red); - clearButton.setForeground(Color.gray); - saveButton.setToolTipText("保存计算过程到文件里"); //设置鼠标停留在saveButton上时显示的信息; - copyButton.setToolTipText("复制选中计算过程"); //设置鼠标停留在copyButton上时显示的信息; - clearButton.setToolTipText("清除计算过程"); //设置鼠标停留在clearButton上时显示的信息; - saveButton.addActionListener(this); - clearButton.addActionListener(this); - copyButton.addActionListener(this); - - panelRight.add(new JScrollPane(showStep),BorderLayout.CENTER);//滚动窗格 - JPanel southRight=new JPanel(); //建一个放在panelYou南边的southYou容器存放3个文字按钮。 - southRight.add(saveButton); //放入按钮 - southRight.add(copyButton); - southRight.add(clearButton); - panelRight.add(southRight,BorderLayout.SOUTH);//将southYou放在panelYou南边。 - JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,panelLeft,panelRight);//界面一分为二; - add(split,BorderLayout.CENTER); - validate(); - } - - public void actionPerformed(ActionEvent e) { - if(e.getSource()==copyButton) { //如果按下复制按钮 - showStep.copy(); //实现复制 - } - if(e.getSource()==clearButton) { //如果按下清除按钮 - showStep.setText(null); //实现清除 - } - if(e.getSource()==saveButton){ //如果按下保存按钮 - JFileChooser chooser=new JFileChooser(); //文件选择器 - int state = chooser.showSaveDialog(null); //显示文件保存对话框 - File file = chooser.getSelectedFile(); //输入要保存的文件名; - if(file!=null&&state==JFileChooser.APPROVE_OPTION) { //若选好了文件. - try { - String baoCun=showStep.getText(); //将运算步骤文本赋值给baoCun; - StringReader read = new StringReader(baoCun); //生成字符流。 - BufferedReader in=new BufferedReader(read); //生成缓冲输入流。 - FileWriter out=new FileWriter(file); //根据选好的文件,生成FileWriter对象; - BufferedWriter out1=new BufferedWriter(out); //生成缓冲输出流; - String str=null; - while((str=in.readLine())!=null) { //判断是否有字符 - out1.write(str); //把内容写到文件中 - out1.newLine(); //新的一行 - } - in.close(); //关闭文件; - out1.close(); - }catch(IOException el){ - - } - - } - } - - } - public static void main(String args[]) { - new CalculatorFrame(); - } - - - } - diff --git a/src/java2020spring/MainClass.java b/src/java2020spring/MainClass.java new file mode 100644 index 0000000..a652769 --- /dev/null +++ b/src/java2020spring/MainClass.java @@ -0,0 +1,13 @@ +package java2020spring; + +import java2020spring.Calculator; + +public class MainClass { + public static void main(String[] args) + { + Calculator calculator = new Calculator(); + + calculator.init(); + } + +} -- Gitee From 4d5b751993176f8c6018d2b084ad168936366ec4 Mon Sep 17 00:00:00 2001 From: 86171 <86171@LAPTOP-IFQ8Q1G7> Date: Thu, 10 Jun 2021 23:56:26 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BC=9A=E7=BB=A7=E7=BB=AD=E5=AE=8C?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 169 ++++++++++++++++++++++++----- src/java2020spring/MainClass.java | 1 - 2 files changed, 143 insertions(+), 27 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index 5003dea..54a57fc 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -13,16 +13,17 @@ public class Calculator { JPanel panel; JLabel label; String data = "";//data:当前输入的数据 + double curdata; boolean isLeftAvailable;// isLeftAvailable:判断数据应该向哪一个操作数中存储 double left, right;//left, right:左右操作数 String preOperator = ""; public void init(){//初始化 - setMyFrame();//窗口 - setMyTextField();//输入框 - setMyButton();//按钮 - setMyLabel();//标签 + setFrame();//窗口 + setTextField();//输入框 + setButton();//按钮 + setLabel();//标签 display();//显示窗口 } @@ -32,7 +33,7 @@ public class Calculator { } - private void setMyFrame(){//设置窗口 + private void setFrame(){//设置窗口 frame = new JFrame(); frame.setBounds(80,80,450,650);//设置面板位置和大小 frame.setLayout(new BorderLayout()); @@ -44,7 +45,7 @@ public class Calculator { } - private void setMyTextField()//设置文本框 + private void setTextField()//设置文本框 { textField = new JTextField("0"); textField.setBounds(50,25,400,200);// 设置文本框的位置和大小 @@ -56,10 +57,10 @@ public class Calculator { * setMyButton: * @description: 设置按键事件 */ - private void setMyButton() // 设置按钮 + private void setButton() // 设置按钮 { // 按钮文本 - String[] arr ={ "C","DEL","%","/", + String[] arr ={ "C","DEL","sqrt","/", "7","8","9","*", "4","5","6","+", "1","2","3","-", @@ -72,7 +73,7 @@ public class Calculator { for(int i = 0; i < button.length; i++){ button[i] = new JButton(arr[i]);// 创建按钮 button[i].setFont(new Font("黑体", Font.BOLD, 20));// 设置按钮字体 - button[i].setBackground(Color.white); // 设置按钮背景颜色 + button[i].setBackground(Color.WHITE); // 设置按钮背景颜色 if(button.equals("=")){// 设置 "="号的颜色 button[i].setBackground(Color.BLUE); @@ -80,9 +81,7 @@ public class Calculator { int event = i;// 添加事件 - - /*设置鼠标监听*/ - + // 设置鼠标监听 button[i].addMouseListener(new MouseAdapter() {// 点击按钮 @Override public void mouseClicked(MouseEvent e) {//获取按钮上的内容 @@ -98,7 +97,7 @@ public class Calculator { @Override public void mouseExited(MouseEvent e) {// 当鼠标离开组件事件时 - button[event].setFont(new Font("黑体", Font.CENTER_BASELINE, 20)); + button[event].setFont(new Font("黑体", Font.BOLD, 20)); button[event].setBackground(Color.WHITE); button[event].setForeground(Color.BLACK); } @@ -110,13 +109,12 @@ public class Calculator { } - private void setMyLabel(){// 设置标签 - - + private void setLabel(){// 设置标签 + label = new JLabel(); + label.setText("该计算器为简单计算器,制作人:202025710419,版权归制作人所有。"); + frame.add(label,BorderLayout.SOUTH); } - - public void click(String option){//设置点击事件 String operator = ""; @@ -124,15 +122,97 @@ public class Calculator { data += "1"; textField.setText(data); } - else if("2".equals(option)) { - + else if("2".equals(option)){ + data += "2"; + textField.setText(data); + } + else if("3".equals(option)){ + data += "3"; + textField.setText(data); + } + else if("4".equals(option)){ + data += "4"; + textField.setText(data); + } + else if("5".equals(option)){ + data += "5"; + textField.setText(data); + } + else if("6".equals(option)){ + data += "6"; + textField.setText(data); + } + else if("7".equals(option)){ + data += "7"; + textField.setText(data); + } + else if("8".equals(option)){ + data += "8"; + textField.setText(data); + } + else if("9".equals(option)){ + data += "9"; + textField.setText(data); + } + else if("0".equals(option)){ + data += "0"; + textField.setText(data); + } + else if(".".equals(option)){ + data += "."; + textField.setText(data); + } + else if("+/-".equals(option)){ + if(data.indexOf('-') < 0){ //正数 + data = "-" + data; + } + else{ // 负数 + data = data.substring(1); + } + textField.setText(data); + } + else if("sqrt".equals(option)){//开方功能 + double data1=Double.parseDouble(data); + curdata = Math.sqrt(data1); + String data2=String.valueOf(curdata); + textField.setText(data2); + } + else if("+".equals(option)){ + operator = "+"; + calculator(operator); + } + else if("-".equals(option)){ + operator = "-"; + calculator(operator); + } + else if("*".equals(option)){ + operator = "*"; + calculator(operator); + } + else if("/".equals(option)){ + operator = "/"; + calculator(operator); + } + else if("=".equals(option)){ + operator = "="; + calculator(operator); + } + + else if("C".equals(option)){ + data = ""; + isLeftAvailable = false;//删除文本框中全部内容 + textField.setText(data); + } + + else if("DEL".equals(option)){ + if(data.length() != 0){ + data = data.substring(0, data.length() - 1);//删除文本框中最后输入的内容 + } + textField.setText(data); } } - - - private void calculator(String operator){ if("".equals(data)){// 如果 data 为空,不执行任何操作 if("=".equals(operator)){// 用户只要输入 = ,无论前面的输入是否合法,结束此轮计算 @@ -152,13 +232,50 @@ public class Calculator { if(isLeftAvailable){ right = Double.parseDouble(data); - data = "null"; + data = ""; + /* 使用"="的上一个运算符:*/ - } + if("+".equals(preOperator)){ + left += right; + } + else if("-".equals(preOperator)){ + left -= right; + } + else if("*".equals(preOperator)){ + left *= right; + } + else if("/".equals(preOperator)){ + left /= right; + } + + if(left == (int)left){// 如果运算结果为整数,就用整数输出 + left = (int)left; + } + + } + + else{ + left = Double.parseDouble(data); //清空 data + data = ""; + isLeftAvailable = true; + } - + String result = left + "";// 将运算结果转换为字符串 + // "=" 操作符清理右操作数,重新开始计算 + if("=".equals(operator)) + { + isLeftAvailable = false; + } + preOperator = operator;// 更新运算符 + textField.setText(text(result) + " " +operator); } + + private String text(String result) { + return result; + } + + } diff --git a/src/java2020spring/MainClass.java b/src/java2020spring/MainClass.java index a652769..893191e 100644 --- a/src/java2020spring/MainClass.java +++ b/src/java2020spring/MainClass.java @@ -1,6 +1,5 @@ package java2020spring; -import java2020spring.Calculator; public class MainClass { public static void main(String[] args) -- Gitee From 36045a577f445bf1b46eb3a6798330b0e33c3b56 Mon Sep 17 00:00:00 2001 From: 86171 <86171@LAPTOP-IFQ8Q1G7> Date: Fri, 11 Jun 2021 10:44:53 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=B7=B2=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2020spring/Calculator.java | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/java2020spring/Calculator.java b/src/java2020spring/Calculator.java index 54a57fc..2224005 100644 --- a/src/java2020spring/Calculator.java +++ b/src/java2020spring/Calculator.java @@ -33,7 +33,7 @@ public class Calculator { } - private void setFrame(){//设置窗口 + private void setFrame(){//设置窗口面板 frame = new JFrame(); frame.setBounds(80,80,450,650);//设置面板位置和大小 frame.setLayout(new BorderLayout()); @@ -73,12 +73,13 @@ public class Calculator { for(int i = 0; i < button.length; i++){ button[i] = new JButton(arr[i]);// 创建按钮 button[i].setFont(new Font("黑体", Font.BOLD, 20));// 设置按钮字体 - button[i].setBackground(Color.WHITE); // 设置按钮背景颜色 - - if(button.equals("=")){// 设置 "="号的颜色 - button[i].setBackground(Color.BLUE); + button[i].setBackground(Color.WHITE); + if(button.length - 1 == i){ //设置“=”的特殊颜色 + button[i].setBackground(Color.GRAY); } + + int event = i;// 添加事件 // 设置鼠标监听 @@ -172,10 +173,15 @@ public class Calculator { textField.setText(data); } else if("sqrt".equals(option)){//开方功能 + double data1=Double.parseDouble(data); + if(data1<0)//如果被开方数为负数 + textField.setText("error"); + else {//被开方数为正数 curdata = Math.sqrt(data1); String data2=String.valueOf(curdata); textField.setText(data2); + } } else if("+".equals(option)){ operator = "+"; @@ -234,7 +240,7 @@ public class Calculator { right = Double.parseDouble(data); data = ""; - /* 使用"="的上一个运算符:*/ + /* 使用"="的之前一个运算符:*/ if("+".equals(preOperator)){ left += right; @@ -264,8 +270,7 @@ public class Calculator { String result = left + "";// 将运算结果转换为字符串 // "=" 操作符清理右操作数,重新开始计算 - if("=".equals(operator)) - { + if("=".equals(operator)){ isLeftAvailable = false; } preOperator = operator;// 更新运算符 -- Gitee