From 542dde1d9399a89a3ac99b80866cc25c49447662 Mon Sep 17 00:00:00 2001 From: 22795 <22795@LAPTOP-OBK8I4QI> Date: Sat, 28 May 2022 15:04:42 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E7=AA=97=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/start/Calculator.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/start/Calculator.java diff --git a/src/start/Calculator.java b/src/start/Calculator.java new file mode 100644 index 0000000..2e16d80 --- /dev/null +++ b/src/start/Calculator.java @@ -0,0 +1,5 @@ +package start; + +public class Calculator { + +} -- Gitee From 01939f8f5ce85933bf34d828a1b5dd3ad275e939 Mon Sep 17 00:00:00 2001 From: 22795 <22795@LAPTOP-OBK8I4QI> Date: Wed, 1 Jun 2022 16:05:05 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E7=9A=84=E5=91=88=E7=8E=B0=E6=95=88=E6=9E=9C=E5=92=8C?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=E7=9B=91=E8=A7=86=E5=99=A8=EF=BC=8C=E4=BD=86?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=BF=98=E6=B2=A1=E6=89=93=E5=AE=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .settings/org.eclipse.core.resources.prefs | 2 + src/start/Calculator.java | 91 +++++++++++++++++++++- 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 .settings/org.eclipse.core.resources.prefs diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..0b54402 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding//src/start/Calculator.java=UTF-8 diff --git a/src/start/Calculator.java b/src/start/Calculator.java index 2e16d80..1380952 100644 --- a/src/start/Calculator.java +++ b/src/start/Calculator.java @@ -1,5 +1,94 @@ package start; +import javax.swing.*; +import java.awt.*; +import javax.swing.border.*; +import java.awt.event.*; +public class Calculator extends JFrame implements ActionListener{ + /************北部控件*************/ + JPanel northP=new JPanel(); + JTextField inputtext=new JTextField(); + + /************中间控件*************/ + JPanel centerP=new JPanel(); + + public Calculator() throws HeadlessException{ + this.init(); + this.addNorthComponent(); + this.addCenterComponent(); + + } + + public void init() { + this.setTitle("Calculator"); + this.setSize(490,530); + this.setResizable(false);//设置窗体不可调整大小 + this.setLocationRelativeTo(null);//设置窗体初始位置居中 + this.setLayout(new BorderLayout()); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); +// LineBorder border = new LineBorder(Color.GREEN,6,true); (不知道哪个边框) + } + + public void addNorthComponent() { + inputtext.setPreferredSize(new Dimension(300,60)); + northP.add(inputtext); -public class Calculator { + this.add(northP,BorderLayout.NORTH);//把C控件放到Calculator窗体北面 + + } + + + public void addCenterComponent() { + JButton btn[][] = new JButton[6][4]; + String [][] btn_text= { + {"%","CE","C","<-"}, + {"1/x","^2","✓","÷"}, + {"7","8","9","×"}, + {"4","5","6","-"}, + {"1","2","3","+"}, + {"00","0",".","="} + }; + + this.centerP.setLayout(new GridLayout(6,6)); + + for(int i=0;i<6;i++) { + for(int j=0;j<4;j++) { + btn[i][j]= new JButton(btn_text[i][j]); + btn[i][j].setBackground(new Color(105,105,105)); + btn[i][j].setForeground(Color.white); + btn[i][j].setFont(new Font("",Font.BOLD,30)); + + btn[i][j].addActionListener(this); + centerP.add(btn[i][j]); + } + } + this.add(centerP,BorderLayout.CENTER); + } + + + private String firstInput = null; + private String operator = null; + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + String clickStr =e.getActionCommand(); + if(".0123456789%".indexOf(clickStr)!= -1) { + this.inputtext.setText(inputtext.getText() + clickStr); + this.inputtext.setHorizontalAlignment(JTextField.RIGHT); + } + else if(clickStr.matches("[\\+\\-×÷]{1}")) { + operator = clickStr; + firstInput = this.inputtext.getText(); + this.inputtext.setText(null); + } + else if(clickStr.equals("=")) { + + } + } + + public static void main(String[] args) { + // TODO 自动生成的方法存根 + Calculator Calculator=new Calculator(); + Calculator.setVisible(true); + } } -- Gitee From 56087187b725e449c25558bbb47d36a7b01c989a Mon Sep 17 00:00:00 2001 From: 22795 <22795@LAPTOP-OBK8I4QI> Date: Wed, 8 Jun 2022 16:10:03 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=89=A9=E4=B8=8B?= =?UTF-8?q?=E7=9A=84=E5=86=85=E5=AE=B9=EF=BC=8C=E5=8C=85=E6=8B=AC=E5=A4=96?= =?UTF-8?q?=E8=A7=82=E5=92=8C=E6=8C=89=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/start/Calculator.java | 180 +++++++++++++++++++++++++++----------- 1 file changed, 130 insertions(+), 50 deletions(-) diff --git a/src/start/Calculator.java b/src/start/Calculator.java index 1380952..11327db 100644 --- a/src/start/Calculator.java +++ b/src/start/Calculator.java @@ -1,94 +1,174 @@ package start; + import javax.swing.*; import java.awt.*; import javax.swing.border.*; import java.awt.event.*; -public class Calculator extends JFrame implements ActionListener{ - /************北部控件*************/ - JPanel northP=new JPanel(); - JTextField inputtext=new JTextField(); - - /************中间控件*************/ - JPanel centerP=new JPanel(); - - public Calculator() throws HeadlessException{ +import java.lang.*; + +public class Calculator extends JFrame implements ActionListener { + /************ 北部控件 *************/ + JPanel northP = new JPanel(); + JTextField inputtext = new JTextField(); + + /************ 中间控件 *************/ + JPanel centerP = new JPanel(); + + public Calculator() throws HeadlessException { this.init(); this.addNorthComponent(); this.addCenterComponent(); - + } - + public void init() { this.setTitle("Calculator"); - this.setSize(490,530); - this.setResizable(false);//设置窗体不可调整大小 - this.setLocationRelativeTo(null);//设置窗体初始位置居中 + this.setSize(490, 530); + this.setResizable(false);// 设置窗体不可调整大小 + this.setLocationRelativeTo(null);// 设置窗体初始位置居中 this.setLayout(new BorderLayout()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); -// LineBorder border = new LineBorder(Color.GREEN,6,true); (不知道哪个边框) } - - public void addNorthComponent() { - inputtext.setPreferredSize(new Dimension(300,60)); + + public void addNorthComponent() { + inputtext.setPreferredSize(new Dimension(470, 60)); + inputtext.setFont(new Font("Calibre", Font.PLAIN, 45)); + inputtext.setBackground(new Color(32,32,33)); + inputtext.setForeground(new Color(255, 255, 255)); + northP.setBackground(new Color(32,32,33)); + LineBorder border = new LineBorder(Color.LIGHT_GRAY, 2, true); + inputtext.setBorder(border); northP.add(inputtext); - this.add(northP,BorderLayout.NORTH);//把C控件放到Calculator窗体北面 - + this.add(northP, BorderLayout.NORTH);// 把C控件放到Calculator窗体北面 + } - - - public void addCenterComponent() { + + public void addCenterComponent() { + centerP.setBackground(new Color(32,32,33)); JButton btn[][] = new JButton[6][4]; - String [][] btn_text= { - {"%","CE","C","<-"}, - {"1/x","^2","✓","÷"}, - {"7","8","9","×"}, - {"4","5","6","-"}, - {"1","2","3","+"}, - {"00","0",".","="} - }; - - this.centerP.setLayout(new GridLayout(6,6)); - - for(int i=0;i<6;i++) { - for(int j=0;j<4;j++) { - btn[i][j]= new JButton(btn_text[i][j]); - btn[i][j].setBackground(new Color(105,105,105)); + String[][] btn_text = { { "%", "±", "C", "<-" }, { "1/x", "^2", "✓", "÷" }, { "7", "8", "9", "×" }, + { "4", "5", "6", "-" }, { "1", "2", "3", "+" }, { "00", "0", ".", "=" } }; + + this.centerP.setLayout(new GridLayout(6, 6)); + + for (int i = 0; i < 6; i++) { + for (int j = 0; j < 4; j++) { + btn[i][j] = new JButton(btn_text[i][j]); + btn[i][j].setBackground(new Color(70,70,70)); btn[i][j].setForeground(Color.white); - btn[i][j].setFont(new Font("",Font.BOLD,30)); - + btn[i][j].setFont(new Font("", Font.BOLD, 30)); + if (i < 2) { + btn[i][j].setBackground(new Color(52,49,49)); + } + if (j > 2) { + btn[i][j].setBackground(new Color(52,49,49)); + } + btn[i][j].addActionListener(this); centerP.add(btn[i][j]); } } - this.add(centerP,BorderLayout.CENTER); + + this.add(centerP, BorderLayout.CENTER); } - - + private String firstInput = null; private String operator = null; + @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 - String clickStr =e.getActionCommand(); - if(".0123456789%".indexOf(clickStr)!= -1) { + String clickStr = e.getActionCommand(); + if (".0123456789".indexOf(clickStr) != -1) {//数字输入 + this.inputtext.setText(inputtext.getText() + clickStr); + this.inputtext.setHorizontalAlignment(JTextField.RIGHT); + + } + else if(clickStr.equals("00")) { this.inputtext.setText(inputtext.getText() + clickStr); this.inputtext.setHorizontalAlignment(JTextField.RIGHT); } - else if(clickStr.matches("[\\+\\-×÷]{1}")) { + else if (clickStr.matches("[\\+\\-×÷±]{1}")) { operator = clickStr; firstInput = this.inputtext.getText(); this.inputtext.setText(null); + } + else if (clickStr.equals("<-")) {//删除键 + String str3 = inputtext.getText(); + str3 = str3.substring(0, str3.length() - 1); + inputtext.setText(str3); + } + else if (clickStr.equals("=")) {//等号键 + Double a = Double.valueOf(firstInput); + Double b = Double.valueOf(this.inputtext.getText()); + Double result = null; + switch (operator) { + case "+": + result = a + b; + break; + case "-": + result = a - b; + break; + case "×": + result = a * b; + break; + case "÷": + if(b !=0) { + result = a / b; + } + break; + case "±": + Double a1,a2; + a1 = a+b; + a2 = a-b; + this.inputtext.setText(a1.toString()+"&"+a2.toString()); + break; + } + this.inputtext.setText(result.toString()); + } + else if (clickStr.equals("1/x")) {//倒数运算 + String temp = inputtext.getText(); + String str4; + double x = Double.valueOf(temp); + if(x != 0) { + x = 1/x; + str4 = String.valueOf(x); + this.inputtext.setText(str4); + } + } + else if(clickStr.equals("^2")) {//平方运算 + String temp = inputtext.getText(); + String str5; + double x = Double.valueOf(temp); + x = x * x; + str5 = String.valueOf(x); + this.inputtext.setText(str5); + } + else if(clickStr.equals("✓")) {//开方运算 + String temp = inputtext.getText(); + String str6; + double x = Double.valueOf(temp); + x = Math.sqrt(x); + str6 = String.valueOf(x); + this.inputtext.setText(str6); + } + else if(clickStr.equals("%")) { + String temp = inputtext.getText(); + String str7; + double x = Double.valueOf(temp); + x = x / 100; + str7 = String.valueOf(x); + this.inputtext.setText(str7); } - else if(clickStr.equals("=")) { - + else if (clickStr.equals("C")) {//清空 + inputtext.setText(""); } } - public static void main(String[] args) { // TODO 自动生成的方法存根 - Calculator Calculator=new Calculator(); + Calculator Calculator = new Calculator(); Calculator.setVisible(true); } } -- Gitee