From 4185703bada608ef69ee2600851b381325db2567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=88=E6=B3=BD=E5=AE=87?= <2737577908@qq.com> Date: Wed, 9 Oct 2024 11:07:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ src/Computer.java | 157 ++++++++++++++++++++++++++++++++++++++++++++++ src/Main.java | 15 +++++ 5 files changed, 194 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 src/Computer.java create mode 100644 src/Main.java 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/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /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..5430172 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/Computer.java b/src/Computer.java new file mode 100644 index 0000000..bc163df --- /dev/null +++ b/src/Computer.java @@ -0,0 +1,157 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +public class Computer extends JFrame{ + private static final long serialVersionUID = 1L; + JPanel xs, aj; + JTextField xsArea; + JButton[] aJButtons = new JButton[16]; + String a; + int flag = 0; + public static void main(String[] args) { + Computer t = new Computer(); + } + + public Computer() { + this.setTitle("计算器"); + Container c = getContentPane(); + c.setLayout(new BorderLayout()); + xs = new JPanel(new FlowLayout()); + xsArea = new JTextField(20); + xsArea.setFont(new Font("宋体", Font.PLAIN, 38)); + xs.add(xsArea); + c.add(xs, BorderLayout.NORTH); + aj = new JPanel(new GridLayout(4,4)); + aJButtons[0]=new JButton("0"); + aJButtons[1]=new JButton("1"); + aJButtons[2]=new JButton("2"); + aJButtons[3]=new JButton("3"); + aJButtons[4]=new JButton("4"); + aJButtons[5]=new JButton("5"); + aJButtons[6]=new JButton("6"); + aJButtons[7]=new JButton("7"); + aJButtons[8]=new JButton("8"); + aJButtons[9]=new JButton("9"); + aJButtons[10]=new JButton("+"); + aJButtons[11]=new JButton("-"); + aJButtons[12]=new JButton("*"); + aJButtons[13]=new JButton("/"); + aJButtons[14]=new JButton("="); + aJButtons[15]=new JButton("."); + + aj.add(aJButtons[7]); + aj.add(aJButtons[8]); + aj.add(aJButtons[9]); + aj.add(aJButtons[13]); + aj.add(aJButtons[4]); + aj.add(aJButtons[5]); + aj.add(aJButtons[6]); + aj.add(aJButtons[12]); + aj.add(aJButtons[1]); + aj.add(aJButtons[2]); + aj.add(aJButtons[3]); + aj.add(aJButtons[11]); + aj.add(aJButtons[0]); + aj.add(aJButtons[15]); + aj.add(aJButtons[10]); + aj.add(aJButtons[14]); + c.add(aj, BorderLayout.CENTER); + setSize(400, 400); + setVisible(true); + + for(int i = 0; i < 10; i++) { + final String o = aJButtons[i].getActionCommand(); + aJButtons[i].addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + a = xsArea.getText(); + a = a + o; + xsArea.setText(a); + } + }); + + } + + aJButtons[10].addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + a = xsArea.getText(); + a = a + "+"; + flag = 1;//加法 + xsArea.setText(a); + } + }); + + aJButtons[11].addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + a = xsArea.getText(); + a = a + "-"; + flag = 2;//减法 + xsArea.setText(a); + } + }); + + aJButtons[12].addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + a=xsArea.getText(); + a=a+"*"; + flag=3;//乘法 + xsArea.setText(a); + } + }); + + aJButtons[13].addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + a=xsArea.getText(); + a=a+"/"; + flag=4;//除法 + xsArea.setText(a); + } + }); + + aJButtons[14].addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + a = xsArea.getText(); + String[] sz = new String[2]; +//分割字符串:+ 或 - 或 * 或 / + sz=a.split("\\+|\\-|\\*|\\/");//把分割的两个部分放进sz中 + double b = 0; + if(flag == 0) { +//输出原数 + b = Double.parseDouble(a); + } + + else if(flag == 1) { +//加法 + b = Double.parseDouble(sz[0]) + Double.parseDouble(sz[1]); + } + + else if(flag == 2) { +//减法 + b = Double.parseDouble(sz[0]) - Double.parseDouble(sz[1]); + } + + else if(flag == 3) { +//乘法 + b = Double.parseDouble(sz[0]) * Double.parseDouble(sz[1]); + } + + else if(flag == 4) { +//除法 + b = Double.parseDouble(sz[0]) / Double.parseDouble(sz[1]); + } + + a = ""+b; + xsArea.setText(a); + } + }); + + aJButtons[15].addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + a=xsArea.getText(); + a=a+"."; + xsArea.setText(a); + } + }); + + } + +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..fe7aa2b --- /dev/null +++ b/src/Main.java @@ -0,0 +1,15 @@ +//TIP 要运行代码,请按 或 +// 点击装订区域中的 图标。 +public class Main { + public static void main(String[] args) { + //TIP 当文本光标位于高亮显示的文本处时按 + // 查看 IntelliJ IDEA 建议如何修正。 + System.out.printf("Hello and welcome!"); + + for (int i = 1; i <= 5; i++) { + //TIP 按 开始调试代码。我们已经设置了一个 断点 + // 但您始终可以通过按 添加更多断点。 + System.out.println("i = " + i); + } + } +} \ No newline at end of file