diff --git a/src/SuperCalculator/.idea/.gitignore b/src/SuperCalculator/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..26d33521af10bcc7fd8cea344038eaaeb78d0ef5 --- /dev/null +++ b/src/SuperCalculator/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/src/SuperCalculator/.idea/.name b/src/SuperCalculator/.idea/.name new file mode 100644 index 0000000000000000000000000000000000000000..1efc76344c5bab64d1c2b6b73f1d2553e6c4435d --- /dev/null +++ b/src/SuperCalculator/.idea/.name @@ -0,0 +1 @@ +Calculator.java \ No newline at end of file diff --git a/src/SuperCalculator/.idea/misc.xml b/src/SuperCalculator/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..05483570e041eb648703359441d61bf9a5feeb13 --- /dev/null +++ b/src/SuperCalculator/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/SuperCalculator/.idea/modules.xml b/src/SuperCalculator/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..94935151ea4cc7452d6ebfd5385809aa99d4c65e --- /dev/null +++ b/src/SuperCalculator/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/SuperCalculator/SuperCalculator.iml b/src/SuperCalculator/SuperCalculator.iml new file mode 100644 index 0000000000000000000000000000000000000000..c90834f2d607afe55e6104d8aa2cdfffb713f688 --- /dev/null +++ b/src/SuperCalculator/SuperCalculator.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/SuperCalculator/out/production/SuperCalculator/Calculator$1.class b/src/SuperCalculator/out/production/SuperCalculator/Calculator$1.class new file mode 100644 index 0000000000000000000000000000000000000000..d0431eb8ab9e4189bcb73666668f31164143f234 Binary files /dev/null and b/src/SuperCalculator/out/production/SuperCalculator/Calculator$1.class differ diff --git a/src/SuperCalculator/out/production/SuperCalculator/Calculator.class b/src/SuperCalculator/out/production/SuperCalculator/Calculator.class new file mode 100644 index 0000000000000000000000000000000000000000..a021153774f56886696ea8706f4726221aa44e65 Binary files /dev/null and b/src/SuperCalculator/out/production/SuperCalculator/Calculator.class differ diff --git a/src/SuperCalculator/src/Calculator.java b/src/SuperCalculator/src/Calculator.java new file mode 100644 index 0000000000000000000000000000000000000000..9b3f718730c73826824cdf7bc8b477c53ef3d07a --- /dev/null +++ b/src/SuperCalculator/src/Calculator.java @@ -0,0 +1,144 @@ + + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class Calculator extends JFrame implements ActionListener { + //北面控件 + private JPanel jp_north = new JPanel();//声明一个面板 + private JTextField input_text = new JTextField(); //输入框 + private JButton c_Btn = new JButton("清空");//清空按钮 + //中间控件 + private JPanel jp_center = new JPanel(); //声明一个面板 + + //主面板 + public Calculator() throws HeadlessException { + this.init();//调用方法init + this.addNorthComponent();//加载调用北面的控件 + this.addCenterButton();//加载调用中间的控件 + + } + + public void init() { + this.setTitle("真的一点也不会做的计算器 "); //标题 + this.setSize(800, 800);//大小 + this.setLayout(new BorderLayout());//边框布局 + this.setResizable(true);//窗体可以拉伸 + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口的时候,退出程序 + input_text.setHorizontalAlignment(SwingConstants.CENTER);//使文本框上的文字居中 + input_text.setBackground(Color.yellow);//设置文本框背景颜色为黄色 + input_text.setFont(new Font("微软雅黑", Font.PLAIN, 36));//设置字体颜色和大小,font.plain字体普通 + } + + public void addNorthComponent() { + this.input_text.setPreferredSize(new Dimension(400,50)); + jp_north.add(input_text); + + this.c_Btn.setForeground(Color.blue); + jp_north.add(c_Btn); + + c_Btn.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + input_text.setText(""); + } + }); + + + this.add(jp_north,BorderLayout.NORTH); //控件放到窗体北面 + } + + public void addCenterButton() { + String bth_text = "123+456-789*.0=/"; + this.jp_center.setLayout(new GridLayout(4,4));//声明一个4x4的网格 + for(int i=0;i<16;i++) { + String temp = bth_text.substring(i,i+1); + JButton btn = new JButton(); + btn.setText(temp); + + if(temp.equals("+")||temp.equals("-")||temp.equals("*")||temp.equals("/")||temp.equals(".")||temp.equals("=")) + { + btn.setFont(new Font("微软雅黑",Font.BOLD,70)); + btn.setForeground(Color.darkGray); + btn.setBackground(Color.pink); + } + if(temp.equals("1")||temp.equals("2")||temp.equals("3")||temp.equals("4")||temp.equals("5")||temp.equals("6")||temp.equals("7")||temp.equals("8")||temp.equals("9")||temp.equals("0")) + { + btn.setFont(new Font("微软雅黑",Font.BOLD,70)); + btn.setForeground(Color.gray); + btn.setBackground(Color.pink); + } + + btn.addActionListener(this); + jp_center.add(btn); + } + this.add(jp_center,BorderLayout.CENTER); + } + + + public static void main(String[] args) { + Calculator calculator = new Calculator(); + calculator.setVisible(true); //显示GUI组件 + } + + private String firstInput = null;//记录第一次输入的数 + private String symbol = null;//记录操作符 + @Override + public void actionPerformed(ActionEvent e) { + // TODO Auto-generated method stub + String click = e.getActionCommand();//获取点击到的值 + if(".0123456789".indexOf(click)!=-1) { + this.input_text.setText(input_text.getText()+click);//把按钮上的值赋值到文本框里 + this.input_text.setHorizontalAlignment(JTextField.RIGHT);//数字从右边往左显示 + }else if(click.matches("[\\+\\-*/]{1}")) { + symbol = click;//记录符号 + firstInput = this.input_text.getText();//记录数字 + this.input_text.setText("");//清空文本框内容 + }else if(click.equals("=")) { + Double a = Double.valueOf(firstInput);//点运算符之前的值 + Double b = Double.valueOf(this.input_text.getText());//点运算符之后的值,即当前值 + Double result = null; + switch (symbol) { + case"+":result=a+b;break; + case"-":result=a-b;break; + case"*":result=a*b;break; + case"/": + if(b!=0) { + result=a/b; + }break; + } + this.input_text.setText(result.toString()); + } + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java deleted file mode 100644 index 24deb29bfc0e5f50fdedcd21b504e77b529d48b6..0000000000000000000000000000000000000000 --- 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!"); - } -}