diff --git a/.classpath b/.classpath deleted file mode 100644 index ac9ce57c2f4d3a99eb1868e101f3060156474663..0000000000000000000000000000000000000000 --- a/.classpath +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/.gitignore b/.gitignore index ae3c1726048cd06b9a143e0376ed46dd9b9a8d53..981aeb856a9b1b9235d497476e7847867227ce4a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -/bin/ +/out \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..35410cacdc5e87f985c93a96520f5e11a5c822e4 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000000000000000000000000000000000000..c5821b56bc9bed2b92528b3017cef113441d16d5 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..639900d13c6182e452e33a3bd638e70a0146c785 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e87dca2603b4700277c7e25d008f7da4d123c80 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000000000000000000000000000000000000..e96534fb27b68192f27f985d3879e173ec77adb8 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.project b/.project deleted file mode 100644 index b7e8a74e9dd98bfb5b42b70cd6033510d6823948..0000000000000000000000000000000000000000 --- a/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - java2022spring - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index dbc27c5dc60ef7094a88953087ea0294b6c44a38..0000000000000000000000000000000000000000 --- a/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,14 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=17 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning -org.eclipse.jdt.core.compiler.release=enabled -org.eclipse.jdt.core.compiler.source=17 diff --git a/java2022/java2022.iml b/java2022/java2022.iml new file mode 100644 index 0000000000000000000000000000000000000000..6db0c464af6419e65c295cc9408a43279d15e30a --- /dev/null +++ b/java2022/java2022.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/java2022/src/calculator.java b/java2022/src/calculator.java new file mode 100644 index 0000000000000000000000000000000000000000..394294da6a4c1741f2cca4aca3ec8b3d3c2792b2 --- /dev/null +++ b/java2022/src/calculator.java @@ -0,0 +1,283 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class calculator extends JFrame implements ActionListener { + //创建面板对象用来放置编辑框和标签 + private JPanel jPanel1=new JPanel(); + private JPanel jPanel2=new JPanel(); + private JPanel jPanel3=new JPanel(); + + //定义文本编辑框 + private JTextField jTextField1; + //定义一个可变字符串,用来显示计算过程 + StringBuffer str; + //x,y表示两个操作数 + double x,y; + //用flag来表示运算符,0表示“+”,1表示“-”,2表示“*”,3表示“/” + int flag; + //创建一个有12个按钮的数组 + private JButton jButton[]=new JButton[12]; + //算数功能按钮 + private JButton jButton1,jButton2,jButton3,jButton4, + jButton5,jButton6,jButton7, jButton8; + + public calculator(){ + super("我的计算器"); + + //建立计算器框架 + Container c = getContentPane(); //创建内容面板 + jTextField1=new JTextField("请进行计算",30); + jTextField1.setSize(15,30); + jTextField1.setEditable(false);//文本框不可编辑 + jTextField1.setBackground(Color.white);//设置颜色 + + str =new StringBuffer(); + jPanel2.add(jTextField1); + jPanel2.setLayout(new GridLayout(4,1));//设置布局 + + + for (int i = 0; i < 10; i++) { + String s=""+i; + jButton[i]=new JButton(s); + //设置按钮的字体样式 + jButton[i].setFont(new Font("楷体",Font.BOLD,15)); + //注册监听器 + jButton[i].addActionListener(this); + } + + //将按钮实例化 + jButton[10]=new JButton("-/+"); + jButton[11]=new JButton("."); + jButton1=new JButton("+"); + jButton2=new JButton("←"); + jButton3=new JButton("-"); + jButton4=new JButton("AC"); + jButton5=new JButton("*"); + jButton6=new JButton("√"); + jButton7=new JButton("/"); + jButton8=new JButton("="); + + + //设置按钮的前景色 + for (int i = 0; i < 12; i++) { + jButton[i].setForeground(Color.black); + } + jButton1.setForeground(Color.red); + jButton2.setForeground(Color.blue); + jButton3.setForeground(Color.red); + jButton4.setForeground(Color.blue); + jButton5.setForeground(Color.red); + jButton6.setForeground(Color.red); + jButton7.setForeground(Color.red); + jButton8.setForeground(Color.blue); + + //把按钮添加到面板上 + jPanel1.add(jButton[1]);jPanel1.add(jButton[2]);jPanel1.add(jButton[3]); + jPanel1.add(jButton1);jPanel1.add(jButton2); + jPanel1.add(jButton[4]);jPanel1.add(jButton[5]);jPanel1.add(jButton[6]); + jPanel1.add(jButton3);jPanel1.add(jButton4); + jPanel1.add(jButton[7]);jPanel1.add(jButton[8]);jPanel1.add(jButton[9]); + jPanel1.add(jButton5);jPanel1.add(jButton6); + jPanel1.add(jButton[0]);jPanel1.add(jButton[10]);jPanel1.add(jButton[11]); + jPanel1.add(jButton7);jPanel1.add(jButton8); + jPanel1.setLayout(new GridLayout(4,5,5,5)); + + + //注册按钮的监听器 + jButton[10].addActionListener( this); + jButton[11].addActionListener( this); + jButton1.addActionListener(this);jButton2.addActionListener(this); + jButton3.addActionListener(this);jButton4.addActionListener(this); + jButton5.addActionListener(this);jButton6.addActionListener(this); + jButton7.addActionListener(this);jButton8.addActionListener(this); + + //在容器上添加控件 + c.add(jPanel2); + c.add(jPanel1); + c.add(jPanel3); + c.setLayout(new FlowLayout());//顺序布局 + + //初始化面板 + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + setSize(400,300); + + } + + public static void main(String[] args) { + //主方法创建窗口 + try { + UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); + } catch (Exception e) { + e.printStackTrace(); + } + calculator frame=new calculator(); + frame.setBackground(Color.white); + } + + public void actionPerformed(ActionEvent e){ + //处理按钮事件 + try { + //清零 + if (e.getSource()==jButton4){ + jTextField1.setText("0"); + //对齐右边 + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + str.setLength(0); + } + //点击“+/-”选择输入正负数 + else if (e.getSource()==jButton[10]){ + //将trim后词转换为双精度浮点数 + x=Double.parseDouble(jTextField1.getText().trim()); + jTextField1.setText(""+(-x)); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + } + //加号 + else if (e.getSource()==jButton1) { + x=Double.parseDouble(jTextField1.getText().trim()); + str.setLength(0); + y=0d; + flag =0; + } + //减号 + else if (e.getSource()==jButton3) { + x=Double.parseDouble(jTextField1.getText().trim()); + str.setLength(0); + y=0d; + flag =1; + } + //乘号 + else if (e.getSource()==jButton5) { + x=Double.parseDouble(jTextField1.getText().trim()); + str.setLength(0); + y=0d; + flag =2; + } + //除号 + else if (e.getSource()==jButton7) { + x=Double.parseDouble(jTextField1.getText().trim()); + str.setLength(0); + y=0d; + flag =3; + } + //等号 + else if (e.getSource()==jButton8) { + str.setLength(0); + switch (flag) { + case 0 -> { + jTextField1.setText("" + (x + y)); + //设置字体颜色样式,位置 + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setFont(new Font("楷体", Font.BOLD, 10)); + jTextField1.setForeground(Color.black); + } + case 1 -> { + jTextField1.setText("" + (x - y)); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setFont(new Font("楷体", Font.BOLD, 10)); + jTextField1.setForeground(Color.black); + } + case 2 -> { + jTextField1.setText("" + (x * y)); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setFont(new Font("楷体", Font.BOLD, 10)); + jTextField1.setForeground(Color.black); + } + case 3 -> { + jTextField1.setText("" + (x / y)); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setFont(new Font("楷体", Font.BOLD, 10)); + jTextField1.setForeground(Color.black); + } + } + } + //"."按钮输入小数 + else if (e.getSource()==jButton[11]) { + if (jTextField1.getText().trim().indexOf('.')!=-1) { + + } + else{ + if (jTextField1.getText().trim().equals("0")) { + jTextField1.setText(str.append(e.getActionCommand()).toString()); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + } + else if (jTextField1.getText().trim().equals("")) { + + } + else { + jTextField1.setText((str.append(e.getActionCommand()).toString())); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + } + } + y=0d; + } + + //求平方根 + else if (e.getSource()==jButton6) { + x=Double.parseDouble(jTextField1.getText().trim()); + if (x<0) { + jTextField1.setText("数字格式异常"); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setForeground(Color.RED); + } + else { + double result=Math.sqrt(x); + jTextField1.setText(String.valueOf(result)); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setFont(new Font("楷体",Font.BOLD,10)); + } + str.setLength(0); + y=0d; + } + else { + //选择了“0”按键 + if(e.getSource()==jButton[0]) { + if (jTextField1.getText().trim().equals("0")){ + + } + else { + jTextField1.setText(str.append(e.getActionCommand()).toString()); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + y=Double.parseDouble(jTextField1.getText().trim()); + } + } + else if (e.getSource()==jButton2) { + //如果显示的不是0 + if (!jTextField1.getText().trim().equals("0")){ + if (str.length()!=1){ + jTextField1.setText(str.delete(str.length()-1, + str.length()).toString()); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + } + else { + jTextField1.setText("0"); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + str.setLength(0); + } + } + y=Double.parseDouble(jTextField1.getText().trim()); + } + else { + jTextField1.setText(str.append(e.getActionCommand()).toString()); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + y=Double.parseDouble(jTextField1.getText().trim()); + } + } + + + } + + catch (NumberFormatException e1){ + jTextField1.setText("数字格式异常"); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setForeground(Color.RED); + } + catch (StringIndexOutOfBoundsException e1){ + jTextField1.setText("字符索引越界"); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setForeground(Color.RED); + } + } + +} diff --git a/java2022spring.iml b/java2022spring.iml new file mode 100644 index 0000000000000000000000000000000000000000..9a5cfcef63aeb6c73691f546497af36a69541a4e --- /dev/null +++ b/java2022spring.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/readme.md b/readme.md deleted file mode 100644 index 4af16add33b576d095308a1888db35774618715d..0000000000000000000000000000000000000000 --- a/readme.md +++ /dev/null @@ -1,2 +0,0 @@ -# ѧ -0103 \ No newline at end of file diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java deleted file mode 100644 index 24deb29bfc0e5f50fdedcd21b504e77b529d48b6..0000000000000000000000000000000000000000 --- a/src/java2022spring/Test.java +++ /dev/null @@ -1,7 +0,0 @@ -package java2022spring; - -public class Test { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -}