diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000000000000000000000000000000000..0b5440269e1ddf9ceed9a17e7a6ee9bf8bae27a8 --- /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 new file mode 100644 index 0000000000000000000000000000000000000000..11327db5bee4429607082437c906131960bfe1ae --- /dev/null +++ b/src/start/Calculator.java @@ -0,0 +1,174 @@ +package start; + +import javax.swing.*; +import java.awt.*; +import javax.swing.border.*; +import java.awt.event.*; +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.setLayout(new BorderLayout()); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + + 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窗体北面 + + } + + public void addCenterComponent() { + centerP.setBackground(new Color(32,32,33)); + JButton btn[][] = new JButton[6][4]; + 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)); + 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); + } + + 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.equals("00")) { + 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("<-")) {//删除键 + 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("C")) {//清空 + inputtext.setText(""); + } + } + + public static void main(String[] args) { + // TODO 自动生成的方法存根 + Calculator Calculator = new Calculator(); + Calculator.setVisible(true); + } +}