From a6832cd66baea44da2d934ef873e71e2623d5d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=91=E5=AE=9A=E5=AE=87?= <2459036685@qq.com> Date: Fri, 20 May 2022 22:03:41 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=B8=80=E4=B8=8B?= =?UTF-8?q?=E5=96=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .classpath | 10 ---------- .gitignore | 2 +- .project | 17 ----------------- .settings/org.eclipse.jdt.core.prefs | 14 -------------- java2022/src/hello.java | 5 +++++ readme.md | 2 -- src/java2022spring/Test.java | 7 ------- 7 files changed, 6 insertions(+), 51 deletions(-) delete mode 100644 .classpath delete mode 100644 .project delete mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 java2022/src/hello.java delete mode 100644 readme.md delete mode 100644 src/java2022spring/Test.java diff --git a/.classpath b/.classpath deleted file mode 100644 index ac9ce57..0000000 --- a/.classpath +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/.gitignore b/.gitignore index ae3c172..981aeb8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -/bin/ +/out \ No newline at end of file diff --git a/.project b/.project deleted file mode 100644 index b7e8a74..0000000 --- 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 dbc27c5..0000000 --- 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/src/hello.java b/java2022/src/hello.java new file mode 100644 index 0000000..5525a73 --- /dev/null +++ b/java2022/src/hello.java @@ -0,0 +1,5 @@ +public class hello { + public static void main(String[] args) { + System.out.println("java设计性实验"); + } +} diff --git a/readme.md b/readme.md deleted file mode 100644 index 4af16ad..0000000 --- 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 24deb29..0000000 --- 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!"); - } -} -- Gitee From ffdcaa23ba4b4c4a79348809e2357448140b7c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=91=E5=AE=9A=E5=AE=87?= <2459036685@qq.com> Date: Wed, 1 Jun 2022 20:57:18 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java2022/src/calculator.java | 42 ++++++++++++++++++++++++++++++++++++ java2022/src/hello.java | 5 ----- 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 java2022/src/calculator.java delete mode 100644 java2022/src/hello.java diff --git a/java2022/src/calculator.java b/java2022/src/calculator.java new file mode 100644 index 0000000..acdeb19 --- /dev/null +++ b/java2022/src/calculator.java @@ -0,0 +1,42 @@ +import javax.swing.*; +import javax.swing.border.LineBorder; +import java.awt.*; + +public class calculator extends JFrame { + //创建面板对象用来放置编辑框和标签 + private JPanel jPanel1=new JPanel(); + private JPanel jPanel2=new JPanel(); + private JPanel jPanel3=new JPanel(); + //定义文本编辑框 + private JTextField jTextField1; + //定义标签 + private JLabel jLabel; + //定义一个可变字符串,用来显示计算过程 + StringBuffer stringBuffer; + //x,y表示两个操作数 + double x,y; + //用z来表示运算符,0表示“+”,1表示“-”,2表示“*”,3表示“/” + int z; + //创建一个有12个按钮的数组 + private JButton jButton[]=new JButton[12]; + //算数功能按钮 + private JButton jButton1,jButton2,jButton3,jButton4, + jButton5,jButton6,jButton7, jButton8; + + public calculator(){ + //建立计算器框架 + + + + + + + } + + + + public static void main(String[] args) { + calculator frame=new calculator(); + frame.setVisible(true); + } +} diff --git a/java2022/src/hello.java b/java2022/src/hello.java deleted file mode 100644 index 5525a73..0000000 --- a/java2022/src/hello.java +++ /dev/null @@ -1,5 +0,0 @@ -public class hello { - public static void main(String[] args) { - System.out.println("java设计性实验"); - } -} -- Gitee From fd8fd07f8f2c6a10a9e7b721d46b735a03881121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=91=E5=AE=9A=E5=AE=87?= <2459036685@qq.com> Date: Fri, 3 Jun 2022 15:30:05 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E7=AD=89=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 +++ .idea/compiler.xml | 8 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 9 +++ .idea/uiDesigner.xml | 124 +++++++++++++++++++++++++++++++++++ .idea/vcs.xml | 6 ++ java2022/java2022.iml | 11 ++++ java2022/src/calculator.java | 60 ++++++++++++++++- java2022spring.iml | 8 +++ 9 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 java2022/java2022.iml create mode 100644 java2022spring.iml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /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 0000000..c5821b5 --- /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 0000000..639900d --- /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 0000000..6e87dca --- /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 0000000..e96534f --- /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 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/java2022/java2022.iml b/java2022/java2022.iml new file mode 100644 index 0000000..6db0c46 --- /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 index acdeb19..ecd411d 100644 --- a/java2022/src/calculator.java +++ b/java2022/src/calculator.java @@ -1,6 +1,7 @@ import javax.swing.*; -import javax.swing.border.LineBorder; import java.awt.*; +import java.awt.event.ActionListener; +import java.io.*; public class calculator extends JFrame { //创建面板对象用来放置编辑框和标签 @@ -25,9 +26,66 @@ public class calculator extends JFrame { public calculator(){ //建立计算器框架 + super("岑定宇的计算器"); + Container container = getContentPane(); //创建内容面板 + jTextField1=new JTextField(30); + jTextField1.setEditable(false);//文本框不可编辑 + jTextField1.setBackground(Color.cyan);//设置颜色 + jLabel=new JLabel("欢迎使用本计算器"); + //设置标签的字体样式与颜色 + jLabel.setFont(new Font("楷体",Font.BOLD,20)); + jLabel.setForeground(Color.pink); + jLabel.setHorizontalAlignment(JLabel.CENTER);//设置标签居中 + stringBuffer=new StringBuffer(); + jPanel2.add(jLabel); + jPanel2.add(jTextField1); + jPanel2.setLayout(new GridLayout(4,1));//设置布局 + for (int i = 0; i < 10; i++) { + String s=""+i; + jButton[i]=new JButton(); + //设置按钮的字体样式 + jButton[i].setFont(new Font("楷体",Font.BOLD,15)); + //注册监听器 + jButton[i].addActionListener((ActionListener) this); + } + //将按钮实例化 + jButton[10]=new JButton("-/+"); + jButton[11]=new JButton("."); + jButton1=new JButton(); + jButton2=new JButton(); + jButton3=new JButton(); + jButton4=new JButton(); + jButton5=new JButton(); + jButton6=new JButton(); + jButton7=new JButton(); + jButton8=new JButton(); + + //设置按钮的前景色 + for (int i = 0; i < 12; i++) { + jButton[i].setForeground(Color.blue); + } + 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)); diff --git a/java2022spring.iml b/java2022spring.iml new file mode 100644 index 0000000..9a5cfce --- /dev/null +++ b/java2022spring.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file -- Gitee From 3802f60ecd5e8809203ab9313ee858adb1e16979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=91=E5=AE=9A=E5=AE=87?= <2459036685@qq.com> Date: Sat, 4 Jun 2022 00:04:59 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java2022/src/calculator.java | 115 +++++++++++++++++++++++++++++++++-- 1 file changed, 110 insertions(+), 5 deletions(-) diff --git a/java2022/src/calculator.java b/java2022/src/calculator.java index ecd411d..5c3dc58 100644 --- a/java2022/src/calculator.java +++ b/java2022/src/calculator.java @@ -1,9 +1,10 @@ import javax.swing.*; import java.awt.*; +import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; -public class calculator extends JFrame { +public class calculator extends JFrame implements ActionListener { //创建面板对象用来放置编辑框和标签 private JPanel jPanel1=new JPanel(); private JPanel jPanel2=new JPanel(); @@ -30,7 +31,7 @@ public class calculator extends JFrame { Container container = getContentPane(); //创建内容面板 jTextField1=new JTextField(30); jTextField1.setEditable(false);//文本框不可编辑 - jTextField1.setBackground(Color.cyan);//设置颜色 + jTextField1.setBackground(Color.white);//设置颜色 jLabel=new JLabel("欢迎使用本计算器"); //设置标签的字体样式与颜色 @@ -48,7 +49,7 @@ public class calculator extends JFrame { //设置按钮的字体样式 jButton[i].setFont(new Font("楷体",Font.BOLD,15)); //注册监听器 - jButton[i].addActionListener((ActionListener) this); + jButton[i].addActionListener(this); } //将按钮实例化 @@ -88,13 +89,117 @@ public class calculator extends JFrame { 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); - } + //在容器上添加控件 + container.add(jPanel2); + container.add(jPanel1); + container.add(jPanel3); + container.setLayout(new FlowLayout());//顺序布局 + //初始化面板 + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + setSize(400,300); + } public static void main(String[] args) { + //主方法创建窗口 calculator frame=new calculator(); - frame.setVisible(true); + frame.setBackground(Color.white); + } + + public void actionPerformed(ActionEvent e){ + //处理按钮事件 + try { + //清零 + if (e.getSource()==jButton4){ + jTextField1.setText("0"); + //对齐右边 + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + stringBuffer.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()); + stringBuffer.setLength(0); + y=0d; + z=0; + } + //减号 + else if (e.getSource()==jButton3) { + x=Double.parseDouble(jTextField1.getText().trim()); + stringBuffer.setLength(0); + y=0d; + z=1; + } + //乘号 + else if (e.getSource()==jButton5) { + x=Double.parseDouble(jTextField1.getText().trim()); + stringBuffer.setLength(0); + y=0d; + z=2; + } + //除号 + else if (e.getSource()==jButton7) { + x=Double.parseDouble(jTextField1.getText().trim()); + stringBuffer.setLength(0); + y=0d; + z=3; + } + //等号 + else if (e.getSource()==jButton8) { + stringBuffer.setLength(0); + switch (z){ + case 0:jTextField1.setText(""+(x+y)); + //设置字体颜色样式,位置 + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setFont(new Font("楷体",Font.BOLD,10)); + jTextField1.setForeground(Color.black); + break; + case 1:jTextField1.setText(""+(x-y)); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setFont(new Font("楷体",Font.BOLD,10)); + jTextField1.setForeground(Color.black); + break; + case 2:jTextField1.setText(""+(x*y)); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setFont(new Font("楷体",Font.BOLD,10)); + jTextField1.setForeground(Color.black); + break; + case 3:jTextField1.setText(""+(x/y)); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setFont(new Font("楷体",Font.BOLD,10)); + jTextField1.setForeground(Color.black); + break; + + } + } + + } + + } + + + + + + + + } -- Gitee From 375594f5db97d949253040bfd9f979a318c250bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=91=E5=AE=9A=E5=AE=87?= <2459036685@qq.com> Date: Sat, 4 Jun 2022 15:22:16 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=99=A8=E5=88=9D?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java2022/src/calculator.java | 139 +++++++++++++++++++++++++++-------- 1 file changed, 107 insertions(+), 32 deletions(-) diff --git a/java2022/src/calculator.java b/java2022/src/calculator.java index 5c3dc58..592e608 100644 --- a/java2022/src/calculator.java +++ b/java2022/src/calculator.java @@ -2,7 +2,6 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.io.*; public class calculator extends JFrame implements ActionListener { //创建面板对象用来放置编辑框和标签 @@ -14,7 +13,7 @@ public class calculator extends JFrame implements ActionListener { //定义标签 private JLabel jLabel; //定义一个可变字符串,用来显示计算过程 - StringBuffer stringBuffer; + StringBuffer str; //x,y表示两个操作数 double x,y; //用z来表示运算符,0表示“+”,1表示“-”,2表示“*”,3表示“/” @@ -27,25 +26,27 @@ public class calculator extends JFrame implements ActionListener { public calculator(){ //建立计算器框架 - super("岑定宇的计算器"); - Container container = getContentPane(); //创建内容面板 + super("我的计算器"); + Container c = getContentPane(); //创建内容面板 jTextField1=new JTextField(30); jTextField1.setEditable(false);//文本框不可编辑 jTextField1.setBackground(Color.white);//设置颜色 - jLabel=new JLabel("欢迎使用本计算器"); + jLabel=new JLabel(""); //设置标签的字体样式与颜色 - jLabel.setFont(new Font("楷体",Font.BOLD,20)); + jLabel.setFont(new Font("黑体",Font.BOLD,20)); jLabel.setForeground(Color.pink); jLabel.setHorizontalAlignment(JLabel.CENTER);//设置标签居中 - stringBuffer=new StringBuffer(); + + str =new StringBuffer(); jPanel2.add(jLabel); jPanel2.add(jTextField1); jPanel2.setLayout(new GridLayout(4,1));//设置布局 + for (int i = 0; i < 10; i++) { String s=""+i; - jButton[i]=new JButton(); + jButton[i]=new JButton(s); //设置按钮的字体样式 jButton[i].setFont(new Font("楷体",Font.BOLD,15)); //注册监听器 @@ -55,18 +56,18 @@ public class calculator extends JFrame implements ActionListener { //将按钮实例化 jButton[10]=new JButton("-/+"); jButton[11]=new JButton("."); - jButton1=new JButton(); - jButton2=new JButton(); - jButton3=new JButton(); - jButton4=new JButton(); - jButton5=new JButton(); - jButton6=new JButton(); - jButton7=new JButton(); - jButton8=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.blue); + jButton[i].setForeground(Color.black); } jButton1.setForeground(Color.red); jButton2.setForeground(Color.blue); @@ -98,10 +99,10 @@ public class calculator extends JFrame implements ActionListener { jButton7.addActionListener(this);jButton8.addActionListener(this); //在容器上添加控件 - container.add(jPanel2); - container.add(jPanel1); - container.add(jPanel3); - container.setLayout(new FlowLayout());//顺序布局 + c.add(jPanel2); + c.add(jPanel1); + c.add(jPanel3); + c.setLayout(new FlowLayout());//顺序布局 //初始化面板 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -124,7 +125,7 @@ public class calculator extends JFrame implements ActionListener { jTextField1.setText("0"); //对齐右边 jTextField1.setHorizontalAlignment(JTextField.RIGHT); - stringBuffer.setLength(0); + str.setLength(0); } //点击“+/-”选择输入正负数 else if (e.getSource()==jButton[10]){ @@ -136,34 +137,34 @@ public class calculator extends JFrame implements ActionListener { //加号 else if (e.getSource()==jButton1) { x=Double.parseDouble(jTextField1.getText().trim()); - stringBuffer.setLength(0); + str.setLength(0); y=0d; z=0; } //减号 else if (e.getSource()==jButton3) { x=Double.parseDouble(jTextField1.getText().trim()); - stringBuffer.setLength(0); + str.setLength(0); y=0d; z=1; } //乘号 else if (e.getSource()==jButton5) { x=Double.parseDouble(jTextField1.getText().trim()); - stringBuffer.setLength(0); + str.setLength(0); y=0d; z=2; } //除号 else if (e.getSource()==jButton7) { x=Double.parseDouble(jTextField1.getText().trim()); - stringBuffer.setLength(0); + str.setLength(0); y=0d; z=3; } //等号 else if (e.getSource()==jButton8) { - stringBuffer.setLength(0); + str.setLength(0); switch (z){ case 0:jTextField1.setText(""+(x+y)); //设置字体颜色样式,位置 @@ -189,17 +190,91 @@ public class calculator extends JFrame implements ActionListener { } } + //"."按钮输入小数 + 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(Double.valueOf(x)); + 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); + } + } } -- Gitee From 4502c7daaee849c507e6072727ff6fc71bce0939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=91=E5=AE=9A=E5=AE=87?= <2459036685@qq.com> Date: Sun, 5 Jun 2022 19:04:58 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E5=B7=AE=E4=B8=8D=E5=A4=9A=E6=90=9E?= =?UTF-8?q?=E5=AE=8C=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java2022/src/calculator.java | 65 +++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/java2022/src/calculator.java b/java2022/src/calculator.java index 592e608..f3d46f3 100644 --- a/java2022/src/calculator.java +++ b/java2022/src/calculator.java @@ -1,17 +1,15 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +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; - //定义标签 - private JLabel jLabel; //定义一个可变字符串,用来显示计算过程 StringBuffer str; //x,y表示两个操作数 @@ -25,21 +23,16 @@ public class calculator extends JFrame implements ActionListener { jButton5,jButton6,jButton7, jButton8; public calculator(){ - //建立计算器框架 super("我的计算器"); + + //建立计算器框架 Container c = getContentPane(); //创建内容面板 - jTextField1=new JTextField(30); + jTextField1=new JTextField("请进行计算",30); + jTextField1.setSize(15,30); jTextField1.setEditable(false);//文本框不可编辑 jTextField1.setBackground(Color.white);//设置颜色 - jLabel=new JLabel(""); - //设置标签的字体样式与颜色 - jLabel.setFont(new Font("黑体",Font.BOLD,20)); - jLabel.setForeground(Color.pink); - jLabel.setHorizontalAlignment(JLabel.CENTER);//设置标签居中 - str =new StringBuffer(); - jPanel2.add(jLabel); jPanel2.add(jTextField1); jPanel2.setLayout(new GridLayout(4,1));//设置布局 @@ -65,6 +58,7 @@ public class calculator extends JFrame implements ActionListener { jButton7=new JButton("/"); jButton8=new JButton("="); + //设置按钮的前景色 for (int i = 0; i < 12; i++) { jButton[i].setForeground(Color.black); @@ -113,6 +107,11 @@ public class calculator extends JFrame implements ActionListener { 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); } @@ -165,29 +164,32 @@ public class calculator extends JFrame implements ActionListener { //等号 else if (e.getSource()==jButton8) { str.setLength(0); - switch (z){ - case 0:jTextField1.setText(""+(x+y)); - //设置字体颜色样式,位置 - jTextField1.setHorizontalAlignment(JTextField.RIGHT); - jTextField1.setFont(new Font("楷体",Font.BOLD,10)); - jTextField1.setForeground(Color.black); - break; - case 1:jTextField1.setText(""+(x-y)); + switch (z) { + case 0 -> { + jTextField1.setText("" + (x + y)); + //设置字体颜色样式,位置 jTextField1.setHorizontalAlignment(JTextField.RIGHT); - jTextField1.setFont(new Font("楷体",Font.BOLD,10)); + jTextField1.setFont(new Font("楷体", Font.BOLD, 10)); jTextField1.setForeground(Color.black); - break; - case 2:jTextField1.setText(""+(x*y)); + } + case 1 -> { + jTextField1.setText("" + (x - y)); jTextField1.setHorizontalAlignment(JTextField.RIGHT); - jTextField1.setFont(new Font("楷体",Font.BOLD,10)); + jTextField1.setFont(new Font("楷体", Font.BOLD, 10)); jTextField1.setForeground(Color.black); - break; - case 3:jTextField1.setText(""+(x/y)); + } + case 2 -> { + jTextField1.setText("" + (x * y)); jTextField1.setHorizontalAlignment(JTextField.RIGHT); - jTextField1.setFont(new Font("楷体",Font.BOLD,10)); + jTextField1.setFont(new Font("楷体", Font.BOLD, 10)); jTextField1.setForeground(Color.black); - break; - + } + case 3 -> { + jTextField1.setText("" + (x / y)); + jTextField1.setHorizontalAlignment(JTextField.RIGHT); + jTextField1.setFont(new Font("楷体", Font.BOLD, 10)); + jTextField1.setForeground(Color.black); + } } } //"."按钮输入小数 @@ -220,7 +222,8 @@ public class calculator extends JFrame implements ActionListener { jTextField1.setForeground(Color.RED); } else { - double result=Math.sqrt(Double.valueOf(x)); + double result=Math.sqrt(x); + jTextField1.setText(String.valueOf(result)); jTextField1.setHorizontalAlignment(JTextField.RIGHT); jTextField1.setFont(new Font("楷体",Font.BOLD,10)); } -- Gitee From d30d77caa8befd2b05d9954ab02ad3dae1db72e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=91=E5=AE=9A=E5=AE=87?= <2459036685@qq.com> Date: Tue, 7 Jun 2022 19:57:50 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java2022/src/calculator.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/java2022/src/calculator.java b/java2022/src/calculator.java index f3d46f3..d8d7dac 100644 --- a/java2022/src/calculator.java +++ b/java2022/src/calculator.java @@ -15,7 +15,7 @@ public class calculator extends JFrame implements ActionListener { //x,y表示两个操作数 double x,y; //用z来表示运算符,0表示“+”,1表示“-”,2表示“*”,3表示“/” - int z; + int flag; //创建一个有12个按钮的数组 private JButton jButton[]=new JButton[12]; //算数功能按钮 @@ -138,33 +138,33 @@ public class calculator extends JFrame implements ActionListener { x=Double.parseDouble(jTextField1.getText().trim()); str.setLength(0); y=0d; - z=0; + flag =0; } //减号 else if (e.getSource()==jButton3) { x=Double.parseDouble(jTextField1.getText().trim()); str.setLength(0); y=0d; - z=1; + flag =1; } //乘号 else if (e.getSource()==jButton5) { x=Double.parseDouble(jTextField1.getText().trim()); str.setLength(0); y=0d; - z=2; + flag =2; } //除号 else if (e.getSource()==jButton7) { x=Double.parseDouble(jTextField1.getText().trim()); str.setLength(0); y=0d; - z=3; + flag =3; } //等号 else if (e.getSource()==jButton8) { str.setLength(0); - switch (z) { + switch (flag) { case 0 -> { jTextField1.setText("" + (x + y)); //设置字体颜色样式,位置 -- Gitee From 264e94954bb4cda8b9a3fd474de2a7f129bee4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=91=E5=AE=9A=E5=AE=87?= <2459036685@qq.com> Date: Tue, 7 Jun 2022 23:16:20 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=96=87=E6=9C=AC=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java2022/src/calculator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java2022/src/calculator.java b/java2022/src/calculator.java index d8d7dac..394294d 100644 --- a/java2022/src/calculator.java +++ b/java2022/src/calculator.java @@ -14,7 +14,7 @@ public class calculator extends JFrame implements ActionListener { StringBuffer str; //x,y表示两个操作数 double x,y; - //用z来表示运算符,0表示“+”,1表示“-”,2表示“*”,3表示“/” + //用flag来表示运算符,0表示“+”,1表示“-”,2表示“*”,3表示“/” int flag; //创建一个有12个按钮的数组 private JButton jButton[]=new JButton[12]; -- Gitee