diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java new file mode 100644 index 0000000000000000000000000000000000000000..66abc3b2f529ff92bbe75f52010f9cb08afedcef --- /dev/null +++ b/src/java2022spring/Calculator.java @@ -0,0 +1,324 @@ +package java2022spring; + +import java.awt.*; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.*; +import javax.swing.border.LineBorder; + + + + +public class Calculator extends JFrame implements ActionListener{ + JPanel panel; + JTextField textField = new JTextField(); + JMenuBar menuBar; + JMenu menu1, menu2, menu3; + JMenuItem item1, item2, item3, item4, item5, item6; + + Double Mem = 0.0; + private JButton[][] button = new JButton[6][5]; + + private String getbutton; + private double result; + private boolean start; + + public Calculator() { + init(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + validate(); + } + + public void init() { + setTitle("覃国权的计算器"); + setBounds(300,50,640,700); + setLayout(new BorderLayout()); + LineBorder border = new LineBorder(Color.LIGHT_GRAY,6,true); + setResizable(false); + textField.setPreferredSize(new Dimension(230,150)); + textField.setBackground(new Color(230,240,250)); + textField.setForeground(new Color(30,60,90)); + textField.setFont(new Font("黑体",Font.BOLD,40)); + textField.setEditable(false); + textField.setText("0"); + textField.setHorizontalAlignment(JTextField.RIGHT); + start = true; + result = 0; + getbutton = "="; + this.setResizable(false); + this.add( textField ); + + String[][] str = { + {"MC", "MR", "MS", "M+", "M-"}, + {"←", "CE", "C", "±", "√"}, + {"7", "8", "9", "/", "%"}, + {"4", "5", "6", "*", "1/x"}, + {"1", "2", "3", "-", "x^2"}, + {"00", "0", ".", "+", "="} + }; + + panel = new JPanel(); + panel.setLayout(new GridLayout(6,5)); + for(int i=0;i<6;i++) { + for(int j=0;j<5;j++) { + button[i][j] = new JButton(str[i][j]); + button[i][j].setFont(new Font("黑体",Font.BOLD,30)); + button[i][j].addActionListener( this ); + panel.add(button[i][j]); + + } + } + + button[2][0].setBackground(new Color(230,240,250)); + button[2][1].setBackground(new Color(230,240,250)); + button[2][2].setBackground(new Color(230,240,250)); + button[3][0].setBackground(new Color(230,240,250)); + button[3][1].setBackground(new Color(230,240,250)); + button[3][2].setBackground(new Color(230,240,250)); + button[4][0].setBackground(new Color(230,240,250)); + button[4][1].setBackground(new Color(230,240,250)); + button[4][2].setBackground(new Color(230,240,250)); + button[5][0].setBackground(new Color(230,240,250)); + button[5][1].setBackground(new Color(230,240,250)); + button[5][2].setBackground(new Color(230,240,250)); + button[5][4].setForeground(Color.RED); + button[5][4].setBackground(new Color(230,240,250)); + button[5][4].setFont(new Font("黑体",Font.BOLD,50)); + button[1][0].setFont(new Font("黑体",Font.BOLD,40)); + + add(textField, BorderLayout.NORTH); + add(panel,BorderLayout.CENTER); + + menu1 = new JMenu("更多"); + menu1.setFont(new Font("宋体",Font.BOLD,20)); + menu2 = new JMenu("关于"); + menu2.setFont(new Font("宋体",Font.BOLD,20)); + menu3 = new JMenu("说明"); + menu3.setFont(new Font("宋体",Font.BOLD,20)); + menuBar = new JMenuBar(); + menuBar.add(menu1); + menuBar.add(menu2); + menuBar.add(menu3); + setJMenuBar(menuBar); + item1 = new JMenuItem("质量转换"); + item2 = new JMenuItem("长度转换"); + item3 = new JMenuItem("开发者"); + item4 = new JMenuItem("开发时间"); + item5 = new JMenuItem("敬请期待"); + item6 = new JMenuItem("使用说明"); + menu1.add(item1); + menu1.add(item2); + menu2.add(item3); + menu2.add(item4); + menu1.add(item5); + menu3.add(item6); + itemEvent(); + } + + public void itemEvent() { + item3.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null,"21信管1班\n202125710120\n覃国权","开发者", JOptionPane.PLAIN_MESSAGE); + } + }); + + item4.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null,"2022/06/08","开发时间", JOptionPane.PLAIN_MESSAGE); + } + }); + + item1.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null,"即将上线,敬请期待","质量转换", JOptionPane.PLAIN_MESSAGE); + } + }); + + item2.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null,"即将上线,敬请期待","长度转换", JOptionPane.PLAIN_MESSAGE); + } + }); + + item6.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null,"(1)“C”:将之前输入的数字,计算结果等功能全部归零;\r\n" + + "\r\n" + + "(2)“CE”:清除键,清空当前输入的数据;\r\n" + + "\r\n" + + "(3)“MS”:存储当前的显示值;\r\n" + + "\r\n" + + "(4)“MC”:清空当前存储区中的值;\r\n" + + "\r\n" + + "(5)“MR”:将存储区的数调出到显示栏,同时存储区中的值不变;\r\n" + + "\r\n" + + "(6)“M+”:将存储区的数与当前显示的数相加,结果存到存储区,并不会直接显示;\r\n" + + "\r\n" + + "(7)“M-”:将当前显示的数与存储区的数相减,结果存到存储区;\r\n" + + "\r\n" + + "(8)“CE”:类似隐藏当前显示的所有值;\r\n" + + "\r\n" + + "(9)“C”:清空当前显示的所有值;以小数点形式归零;\r\n" + + "\r\n" + + "(10)“±”:正负号转换器;\r\n" + + "\r\n" + + "(11)“√”:先点击该按钮后写入求根号的数再点击“=”按钮后即可求得根号后的值;\r\n" + + "\r\n" + + "(12)“%”:先点击该按钮后写入求百分数的数再点击“=”按钮后即可求得百分数后的值;\r\n" + + "\r\n" + + "(13)“1/x”:先点击该按钮后写入求倒数的数再点击“=”按钮后即可求得倒数后的值;\r\n" + + "\r\n" + + "(14)“x^2”:先点击该按钮后写入求平方的数再点击“=”按钮后即可求得平方后的值。\r\n" + + "\r\n" + + "(14)“←”:将显示的数或符号从右回退一位\r\n" + ,"使用说明", JOptionPane.PLAIN_MESSAGE); + } + }); + + item5.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null,"功能多有不全,使用不便请多海涵,感谢您的支持","敬请期待", JOptionPane.PLAIN_MESSAGE); + } + }); + + } + + public void actionPerformed(ActionEvent e){ + if( + e.getSource().equals( button[0][0] )||e.getSource().equals( button[0][1] )|| + e.getSource().equals( button[0][2] )||e.getSource().equals( button[0][3] )|| + e.getSource().equals( button[0][4] )||e.getSource().equals( button[1][0] )|| + e.getSource().equals( button[1][1] )||e.getSource().equals( button[1][2] )|| + e.getSource().equals( button[1][3] )||e.getSource().equals( button[2][0] )|| + e.getSource().equals( button[2][1] )||e.getSource().equals( button[2][2] )|| + e.getSource().equals( button[3][0] )||e.getSource().equals( button[3][1] )|| + e.getSource().equals( button[3][2] )||e.getSource().equals( button[4][0] )|| + e.getSource().equals( button[4][1] )||e.getSource().equals( button[4][2] )|| + e.getSource().equals( button[5][0] )||e.getSource().equals( button[5][1] )|| + e.getSource().equals( button[5][2] ) + ) { + String input = e.getActionCommand(); + if (start){ + textField.setText(""); + start = false; + if( input.equals( "±" ) ) + textField.setText( textField.getText()+ "-" ); + + } + if( !input.equals( "±" ) ){ + String str = textField.getText(); + if( input.equals( "←") ){ + if( str.length() > 0 ) + textField.setText( str.substring(0,str.length() - 1 ) ); + } + else if( input.equals( "CE" ) ){ + textField.setText( "0"); + start = true; + } + else if(input.equals("C")){ + textField.setText("0"); + result = 0.0d; + start = true; + } + else if( input.equals( "MC" ) ){ + Mem = 0.0; + start = true; + } + else if( input.equals( "MS" ) ){ + Mem = result; + textField.setText(" "+ result ); + start = true; + } + else if( input.equals( "MR" ) ){ + textField.setText( " "+Mem); + start = true; + } + else if( input.equals( "M+" ) ){ + double x1, x2; + x1 = Mem; + x2 = result; + Mem = x1 + x2; + start = true; + } + else if( input.equals( "M-" ) ){ + double x1, x2; + x1 = Mem; + x2 = result; + Mem = x2 - x1; + } + else + textField.setText( textField.getText() + input ); + } + } + else + { + String command = e.getActionCommand(); + if( start ) + { + getbutton = command; + } + else + { + calculate( Double.parseDouble( textField.getText() ) ); + getbutton = command; + start = true; + } + } + } + + public void calculate( double x ) { + double a = 0; + if ( getbutton.equals( "+" ) ) { + result=result+x; + textField.setText(" "+ result );} + else if (getbutton.equals( "-" ) ) { + result=result-x; + textField.setText(" "+ result );} + else if ( getbutton.equals( "*" ) ) { + result=result*x; + textField.setText(" "+ result );} + else if ( getbutton.equals( "/" ) ){ + if (Double.parseDouble(textField.getText()) == 0.0) { + textField.setText("ERROR"); } + else { + result=result/Double.parseDouble(textField.getText()); + textField.setText(" "+ result );} + } + else if ( getbutton.equals("1/x")) { + if (Double.parseDouble(textField.getText()) == 0.0) { + textField.setText("ERROR"); + } + else { + result = 1 / Double.parseDouble(textField.getText()); + textField.setText(" "+ result );} + } + else if ( getbutton.equals("x^2")) { + result = Double.parseDouble(textField.getText()); + result = result * result; + textField.setText(" "+ result ); + } + else if(getbutton.equals("%") ) { + result = Double.parseDouble(textField.getText()); + result = x/100; + textField.setText(" "+ result ); + } + else if ( getbutton.equals( "=" ) ) { + result=x; + textField.setText(" "+ result );} + else if ( getbutton.equals( "√" ) ){ + if (Double.parseDouble(textField.getText())>=0) { + a = Math.sqrt(x); + result = a; + textField.setText(" "+ result );} + else textField.setText("请输入一个非负数"); + } + +} + + public static void main(String[] args) { + Calculator calculator = new Calculator(); + calculator.setVisible(true); + } +}