diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java new file mode 100644 index 0000000000000000000000000000000000000000..1472121d9e042e06d89d5b2e1dbecebe20adab8b --- /dev/null +++ b/src/java2022spring/Calculator.java @@ -0,0 +1,230 @@ +package java2022spring; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.*; +import javax.swing.*; + +public class Calculator extends JFrame { + String str1="";//用于运算方法中 + String str2=""; + String operation="~"; + JTextField text=new JTextField();//设置全局变量文本框显示界面 + int a=0; + String M="0";//用于MC等功能键中 + + public void Calculator() { + JFrame jf=new JFrame(); + jf.setTitle ("JAVA计算器");//设置标题 + jf.setLayout(new BorderLayout());//设置布局界面 + jf.setSize(460,380);//设置窗口大小 + jf.setLocationRelativeTo(null);//设置程序居中显示 + jf.setResizable(false);//设置界面大小不可更改 + jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时,退出程序 + try{ + Image cal=(new ImageIcon("src\\java2022spring\\image\\calculator.jpeg")).getImage(); + jf.setIconImage(cal); + } + catch(NullPointerException e){ + }//设置窗口图标 + + + + //创建北面JTextField界面 + JPanel jpnorth=new JPanel(); + text.setPreferredSize(new Dimension(450,50)); + jpnorth.add(text,BorderLayout.NORTH); + jf.add(jpnorth,BorderLayout.NORTH);//放置北面界面入项目框 + + + + //创建西面界面jpwest + JPanel jpwest=new JPanel(); + jpwest.setPreferredSize(new Dimension(100,30)); + jf.add(jpwest,BorderLayout.WEST); + //在西面界面创建一个enabled为false的按钮 + JPanel jp4=new JPanel(new GridLayout(1,1)); + jpwest.add(jp4,BorderLayout.NORTH); + jp4.setPreferredSize(new Dimension(65,30) ); + JButton hide=new JButton(); + hide.setEnabled(false);//设置顶部不可见按钮 + jp4.add(hide); + + + + //在西面界面上创建四个功能键 + String buttontextString3[]= {"MC","MR","MS","M+"}; + JPanel jp3=new JPanel(new GridLayout(4,1) ); + jpwest.add(jp3,BorderLayout.SOUTH); + for(int i=0;i<4;i++) {//利用for循环数组实现命名 + JButton jb3=new JButton(buttontextString3[i]); + jb3.setPreferredSize(new Dimension(65,60)); + jb3.setBackground(Color.WHITE); + jp3.add(jb3); + jb3.addActionListener(new ActionListener() {//设置四个功能按键作用 + public void actionPerformed(ActionEvent e) { + String str=e.getActionCommand(); + String action=str; + if(action=="MC") { //MC按钮功能 + M="0"; + text.setText(M); + } + if(action=="MR") //MR按钮功能 + text.setText(M); + if(action=="MS") {//MS按钮功能 + M=text.getText(); + M=M.substring(M.indexOf('\n')+1); + } + if(action=="M+") {//M+按钮功能 + double x1,x2; + x1=Double.parseDouble(M); + x2=Double.parseDouble(text.getText()); + M=""+(x2-x1); + } + text.setHorizontalAlignment(JTextField.RIGHT); + } + }); + } + + + + //创建计算器东面界面jpeast + JPanel jpeast=new JPanel(); + jpeast.setPreferredSize(new Dimension(355,300)); + jf.add(jpeast,BorderLayout.EAST); + + //在东面界面上创建计算器运算按钮 + String buttontextString2[]= {"7","8","9","/","√","4","5","6","*","%","1","2","3","-","1/x","0","±",".","+","="}; + JPanel jp2=new JPanel(new GridLayout(4,5) ); + for(int i=0;i<20;i++) {//利用for循环数组实现命名 + JButton jb2=new JButton(buttontextString2[i]); + jb2.setPreferredSize(new Dimension(70,60));//设置按钮大小 + jb2.setBackground(Color.WHITE);//设置按钮颜色 + jp2.add(jb2); + jb2.addActionListener(new ActionListener() { + + //设计计算机运算逻辑 + @Override + public void actionPerformed(ActionEvent e) { + String str=e.getActionCommand();//获取监视器得到的数字 + String action=str; + + if(action=="0"||action=="1"||action=="2"||action=="3"||action=="4"|| + action=="5"||action=="6"||action=="7"||action=="8"||action=="9"||action==".") { + if(operation=="~") { + str1+= str; + text.setText(str1);} + else if(action==".") + text.setText(text.getText() + "."); + else { + str2+=str; + text.setText(str2); + if (action==".") + text.setText(text.getText() + "."); + } + + text.setHorizontalAlignment(JTextField.RIGHT); + } + + //当用户按运算符号时的算法 + else if(action=="+"||action=="-"||action=="*"||action=="/"||action=="±"||action=="√"||action=="%"||action=="1/x") + { + operation=action; + } + + else if(action=="=") { + double a=Double.valueOf(str1);//将输入数据转换为double型 + double c; + if(operation=="+"||operation=="-"||operation=="/"||operation=="*"||operation=="±") { + + double b=Double.valueOf(str2); + if(operation=="+") { + c=a+b; + text.setText(c+"\n"); + } + if(operation=="-") { + c=a-b; + text.setText(c+""); + } + if(operation=="*") { + c=a*b; + text.setText(c+""); + } + if(operation=="/") { + if (b!=0) { + c=a/b; + text.setText(c+""); + } + else { + text.setText("除数不能为零"); + } + } + if(operation=="±") { + if (b == 0) + text.setText(str + "\n" + a); + else text.setText(str + "\n" + (a + b) + " & " + (a - b)); + } + } + + else if(operation=="√"||operation=="%"||operation=="1/x") { + if(operation=="√") { + text.setText(str + "\n" + Math.sqrt(a)); + } + if(operation=="%") { + text.setText(str + "\n" + a/100); + } + if(operation=="1/x") { + if(a!=0) + text.setText(str + "\n" + 1/a); + else + text.setText(str + "\n" + "不能求倒数"); + + } + } + operation="~"; + str1=""; + str2=""; + } + } + });//添加按钮功能监视器 + + + + } + //在东面界面上创建计算器上方三个功能按钮 + String buttontextString1[]= {"Backspace","CE","C"}; + JPanel jp1=new JPanel(new GridLayout(1,3) ); + jpeast.add(jp1,BorderLayout.NORTH); + for(int i=0;i<3;i++) {//利用for循环数组实现命名 + String a=buttontextString1[i]; + JButton jb1=new JButton(buttontextString1[i]); + jb1.setPreferredSize(new Dimension(117,30));//设置按钮大小 + jb1.setBackground(Color.WHITE);//设置按钮颜色 + jp1.add(jb1); + jb1.addActionListener(new ActionListener() {//设置三个功能键功能 + @Override + public void actionPerformed(ActionEvent e) { + if(a=="C") { + text.setText(""); + str1=""; + str2=""; + } + if(a=="CE") { + text.setText(""); + str1=""; + str2=""; + } + if(a=="Backspace") { + str2=str1.substring(str1.length()); + text.setText(str2); + } + } + }); + } + + + + jpeast.add(jp2,BorderLayout.SOUTH);//添加下方计算器按钮 + + jf.setVisible(true);//设置项目可见 + } +} diff --git a/src/java2022spring/Test.java b/src/java2022spring/Main.java similarity index 40% rename from src/java2022spring/Test.java rename to src/java2022spring/Main.java index 24deb29bfc0e5f50fdedcd21b504e77b529d48b6..86416c2b16b0b55ded06d60a675ff6be4acf7924 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Main.java @@ -1,7 +1,8 @@ -package java2022spring; - -public class Test { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} +package java2022spring; + +public class Main { + public static void main(String[] args) { + Calculator Calculator=new Calculator();//调用方法 + Calculator.Calculator(); +} +} diff --git a/src/java2022spring/image/calculator.jpeg b/src/java2022spring/image/calculator.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..1c9dbc78360109b0a53e4972dbb3470b1f5d4386 Binary files /dev/null and b/src/java2022spring/image/calculator.jpeg differ