From 1a0d78f9630eb855a290e4e4bc011266c8537c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E5=98=89=E9=92=B0?= <鑼冨槈閽癅LAPTOP-RJ8KNJN6> Date: Sat, 4 Jun 2022 22:44:49 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E5=99=A8=E6=8C=89=E9=92=AE=E5=92=8C=E9=9D=A2=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Test.java | 100 +++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29..c740d4e 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,7 +1,101 @@ package java2022spring; +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; -public class Test { +public class Test extends Frame { public static void main(String[] args) { - System.out.println("Hello world!"); - } + +Frame frame=new JFrame("计算器");//标题为计算器 +Panel top=new Panel(); +top.add(new JTextField(30));//结果显示 +frame.add(top,BorderLayout.NORTH);//JTextField放在窗体北面 + +Panel panelleft=new Panel();//左父容器 +Panel panelright=new Panel();//右父容器 +frame.add(panelleft,BorderLayout.WEST); +frame.add(panelright,BorderLayout.CENTER); + + +frame.setBounds(100,100,190,120); +frame.setVisible(true); +((JFrame) frame).setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//退出 + +Panel p1=new Panel();//左上容器 +p1.setLayout(new GridLayout(1,1)); +Button b=new Button("其他"); +p1.add(b); +panelleft.add(p1); +p1.setVisible(true); + +Panel p2=new Panel();//左下容器 +frame.add(p2); +p2.setLayout(new GridLayout(4,1)); +Button b1=new Button("MC"); +Button b2=new Button("MR"); +Button b3=new Button("MS"); +Button b4=new Button("M+"); +p2.add(b1); +p2.add(b2); +p2.add(b3); +p2.add(b4); +panelright.add(p2); +p2.setVisible(true); + +Panel p3=new Panel(); +p3.setLayout(new GridLayout(1,3)); +Button b5=new Button("Backspace"); +Button b6=new Button("CE"); +Button b7=new Button("C"); +p3.add(b5); +p3.add(b6); +p3.add(b7); + +Panel p4=new Panel(); +p4.setLayout(new GridLayout(4,5)); +Button b8=new Button("7"); +Button b9=new Button("8"); +Button b10=new Button("9"); +Button b11=new Button("/"); +Button b12=new Button("sqrt"); +Button b13=new Button("4"); +Button b14=new Button("5"); +Button b15=new Button("6"); +Button b16=new Button("*"); +Button b17=new Button("%"); +Button b18=new Button("1"); +Button b19=new Button("2"); +Button b20=new Button("3"); +Button b21=new Button("-"); +Button b22=new Button("1/x"); +Button b23=new Button("0"); +Button b24=new Button("+/-"); +Button b25=new Button("."); +Button b26=new Button("+"); +Button b27=new Button("="); +p4.add(b8); +p4.add(b9); +p4.add(b10); +p4.add(b11); +p4.add(b12); +p4.add(b13); +p4.add(b14); +p4.add(b15); +p4.add(b16); +p4.add(b17); +p4.add(b18); +p4.add(b19); +p4.add(b20); +p4.add(b21); +p4.add(b22); +p4.add(b23); +p4.add(b24); +p4.add(b25); +p4.add(b26); +p4.add(b27); } + } + + + + -- Gitee From ef95b072b5863db61650944ac9b7a5924d31ee21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E5=98=89=E9=92=B0?= <鑼冨槈閽癅LAPTOP-RJ8KNJN6> Date: Tue, 7 Jun 2022 14:25:20 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E4=BA=86=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=99=A8=E7=AA=97=E5=8F=A3=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E7=9B=B8=E5=BA=94=E6=8C=89=E9=92=AE=E5=92=8C=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E7=BB=93=E6=9E=9C=E6=98=BE=E7=A4=BA=E5=B1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Test.java | 254 ++++++++++++++++++++++------------- 1 file changed, 159 insertions(+), 95 deletions(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index c740d4e..4abb7b4 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,101 +1,165 @@ package java2022spring; -import javax.swing.*; + import java.awt.*; -import java.awt.event.*; - -public class Test extends Frame { - public static void main(String[] args) { - -Frame frame=new JFrame("计算器");//标题为计算器 -Panel top=new Panel(); -top.add(new JTextField(30));//结果显示 -frame.add(top,BorderLayout.NORTH);//JTextField放在窗体北面 - -Panel panelleft=new Panel();//左父容器 -Panel panelright=new Panel();//右父容器 -frame.add(panelleft,BorderLayout.WEST); -frame.add(panelright,BorderLayout.CENTER); - - -frame.setBounds(100,100,190,120); -frame.setVisible(true); -((JFrame) frame).setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//退出 - -Panel p1=new Panel();//左上容器 -p1.setLayout(new GridLayout(1,1)); -Button b=new Button("其他"); -p1.add(b); -panelleft.add(p1); -p1.setVisible(true); - -Panel p2=new Panel();//左下容器 -frame.add(p2); -p2.setLayout(new GridLayout(4,1)); -Button b1=new Button("MC"); -Button b2=new Button("MR"); -Button b3=new Button("MS"); -Button b4=new Button("M+"); -p2.add(b1); -p2.add(b2); -p2.add(b3); -p2.add(b4); -panelright.add(p2); -p2.setVisible(true); - -Panel p3=new Panel(); -p3.setLayout(new GridLayout(1,3)); -Button b5=new Button("Backspace"); -Button b6=new Button("CE"); -Button b7=new Button("C"); -p3.add(b5); -p3.add(b6); -p3.add(b7); - -Panel p4=new Panel(); -p4.setLayout(new GridLayout(4,5)); -Button b8=new Button("7"); -Button b9=new Button("8"); -Button b10=new Button("9"); -Button b11=new Button("/"); -Button b12=new Button("sqrt"); -Button b13=new Button("4"); -Button b14=new Button("5"); -Button b15=new Button("6"); -Button b16=new Button("*"); -Button b17=new Button("%"); -Button b18=new Button("1"); -Button b19=new Button("2"); -Button b20=new Button("3"); -Button b21=new Button("-"); -Button b22=new Button("1/x"); -Button b23=new Button("0"); -Button b24=new Button("+/-"); -Button b25=new Button("."); -Button b26=new Button("+"); -Button b27=new Button("="); -p4.add(b8); -p4.add(b9); -p4.add(b10); -p4.add(b11); -p4.add(b12); -p4.add(b13); -p4.add(b14); -p4.add(b15); -p4.add(b16); -p4.add(b17); -p4.add(b18); -p4.add(b19); -p4.add(b20); -p4.add(b21); -p4.add(b22); -p4.add(b23); -p4.add(b24); -p4.add(b25); -p4.add(b26); -p4.add(b27); +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JTextField; +import javax.swing.border.LineBorder; + +public class Test extends JFrame{ +JTextField showView=null; + +public static void main(String[] args) { +new Test(); } - } + +public Test() //设置计算器尺寸大小和位置 +{ + setTitle("计算器"); + setBounds(300,150,317,375); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setResizable(false); + setLayout(null); + addView(); + addButton(); + setVisible(true); +} - +public void addButton() //添加计算器按钮,设置字体大小 +{ +JButton button_c=new JButton("c"); +button_c.setBounds(5,65,70,50); +button_c.setBorder(new LineBorder(Color.BLACK,1)); +button_c.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_c); + +JButton button_mod=new JButton("%"); +button_mod.setBounds(80,65,70,50); +button_mod.setBorder(new LineBorder(Color.BLACK,1)); +button_mod.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_mod); + +JButton button_clean=new JButton("back"); +button_clean.setBounds(155,65,70,50); +button_clean.setBorder(new LineBorder(Color.BLACK,1)); +button_clean.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_clean); + +JButton button_div=new JButton("/"); +button_div.setBounds(230,65,70,50); +button_div.setBorder(new LineBorder(Color.BLACK,1)); +button_div.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_div); + +JButton button_7=new JButton("7"); +button_7.setBounds(5,120,70,50); +button_7.setBorder(new LineBorder(Color.BLACK,1)); +button_7.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_7); + +JButton button_8=new JButton("8"); +button_8.setBounds(80,120,70,50); +button_8.setBorder(new LineBorder(Color.BLACK,1)); +button_8.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_8); + +JButton button_9=new JButton("9"); +button_9.setBounds(155,120,70,50); +button_9.setBorder(new LineBorder(Color.BLACK,1)); +button_9.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_9); + +JButton button_mul=new JButton("*"); +button_mul.setBounds(230,120,70,50); +button_mul.setBorder(new LineBorder(Color.BLACK,1)); +button_mul.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_mul); + +JButton button_4=new JButton("4"); +button_4.setBounds(5,175,70,50); +button_4.setBorder(new LineBorder(Color.BLACK,1)); +button_4.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_4); + +JButton button_5=new JButton("5"); +button_5.setBounds(80,175,70,50); +button_5.setBorder(new LineBorder(Color.BLACK,1)); +button_5.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_5); + +JButton button_6=new JButton("6"); +button_6.setBounds(155,175,70,50); +button_6.setBorder(new LineBorder(Color.BLACK,1)); +button_6.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_6); + +JButton button_sub=new JButton("-"); +button_sub.setBounds(230,175,70,50); +button_sub.setBorder(new LineBorder(Color.BLACK,1)); +button_sub.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_sub); + +JButton button_1=new JButton("1"); +button_1.setBounds(5,230,70,50); +button_1.setBorder(new LineBorder(Color.BLACK,1)); +button_1.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_1); + +JButton button_2=new JButton("2"); +button_2.setBounds(80,230,70,50); +button_2.setBorder(new LineBorder(Color.BLACK,1)); +button_2.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_2); + +JButton button_3=new JButton("3"); +button_3.setBounds(155,230,70,50); +button_3.setBorder(new LineBorder(Color.BLACK,1)); +button_3.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_3); + +JButton button_add=new JButton("+"); +button_add.setBounds(230,230,70,50); +button_add.setBorder(new LineBorder(Color.BLACK,1)); +button_add.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_add); + +JButton button_00=new JButton("00"); +button_00.setBounds(5,285,70,50); +button_00.setBorder(new LineBorder(Color.BLACK,1)); +button_00.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_00); + +JButton button_0=new JButton("0"); +button_0.setBounds(80,285,70,50); +button_0.setBorder(new LineBorder(Color.BLACK,1)); +button_0.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_0); + +JButton button_point=new JButton("."); +button_point.setBounds(155,285,70,50); +button_point.setBorder(new LineBorder(Color.BLACK,1)); +button_point.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_point); + +JButton button_eq=new JButton("="); +button_eq.setBounds(230,285,70,50); +button_eq.setBorder(new LineBorder(Color.BLACK,1)); +button_eq.setFont(new Font("Cambria",Font.PLAIN,23)); +add(button_eq); + + +} + + + +public void addView() {//结果显示屏 + showView=new JTextField("0"); + showView.setBounds(5,3,295,60); + showView.setHorizontalAlignment(JTextField.RIGHT); + showView.setEditable(false); + showView.setBackground(Color.WHITE); + showView.setFont(new Font("Cambria",Font.BOLD,25)); + add(showView); +}} \ No newline at end of file -- Gitee From b9d4205c3ae51a37500bf77cacf58bfce7f71c56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E5=98=89=E9=92=B0?= <鑼冨槈閽癅LAPTOP-RJ8KNJN6> Date: Tue, 7 Jun 2022 23:43:04 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=99=A8=E6=8C=89=E9=92=AE=E5=B8=83=E5=B1=80=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E8=BF=90=E7=AE=97=E7=AC=A6=E5=8F=B7?= =?UTF-8?q?=E5=92=8C=E5=90=84=E4=B8=AA=E6=8C=89=E9=92=AE=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E5=99=A8=EF=BC=8C=E5=8F=AF=E4=BB=A5=E8=BF=9B=E8=A1=8C=E7=AE=80?= =?UTF-8?q?=E6=98=93=E7=9A=84=E8=AE=A1=E7=AE=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Test.java | 253 +++++++++++++++++++++++++++-------- 1 file changed, 200 insertions(+), 53 deletions(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 4abb7b4..c213f95 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,23 +1,32 @@ package java2022spring; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.math.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.border.LineBorder; public class Test extends JFrame{ -JTextField showView=null; - +JTextField showView=null;//结果显示屏 +NumActionListener numActionListener;//数字监听器 +SymActionListener symActionListener;//符号监听器 +String logic;//符号 +String s1="";//第一个数字 +String s2="";//第二个数字 +String result=""; +boolean isResult=false; + public static void main(String[] args) { new Test(); } - -public Test() //设置计算器尺寸大小和位置 +public Test() //设置计算器尺寸和位置等 { - setTitle("计算器"); - setBounds(300,150,317,375); + setTitle("测试计算器"); + setBounds(300,150,317,435); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setLayout(null); @@ -26,140 +35,278 @@ public Test() // setVisible(true); } +public void addView() {//结果显示屏 + showView=new JTextField("0"); + showView.setBounds(5,5,296,60); + showView.setHorizontalAlignment(JTextField.RIGHT); + showView.setEditable(false); + showView.setBackground(Color.WHITE); + showView.setFont(new Font("Cambria",Font.BOLD,25)); + add(showView); +} public void addButton() //添加计算器按钮,设置字体大小 { -JButton button_c=new JButton("c"); -button_c.setBounds(5,65,70,50); +numActionListener=new NumActionListener();//数字监听器 +symActionListener=new SymActionListener();//符号监听器 + +JButton button_c=new JButton("C");//清除符号 +button_c.setBounds(5,70,145,50); button_c.setBorder(new LineBorder(Color.BLACK,1)); button_c.setFont(new Font("Cambria",Font.PLAIN,23)); +button_c.addActionListener(new CleanActionListener()); add(button_c); -JButton button_mod=new JButton("%"); -button_mod.setBounds(80,65,70,50); +JButton button_back=new JButton("back");//回退符号 +button_back.setBounds(155,70,145,50); +button_back.setBorder(new LineBorder(Color.BLACK,1)); +button_back.setFont(new Font("Cambria",Font.PLAIN,23)); +button_back.addActionListener(e->{ //判断当前是否存在运算符号 + if(!isNotNullAndEmpty(logic)){ + if(s1==null||s1.length()==0) { + return; + }else { + s1=s1.substring(0,s1.length()-1); + } + }else { + if(s1==null||s2.length()==0) {//存在运算符,删除运算符 + logic=""; + }else { + s2=s2.substring(0,s2.length()-1); /*64-77行代码来自百度文库*/ + }} + result=(isNotNullAndEmpty(s1)?s1:"")+(isNotNullAndEmpty(logic)?logic:"")+(isNotNullAndEmpty(s2)?s2:""); + showView.setText(result); +}); +add(button_back); + +JButton button_log=new JButton("log");//对数符号 +button_log.setBounds(5,125,70,50); +button_log.setBorder(new LineBorder(Color.BLACK,1)); +button_log.setFont(new Font("Cambria",Font.PLAIN,23)); +button_log.addActionListener(symActionListener); +add(button_log); + +JButton button_p=new JButton("^");//次方符号 +button_p.setBounds(80,125,70,50); +button_p.setBorder(new LineBorder(Color.BLACK,1)); +button_p.setFont(new Font("Cambria",Font.PLAIN,23)); +button_p.addActionListener(symActionListener); +add(button_p); + +JButton button_mod=new JButton("%");//余数符号 +button_mod.setBounds(155,125,70,50); button_mod.setBorder(new LineBorder(Color.BLACK,1)); button_mod.setFont(new Font("Cambria",Font.PLAIN,23)); +button_mod.addActionListener(symActionListener); add(button_mod); -JButton button_clean=new JButton("back"); -button_clean.setBounds(155,65,70,50); -button_clean.setBorder(new LineBorder(Color.BLACK,1)); -button_clean.setFont(new Font("Cambria",Font.PLAIN,23)); -add(button_clean); -JButton button_div=new JButton("/"); -button_div.setBounds(230,65,70,50); +JButton button_div=new JButton("/");//除号 +button_div.setBounds(230,125,70,50); button_div.setBorder(new LineBorder(Color.BLACK,1)); button_div.setFont(new Font("Cambria",Font.PLAIN,23)); +button_div.addActionListener(symActionListener); add(button_div); JButton button_7=new JButton("7"); -button_7.setBounds(5,120,70,50); +button_7.setBounds(5,180,70,50); button_7.setBorder(new LineBorder(Color.BLACK,1)); button_7.setFont(new Font("Cambria",Font.PLAIN,23)); +button_7.addActionListener(numActionListener); add(button_7); JButton button_8=new JButton("8"); -button_8.setBounds(80,120,70,50); +button_8.setBounds(80,180,70,50); button_8.setBorder(new LineBorder(Color.BLACK,1)); button_8.setFont(new Font("Cambria",Font.PLAIN,23)); +button_8.addActionListener(numActionListener); add(button_8); JButton button_9=new JButton("9"); -button_9.setBounds(155,120,70,50); +button_9.setBounds(155,180,70,50); button_9.setBorder(new LineBorder(Color.BLACK,1)); button_9.setFont(new Font("Cambria",Font.PLAIN,23)); +button_9.addActionListener(numActionListener); add(button_9); -JButton button_mul=new JButton("*"); -button_mul.setBounds(230,120,70,50); +JButton button_mul=new JButton("*");//乘号 +button_mul.setBounds(230,180,70,50); button_mul.setBorder(new LineBorder(Color.BLACK,1)); button_mul.setFont(new Font("Cambria",Font.PLAIN,23)); +button_mul.addActionListener(symActionListener); add(button_mul); JButton button_4=new JButton("4"); -button_4.setBounds(5,175,70,50); +button_4.setBounds(5,235,70,50); button_4.setBorder(new LineBorder(Color.BLACK,1)); button_4.setFont(new Font("Cambria",Font.PLAIN,23)); +button_4.addActionListener(numActionListener); add(button_4); JButton button_5=new JButton("5"); -button_5.setBounds(80,175,70,50); +button_5.setBounds(80,235,70,50); button_5.setBorder(new LineBorder(Color.BLACK,1)); button_5.setFont(new Font("Cambria",Font.PLAIN,23)); +button_5.addActionListener(numActionListener); add(button_5); JButton button_6=new JButton("6"); -button_6.setBounds(155,175,70,50); +button_6.setBounds(155,235,70,50); button_6.setBorder(new LineBorder(Color.BLACK,1)); button_6.setFont(new Font("Cambria",Font.PLAIN,23)); +button_6.addActionListener(numActionListener); add(button_6); -JButton button_sub=new JButton("-"); -button_sub.setBounds(230,175,70,50); +JButton button_sub=new JButton("-");//减号 +button_sub.setBounds(230,235,70,50); button_sub.setBorder(new LineBorder(Color.BLACK,1)); button_sub.setFont(new Font("Cambria",Font.PLAIN,23)); +button_sub.addActionListener(symActionListener); add(button_sub); JButton button_1=new JButton("1"); -button_1.setBounds(5,230,70,50); +button_1.setBounds(5,290,70,50); button_1.setBorder(new LineBorder(Color.BLACK,1)); button_1.setFont(new Font("Cambria",Font.PLAIN,23)); +button_1.addActionListener(symActionListener); add(button_1); JButton button_2=new JButton("2"); -button_2.setBounds(80,230,70,50); +button_2.setBounds(80,290,70,50); button_2.setBorder(new LineBorder(Color.BLACK,1)); button_2.setFont(new Font("Cambria",Font.PLAIN,23)); +button_2.addActionListener(numActionListener); add(button_2); JButton button_3=new JButton("3"); -button_3.setBounds(155,230,70,50); +button_3.setBounds(155,290,70,50); button_3.setBorder(new LineBorder(Color.BLACK,1)); button_3.setFont(new Font("Cambria",Font.PLAIN,23)); +button_3.addActionListener(numActionListener); add(button_3); JButton button_add=new JButton("+"); -button_add.setBounds(230,230,70,50); +button_add.setBounds(230,290,70,50); button_add.setBorder(new LineBorder(Color.BLACK,1)); button_add.setFont(new Font("Cambria",Font.PLAIN,23)); +button_add.addActionListener(symActionListener); add(button_add); -JButton button_00=new JButton("00"); -button_00.setBounds(5,285,70,50); -button_00.setBorder(new LineBorder(Color.BLACK,1)); -button_00.setFont(new Font("Cambria",Font.PLAIN,23)); -add(button_00); +JButton button_cha=new JButton("+/-"); +button_cha.setBounds(5,345,70,50); +button_cha.setBorder(new LineBorder(Color.BLACK,1)); +button_cha.setFont(new Font("Cambria",Font.PLAIN,23)); +button_cha.addActionListener(symActionListener); +add(button_cha); JButton button_0=new JButton("0"); -button_0.setBounds(80,285,70,50); +button_0.setBounds(80,345,70,50); button_0.setBorder(new LineBorder(Color.BLACK,1)); button_0.setFont(new Font("Cambria",Font.PLAIN,23)); +button_0.addActionListener(numActionListener); add(button_0); -JButton button_point=new JButton("."); -button_point.setBounds(155,285,70,50); +JButton button_point=new JButton(".");//小数点 +button_point.setBounds(155,345,70,50); button_point.setBorder(new LineBorder(Color.BLACK,1)); button_point.setFont(new Font("Cambria",Font.PLAIN,23)); +button_point.addActionListener(e->{ + if(isResult&&!isNotNullAndEmpty(logic)) {//如果上一个操作是刚出的结果,则将数字替换 + isResult=false; + s1=""; + } + if(!isNotNullAndEmpty(logic)) {//看是否存在运算符 + if(s1==null||s1.length()==0) { + s1="0"; + }else{ + s1 +=s1.indexOf(".")==-1?".":""; + } + } else { + if(s1==null||s2.length()==0) { + s2="0."; + }else { + s2 +=s2.indexOf(".")==-1?".":""; /*214-228行代码来自百度文库*/ + }} +result=(isNotNullAndEmpty(s1)?s1:"")+(isNotNullAndEmpty(logic)?logic:"")+(isNotNullAndEmpty(s2)?s2:""); +showView.setText(result); +}); add(button_point); -JButton button_eq=new JButton("="); -button_eq.setBounds(230,285,70,50); +JButton button_eq=new JButton("=");//等于符号 +button_eq.setBounds(230,345,70,50); button_eq.setBorder(new LineBorder(Color.BLACK,1)); button_eq.setFont(new Font("Cambria",Font.PLAIN,23)); +button_eq.addActionListener(new EqActionListener()); add(button_eq); - - } +public boolean isNotNullAndEmpty(String str) {//判断字符串为非空 /*该方法来自百度文库*/ + return str!=null&&!"".equals(str.trim()); +} +class NumActionListener implements ActionListener{//数字监听器 + public void actionPerformed(ActionEvent e) { + if(isResult&&!isNotNullAndEmpty(logic)) {//如果上一个操作是刚出结果,点数字将会把数字替换 + isResult=false; + s1=""; + } + JButton btn=(JButton)e.getSource();//获取按钮内容 + String btn_text=btn.getText(); + if(isNotNullAndEmpty(logic)) { + s2=new BigDecimal(s2+btn_text).toString();//拼接字符串。先传BigDecimal,用户可能输入0开头 + }else { + s1=new BigDecimal(s1+btn_text).toString(); + } /*254-258行代码来自百度*/ + result=(isNotNullAndEmpty(s1)?s1:"")+(isNotNullAndEmpty(logic)?logic:"")+(isNotNullAndEmpty(s2)?s2:""); + showView.setText(result); + } + } - -public void addView() {//结果显示屏 - showView=new JTextField("0"); - showView.setBounds(5,3,295,60); - showView.setHorizontalAlignment(JTextField.RIGHT); - showView.setEditable(false); - showView.setBackground(Color.WHITE); - showView.setFont(new Font("Cambria",Font.BOLD,25)); - add(showView); -}} \ No newline at end of file +class CleanActionListener implements ActionListener{//清空按钮的监听器 + public void actionPerformed(ActionEvent e) { + result=""; + s1=""; + s2=""; + logic=""; + showView.setText(result);//清空显示屏内容 + } +} +class SymActionListener implements ActionListener{//符号监听器 + public void actionPerformed(ActionEvent e) { + new EqActionListener().actionPerformed(null); + JButton btn=(JButton)e.getSource();//得到符号的内容 + String logicstr=btn.getText(); + logic=logicstr; + result=s1+logic; + showView.setText(result); + } +} +class EqActionListener implements ActionListener{//等于符号按钮的监听器 + public void actionPerformed(ActionEvent e) { + if(isNotNullAndEmpty(s1)&&isNotNullAndEmpty(s2)&&isNotNullAndEmpty(logic)) { + Double number1=new Double(s1);//将两个字符串转换为double型数字 + Double number2=new Double(s2); + switch(logic) { + case"+": + result=(number1+number2)+"";break; + case"-": + result=(number1-number2)+"";break; + case"*": + result=(number1*number2)+"";break; + case"/": + result=(new Double(number1)/new Double(number2))+"";break; + case"%": + result=(new Double(number1)%new Double(number2))+"";break; + case"^": + result=Math.pow(number1,number2)+"";break; + case"log": + result=Math.log(number1)/Math.log(number2)+"";break; + } + showView.setText(result); + s2="";//清空 + logic=""; + s1=result; + isResult=true; + } + } +} + } \ No newline at end of file -- Gitee From df1c2831f26aba99cd57ac75655571d47600bd88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E5=98=89=E9=92=B0?= <鑼冨槈閽癅LAPTOP-RJ8KNJN6> Date: Wed, 8 Jun 2022 11:42:27 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E9=99=A4?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E5=AF=B9=E6=95=B0=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E6=8C=87=E6=95=B0=E5=87=BD=E6=95=B0=E7=9A=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Test.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index c213f95..e245cd0 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -168,7 +168,7 @@ JButton button_1=new JButton("1"); button_1.setBounds(5,290,70,50); button_1.setBorder(new LineBorder(Color.BLACK,1)); button_1.setFont(new Font("Cambria",Font.PLAIN,23)); -button_1.addActionListener(symActionListener); +button_1.addActionListener(numActionListener); add(button_1); JButton button_2=new JButton("2"); @@ -293,12 +293,24 @@ class EqActionListener implements ActionListener{// case"*": result=(number1*number2)+"";break; case"/": - result=(new Double(number1)/new Double(number2))+"";break; + if(number2==0) { + result="error"; break;//除数如果是0,则提示用户出错 + } + result=(new Double(number1)/new Double(number2))+"";break; case"%": + if(number2==0){ + result="error";break;//除数如果是0,则提示用户出错 + } result=(new Double(number1)%new Double(number2))+"";break; case"^": + if(number1<=0||number1==1){//指数函数的底数要大于0且不等于1 + result="error";break; + } result=Math.pow(number1,number2)+"";break; case"log": + if(number2<=0||number2==1||number1<=0) {//对数底数要大于0且不能等于1,真数要大于0 + result="error";break; + } result=Math.log(number1)/Math.log(number2)+"";break; } showView.setText(result); -- Gitee From 2e189b776763800fe4ce180172146dd71dca6821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E5=98=89=E9=92=B0?= <鑼冨槈閽癅LAPTOP-RJ8KNJN6> Date: Wed, 8 Jun 2022 16:55:08 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=99=A8=E7=9A=84=E8=8F=9C=E5=8D=95=E6=A0=8F=EF=BC=8C?= =?UTF-8?q?=E5=8C=85=E5=90=AB=E7=BC=96=E8=BE=91=E3=80=81=E6=9F=A5=E7=9C=8B?= =?UTF-8?q?=E3=80=81=E5=B8=AE=E5=8A=A9=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Test.java | 66 ++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index e245cd0..028e70e 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -4,10 +4,8 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.math.*; -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JTextField; import javax.swing.border.LineBorder; +import javax.swing.*; public class Test extends JFrame{ JTextField showView=null;//结果显示屏 @@ -19,22 +17,68 @@ String s2="";// String result=""; boolean isResult=false; +JMenuBar calBar; +JMenu m1,m2,m3; +JMenuItem e1,e2,e3,e4,h1,h2,h3; +JRadioButtonMenuItem c1,c2,c3,c4,c5,c6; +JCheckBoxMenuItem c; +ButtonGroup group; public static void main(String[] args) { new Test(); } public Test() //设置计算器尺寸和位置等 { - setTitle("测试计算器"); - setBounds(300,150,317,435); + setTitle("计算器"); + setBounds(300,150,317,460); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setLayout(null); addView(); addButton(); + + calBar=new JMenuBar();//三个菜单栏 + m1=new JMenu("编辑(E)"); + m2=new JMenu("查看(V)"); + m3=new JMenu("帮助(H)"); + m1.setFont(new Font("黑体",Font.PLAIN,13)); + m2.setFont(new Font("黑体",Font.PLAIN,13)); + m3.setFont(new Font("黑体",Font.PLAIN,13)); + e1=new JMenuItem("撤销(Z) Ctrl+Z");//编辑选项 + e2=new JMenuItem("复制(C) Ctrl+C"); + e3=new JMenuItem("粘贴(C) Ctrl+V"); + e4=new JMenuItem("删除(D) Delete"); + c1=new JRadioButtonMenuItem("科学型(T)");//查看选项 + c2=new JRadioButtonMenuItem("标准型(S)"); + c3=new JRadioButtonMenuItem("十六进制(H)"); + c4=new JRadioButtonMenuItem("十进制(D)"); + c5=new JRadioButtonMenuItem("八进制(0)"); + c6=new JRadioButtonMenuItem("二进制(B)"); + c=new JCheckBoxMenuItem("数字分组"); + h1=new JMenuItem("查看帮助(H)");//帮助选项 + h2=new JMenuItem("发送报告(F)"); + h3=new JMenuItem("关于计算器(A)") ; + group=new ButtonGroup(); + m1.add(e1); + m1.add(e2); + m1.add(e3); + m1.add(e4); + m2.add(c1); + m2.add(c2); + m2.add(c3); + m2.add(c4); + m2.add(c5); + m2.add(c); + m3.add(h1); + m3.add(h2); + m3.add(h3); + calBar.add(m1); + calBar.add(m2); + calBar.add(m3); + this.setJMenuBar(calBar); setVisible(true); } - + public void addView() {//结果显示屏 showView=new JTextField("0"); showView.setBounds(5,5,296,60); @@ -72,7 +116,7 @@ button_back.addActionListener(e->{ // if(s1==null||s2.length()==0) {//存在运算符,删除运算符 logic=""; }else { - s2=s2.substring(0,s2.length()-1); /*64-77行代码来自百度文库*/ + s2=s2.substring(0,s2.length()-1); /*该方法代码来自百度文库*/ }} result=(isNotNullAndEmpty(s1)?s1:"")+(isNotNullAndEmpty(logic)?logic:"")+(isNotNullAndEmpty(s2)?s2:""); showView.setText(result); @@ -210,7 +254,7 @@ JButton button_point=new JButton(".");//小 button_point.setBounds(155,345,70,50); button_point.setBorder(new LineBorder(Color.BLACK,1)); button_point.setFont(new Font("Cambria",Font.PLAIN,23)); -button_point.addActionListener(e->{ +button_point.addActionListener(e->{ if(isResult&&!isNotNullAndEmpty(logic)) {//如果上一个操作是刚出的结果,则将数字替换 isResult=false; s1=""; @@ -225,7 +269,7 @@ button_point.addActionListener(e->{ if(s1==null||s2.length()==0) { s2="0."; }else { - s2 +=s2.indexOf(".")==-1?".":""; /*214-228行代码来自百度文库*/ + s2 +=s2.indexOf(".")==-1?".":""; /*该方法代码来自百度文库*/ }} result=(isNotNullAndEmpty(s1)?s1:"")+(isNotNullAndEmpty(logic)?logic:"")+(isNotNullAndEmpty(s2)?s2:""); showView.setText(result); @@ -255,7 +299,7 @@ class NumActionListener implements ActionListener{// s2=new BigDecimal(s2+btn_text).toString();//拼接字符串。先传BigDecimal,用户可能输入0开头 }else { s1=new BigDecimal(s1+btn_text).toString(); - } /*254-258行代码来自百度*/ + } /*该方法代码代码来自百度*/ result=(isNotNullAndEmpty(s1)?s1:"")+(isNotNullAndEmpty(logic)?logic:"")+(isNotNullAndEmpty(s2)?s2:""); showView.setText(result); } @@ -321,4 +365,4 @@ class EqActionListener implements ActionListener{// } } } - } \ No newline at end of file +} \ No newline at end of file -- Gitee From 63f672fc82f94ad8bc00d60ba1f28cbe690aaaae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E5=98=89=E9=92=B0?= <鑼冨槈閽癅LAPTOP-RJ8KNJN6> Date: Wed, 8 Jun 2022 17:31:02 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=87=A0?= =?UTF-8?q?=E6=9D=A1=E5=88=86=E5=89=B2=E7=BA=BF=EF=BC=8C=E5=B9=B6=E4=B8=94?= =?UTF-8?q?=E5=B0=86=E9=80=89=E9=A1=B9=E5=BD=92=E7=BB=84=EF=BC=8C=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=8D=95=E9=80=89=E6=A1=86=E5=92=8C=E5=A4=8D=E9=80=89?= =?UTF-8?q?=E6=A1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Test.java | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 028e70e..5135ea4 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -20,9 +20,9 @@ boolean isResult=false; JMenuBar calBar; JMenu m1,m2,m3; JMenuItem e1,e2,e3,e4,h1,h2,h3; -JRadioButtonMenuItem c1,c2,c3,c4,c5,c6; -JCheckBoxMenuItem c; -ButtonGroup group; +JRadioButtonMenuItem c1,c2,c3,c4,c5,c6;//单选按钮 +JCheckBoxMenuItem c;//复选按钮 +ButtonGroup group1,group2; public static void main(String[] args) { new Test(); } @@ -58,20 +58,31 @@ public Test() // h1=new JMenuItem("查看帮助(H)");//帮助选项 h2=new JMenuItem("发送报告(F)"); h3=new JMenuItem("关于计算器(A)") ; - group=new ButtonGroup(); + group1=new ButtonGroup();//类型选项归组 + group2=new ButtonGroup();//进制选项归组 m1.add(e1); m1.add(e2); m1.add(e3); m1.add(e4); m2.add(c1); m2.add(c2); + m2.addSeparator();//添加分割线 /*该方法来自csdn*/ m2.add(c3); m2.add(c4); m2.add(c5); + m2.add(c6); + m2.addSeparator(); m2.add(c); m3.add(h1); m3.add(h2); + m3.addSeparator(); m3.add(h3); + group1.add(c1); + group1.add(c2); + group2.add(c3); + group2.add(c4); + group2.add(c5); + group2.add(c6); calBar.add(m1); calBar.add(m2); calBar.add(m3); @@ -116,7 +127,7 @@ button_back.addActionListener(e->{ // if(s1==null||s2.length()==0) {//存在运算符,删除运算符 logic=""; }else { - s2=s2.substring(0,s2.length()-1); /*该方法代码来自百度文库*/ + s2=s2.substring(0,s2.length()-1); /*该方法代码来自百度文库*/ }} result=(isNotNullAndEmpty(s1)?s1:"")+(isNotNullAndEmpty(logic)?logic:"")+(isNotNullAndEmpty(s2)?s2:""); showView.setText(result); @@ -269,7 +280,7 @@ button_point.addActionListener(e->{ if(s1==null||s2.length()==0) { s2="0."; }else { - s2 +=s2.indexOf(".")==-1?".":""; /*该方法代码来自百度文库*/ + s2 +=s2.indexOf(".")==-1?".":""; /*该方法代码来自百度文库*/ }} result=(isNotNullAndEmpty(s1)?s1:"")+(isNotNullAndEmpty(logic)?logic:"")+(isNotNullAndEmpty(s2)?s2:""); showView.setText(result); @@ -284,7 +295,7 @@ button_eq.addActionListener(new EqActionListener()); add(button_eq); } -public boolean isNotNullAndEmpty(String str) {//判断字符串为非空 /*该方法来自百度文库*/ +public boolean isNotNullAndEmpty(String str) {//判断字符串为非空 /*该方法来自百度文库*/ return str!=null&&!"".equals(str.trim()); } class NumActionListener implements ActionListener{//数字监听器 @@ -299,7 +310,7 @@ class NumActionListener implements ActionListener{// s2=new BigDecimal(s2+btn_text).toString();//拼接字符串。先传BigDecimal,用户可能输入0开头 }else { s1=new BigDecimal(s1+btn_text).toString(); - } /*该方法代码代码来自百度*/ + } /*该方法代码来自百度*/ result=(isNotNullAndEmpty(s1)?s1:"")+(isNotNullAndEmpty(logic)?logic:"")+(isNotNullAndEmpty(s2)?s2:""); showView.setText(result); } -- Gitee