diff --git a/src/java2020spring/CalculatorFrame.java b/src/java2020spring/CalculatorFrame.java new file mode 100644 index 0000000000000000000000000000000000000000..228d99e1501edf34aecf27efd409d7e8ea480998 --- /dev/null +++ b/src/java2020spring/CalculatorFrame.java @@ -0,0 +1,252 @@ +package java2020spring; +import javax.swing.*; +import java.awt.event.*; +import java.math.BigDecimal; + +public class CalculatorFrame extends JFrame implements ActionListener { + int i=0,ii=0; + double num1=0,num2=0; + double iii=10.0,memory; + JTextField tf; + public void actionPerformed(ActionEvent e) { + String str=e.getActionCommand(); + if(e.getActionCommand().equals("0")) { + sca(0); + } + else if(e.getActionCommand().equals("1")) { + sca(1); + } + else if(e.getActionCommand().equals("2")) { + sca(2); + } + else if(e.getActionCommand().equals("3")) { + sca(3); + } + else if(e.getActionCommand().equals("4")) { + sca(4); + } + else if(e.getActionCommand().equals("5")) { + sca(5); + } + else if(e.getActionCommand().equals("6")) { + sca(6); + } + else if(e.getActionCommand().equals("7")) { + sca(7); + } + else if(e.getActionCommand().equals("8")) { + sca(8); + } + else if(e.getActionCommand().equals("9")) { + sca(9); + } + else if(e.getActionCommand().equals("MR")) { + solveMr(); + } + else if(e.getActionCommand().equals("MC")) { + solveMc(); + } + else if(e.getActionCommand().equals("M+")) { + solveM1(); + } + else if(e.getActionCommand().equals("M-")) { + solveM2(); + } + else if(e.getActionCommand().equals("+")) { + this.i=1; + tf.setText(""); + calcu(this.num1,this.num2,this.i); + this.num2=0; + ii=0; + this.iii=10.0; + } + else if(e.getActionCommand().equals("-")) { + this.i=2; + tf.setText(""); + calcu(this.num1,this.num2,this.i); + this.num2=0; + ii=0; + this.iii=10.0; + } + else if(e.getActionCommand().equals("X")) { + this.i=3; + tf.setText(""); + ii=0; + this.iii=10.0; + } + else if(e.getActionCommand().equals("÷")) { + this.i=4; + tf.setText(""); + ii=0; + this.iii=10.0; + } + else if(e.getActionCommand().equals("%")) { + this.i=5; + tf.setText(""); + calcu(this.num1,this.num2,this.i); + ii=0; + this.iii=10.0; + } + else if(e.getActionCommand().equals("=")) { + calcu(this.num1,this.num2,this.i); + this.num2=0; + this.i=0; + this.iii=10.0; + ii=0; + } + else if(e.getActionCommand().equals("root")) { + this.num1=Math.sqrt(this.num1); + tf.setText(String.valueOf(num1)); + } + else if(e.getActionCommand().equals("cube")) { + this.num1=Math.pow(this.num1,1.0/3); + tf.setText(String.valueOf(num1)); + } + else if(e.getActionCommand().equals(".")) { + ii=1; + } + else if(e.getActionCommand().equals("sin")) { + this.num1=Math.sin(this.num1); + tf.setText(String.valueOf(num1)); + } + else if(e.getActionCommand().equals("cos")) { + this.num1=Math.cos(this.num1); + tf.setText(String.valueOf(num1)); + } + else if(e.getActionCommand().equals("tan")) { + this.num1=Math.tan(this.num1); + tf.setText(String.valueOf(num1)); + } + else if(e.getActionCommand().equals("C")) { + tf.setText(""); + this.num1=0; + this.num2=0; + this.i=0; + ii=0; + iii=10.0; + } + else if(e.getActionCommand().equals("←")) { + int len=tf.getText().length(); + if(len==0) { + tf.setText(""); + } + else + tf.setText(tf.getText().substring(0,len-1)); + } + + + } + @SuppressWarnings("deprecation") + public void sca(int m) { + int len1=tf.getText().length(); + String str1; + if(this.i==0) { + if(ii==0) { + if(len1<9) { + this.num1=this.num1*10+m; + tf.setText(String.valueOf((int)this.num1)); + } + else { + str1="字数超出限制"; + tf.setText(str1); + } + } + else { + double n; + n=(double)m/iii; + iii=iii*10; + this.num1+=n; + BigDecimal Bd=new BigDecimal(this.num1); + double sc=Bd.setScale(9,BigDecimal.ROUND_HALF_DOWN).doubleValue(); + tf.setText(String.valueOf(sc)); + } + + } + else { + if(ii==0) { + if(len1<9) { + this.num2=this.num2*10+m; + tf.setText(String.valueOf((int)this.num2)); + } + else { + str1="字数超出限制"; + tf.setText(str1); + } + } + else { + + double n; + n=(double)m/iii; + iii=iii*10; + this.num2+=n; + BigDecimal Bd=new BigDecimal(this.num2); + double sc=Bd.setScale(9,BigDecimal.ROUND_HALF_DOWN).doubleValue(); + tf.setText(String.valueOf(sc)); + } + + } + } + + public void solveMr() { + this.num1=this.memory; + tf.setText(String.valueOf(num1)); + } + public void solveMc() { + this.iii=10.0; + this.memory=0; + tf.setText(String.valueOf(memory)); + } + public void solveM1() { + this.memory-=this.num1; + if(this.memory!=0) { + tf.setText("M"); + } + else + tf.setText(""); + } + public void solveM2() { + this.memory+=this.num1; + if(this.memory!=0) { + tf.setText("M"); + } + else + tf.setText(""); + } + public void calcu(double a,double b,int i) { + double result=0; + int str2=0; + String str1; + switch(i){ + case 0: + case 1:result=a+b;break; + case 2:result=a-b;break; + case 3:result=a*b;break; + case 4:if(b!=0)result=a/b; + else{ + str2=1; + } + break; + case 5:result=a/100; + } + if(str2==0) { + double n1=result; + int n2=(int)n1; + double n3=(double)n2; + if(n3==result) + tf.setText(String.valueOf(n2)); + else + tf.setText(String.valueOf(result)); + this.num1=result; + } + else { + str1="注意!除数不可为零!"; + tf.setText(str1); + } + } + public void setJTextField(JTextField t) { + tf=t; + + } + } + + \ No newline at end of file diff --git a/src/java2020spring/MainFrame.java b/src/java2020spring/MainFrame.java new file mode 100644 index 0000000000000000000000000000000000000000..d11239058c0b9e17afb7dbc84200f1f3797256be --- /dev/null +++ b/src/java2020spring/MainFrame.java @@ -0,0 +1,8 @@ +package java2020spring; + +public class MainFrame { + public static void main(String args[]) { + new WindowFrame(); + + } +} diff --git a/src/java2020spring/MenuFrame.java b/src/java2020spring/MenuFrame.java new file mode 100644 index 0000000000000000000000000000000000000000..edead74ead8af0565b5fdf25fb93f1374b0e1415 --- /dev/null +++ b/src/java2020spring/MenuFrame.java @@ -0,0 +1,41 @@ +package java2020spring; +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; + +public class MenuFrame extends JFrame implements ActionListener { + String str; + JTextPane area=new JTextPane(); + MenuFrame(JFrame g){ + this.setBounds(200,200,400,400); + this.add(area); + this.setTitle(""); + area.setFont(new Font("华文楷体",Font.BOLD,21)); + + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + + } + @Override + public void actionPerformed(ActionEvent e) { + str=e.getActionCommand(); + if(str.equals("请仔细阅读")) { + String s="1.因作者能力有限,无法实现连乘或连除功能,只能中间使用等于号实现得数与新的数字相乘\n2.除了加减法外,其余运算每运算一次请点击=,以得出准确结果\n3.每次运算后,若要重新运算新的数字,请记得点C清零\n4.感谢您的使用!"; + area.setText(s); + area.setEditable(false); + } + else if(str.equals("作者基本信息")) { + String s="姓名:孔清妹\n班级:20级信管2班\n"; + area.setText(s); + area.setEditable(false); + + } + else if(str.equals("帮助内容")) { + String s="作者时间有限,此功能正在开发中\n如遇问题请参考“须知”\n实在不行,可进行拨打10086投诉作者\n不过作者还是希望不要投诉,作者正在努力!"; + area.setText(s); + area.setEditable(false); + + } + this.setVisible(true); + } + +} diff --git a/src/java2020spring/WindowFrame.java b/src/java2020spring/WindowFrame.java new file mode 100644 index 0000000000000000000000000000000000000000..0bba8878731c17bac0e5220a6588b2c804459d1b --- /dev/null +++ b/src/java2020spring/WindowFrame.java @@ -0,0 +1,245 @@ +package java2020spring; +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +public class WindowFrame extends JFrame{ + JTextField tf=new JTextField(); + String s=(String)(tf.getText()); + JPanel panel1=new JPanel(); + JPanel panel2=new JPanel(new GridLayout(7,4)); + JMenu menu1,menu2,menu3; + JMenuItem item1,item2,item3; + JMenuBar menubar; + + JButton buttonJia=new JButton("+"); + JButton buttonJian=new JButton("-"); + JButton buttonCheng=new JButton("X"); + JButton buttonChu=new JButton("÷"); + JButton buttonZero=new JButton("0"); + JButton button1=new JButton("1"); + JButton button2=new JButton("2"); + JButton button3=new JButton("3"); + JButton button4=new JButton("4"); + JButton button5=new JButton("5"); + JButton button6=new JButton("6"); + JButton button7=new JButton("7"); + JButton button8=new JButton("8"); + JButton button9=new JButton("9"); + JButton buttonYu=new JButton("%"); + JButton buttonCube=new JButton("cube"); + JButton buttonDian=new JButton("."); + JButton buttonQingchu=new JButton("C"); + JButton buttonDengyu=new JButton("="); + JButton buttonRoot=new JButton("root"); + JButton buttonMc=new JButton("MC"); + JButton buttonMr=new JButton("MR"); + JButton buttonM1=new JButton("M-"); + JButton buttonM2=new JButton("M+"); + JButton buttonSin=new JButton("sin"); + JButton buttonCos=new JButton("cos"); + JButton buttonTan=new JButton("tan"); + JButton buttonBack=new JButton("←"); + WindowFrame(){ + addButton(); + setCommand(); + + } + public void addButton() { + + tf.setPreferredSize(new Dimension(350,100)); + tf.setFont(new Font("华文行楷",Font.PLAIN,30)); + Font font1=new Font("华文楷体",Font.BOLD,33); + Font font2=new Font("华文隶书",Font.BOLD,33); + buttonZero.setFont(font1); + button1.setFont(font1); + button2.setFont(font1); + button3.setFont(font1); + button4.setFont(font1); + button5.setFont(font1); + button6.setFont(font1); + button7.setFont(font1); + button8.setFont(font1); + button9.setFont(font1); + buttonJia.setFont(font1); + buttonJian.setFont(font1); + buttonCheng.setFont(font1); + buttonChu.setFont(font1); + buttonYu.setFont(font1); + buttonDian.setFont(font1); + buttonDengyu.setFont(font1); + buttonQingchu.setFont(font2); + buttonMc.setFont(font2); + buttonMr.setFont(font2); + buttonM1.setFont(font2); + buttonM2.setFont(font2); + buttonSin.setFont(font2); + buttonCos.setFont(font2); + buttonTan.setFont(font2); + buttonBack.setFont(font2); + buttonCube.setFont(font2); + buttonRoot.setFont(font2); + + panel1.add(tf); + this.add(panel1,BorderLayout.NORTH); + tf.setBackground(Color.white); + + buttonMc.setForeground(Color.PINK); + buttonMr.setForeground(Color.PINK); + buttonM1.setForeground(Color.PINK); + buttonM2.setForeground(Color.PINK); + buttonSin.setForeground(Color.PINK); + buttonCos.setForeground(Color.PINK); + buttonTan.setForeground(Color.PINK); + buttonQingchu.setForeground(Color.PINK); + buttonCube.setForeground(Color.PINK); + buttonRoot.setForeground(Color.PINK); + buttonBack.setForeground(Color.PINK); + buttonJia.setForeground(Color.RED); + buttonJian.setForeground(Color.RED); + buttonCheng.setForeground(Color.RED); + buttonChu.setForeground(Color.RED); + buttonYu.setForeground(Color.RED); + buttonDengyu.setBackground(Color.GREEN); + buttonDian.setForeground(Color.RED); + + + + panel2.add(buttonMc); + panel2.add(buttonM1); + panel2.add(buttonM2); + panel2.add(buttonMr); + + panel2.add(buttonQingchu); + panel2.add(buttonCheng); + panel2.add(buttonChu); + panel2.add(buttonBack); + + panel2.add(buttonSin); + panel2.add(buttonCos); + panel2.add(buttonTan); + panel2.add(buttonDengyu); + + panel2.add(button7); + panel2.add(button8); + panel2.add(button9); + panel2.add(buttonJian); + + panel2.add(button4); + panel2.add(button5); + panel2.add(button6); + panel2.add(buttonJia); + + panel2.add(button1); + panel2.add(button2); + panel2.add(button3); + panel2.add(buttonCube); + + panel2.add(buttonYu); + panel2.add(buttonZero); + panel2.add(buttonDian); + panel2.add(buttonRoot); + + inti(); + this.setTitle("计算器"); + this.add(panel2,BorderLayout.SOUTH); + this.setSize(400,558); + this.setLocationRelativeTo(null); + this.setVisible(true); + this.setResizable(false); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + void inti() { + menubar=new JMenuBar(); + menu1=new JMenu("须知"); + menu2=new JMenu("查看"); + menu3=new JMenu("帮助"); + item1=new JMenuItem("请仔细阅读"); + item2=new JMenuItem("作者基本信息"); + item3=new JMenuItem("帮助内容"); + menu1.add(item1); + menu2.add(item2); + menu3.add(item3); + menubar.add(menu1); + menubar.add(menu2); + menubar.add(menu3); + setJMenuBar(menubar); + + + } + public void setCommand() { + tf.setEditable(false); + buttonZero.setActionCommand("0"); + button1.setActionCommand("1"); + button2.setActionCommand("2"); + button3.setActionCommand("3"); + button4.setActionCommand("4"); + button5.setActionCommand("5"); + button6.setActionCommand("6"); + button7.setActionCommand("7"); + button8.setActionCommand("8"); + button9.setActionCommand("9"); + buttonJia.setActionCommand("+"); + buttonJian.setActionCommand("-"); + buttonCheng.setActionCommand("X"); + buttonChu.setActionCommand("÷"); + buttonYu.setActionCommand("%"); + buttonCube.setActionCommand("cube"); + buttonDian.setActionCommand("."); + buttonQingchu.setActionCommand("C"); + buttonDengyu.setActionCommand("="); + buttonRoot.setActionCommand("root"); + buttonMc.setActionCommand("MC"); + buttonMr.setActionCommand("MR"); + buttonM1.setActionCommand("M+"); + buttonM2.setActionCommand("M-"); + buttonSin.setActionCommand("sin"); + buttonCos.setActionCommand("cos"); + buttonTan.setActionCommand("tan"); + buttonBack.setActionCommand("←"); + + + CalculatorFrame listener; + listener=new CalculatorFrame(); + listener.setJTextField(tf); + tf.setHorizontalAlignment(JTextField.RIGHT); + tf.addActionListener(listener); + buttonZero.addActionListener(listener); + button1.addActionListener(listener); + button2.addActionListener(listener); + button3.addActionListener(listener); + button4.addActionListener(listener); + button5.addActionListener(listener); + button6.addActionListener(listener); + button7.addActionListener(listener); + button8.addActionListener(listener); + button9.addActionListener(listener); + buttonJia.addActionListener(listener); + buttonJian.addActionListener(listener); + buttonCheng.addActionListener(listener); + buttonChu.addActionListener(listener); + buttonYu.addActionListener(listener); + buttonDian.addActionListener(listener); + buttonCube.addActionListener(listener); + buttonQingchu.addActionListener(listener); + buttonDengyu.addActionListener(listener); + buttonRoot.addActionListener(listener); + buttonMc.addActionListener(listener); + buttonMr.addActionListener(listener); + buttonM1.addActionListener(listener); + buttonM2.addActionListener(listener); + buttonSin.addActionListener(listener); + buttonCos.addActionListener(listener); + buttonTan.addActionListener(listener); + buttonBack.addActionListener(listener); + + MenuFrame listener2=new MenuFrame(this); + item1.addActionListener(listener2); + item2.addActionListener(listener2); + item3.addActionListener(listener2); + + } + + + } + +