diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java new file mode 100644 index 0000000000000000000000000000000000000000..809b29bff2f1f8e6d20d92faa54877eff98140fd --- /dev/null +++ b/src/java2022spring/Calculator.java @@ -0,0 +1,141 @@ +package java2022spring; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Font; +import java.awt.GridLayout; +import java.awt.HeadlessException; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JTextField; + +public class Calculator extends JFrame implements ActionListener { + private JPanel jp_north=new JPanel(); + private JTextField input_text=new JTextField(20); + private JButton c_btn=new JButton("C"); + private JPanel jp_center=new JPanel(); + + public Calculator() throws HeadlessException { + this.init(); + this.addNorthComponent(); + this.addCenterButton(); + } + + +//初始化窗体 + public void init() { + this.setTitle("计算器"); + this.setSize(300, 400); + this.setLayout(new BorderLayout()); + this.setResizable(false); + this.setLocationRelativeTo(null); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + } + +//添加北面的控件 + public void addNorthComponent() { + jp_north.add(input_text); + this.c_btn.setForeground(new Color(225, 155, 0)); + jp_north.add(c_btn); + + c_btn.addActionListener(new ActionListener() { + + + @Override + public void actionPerformed(ActionEvent e) { + str1="0"; + str2="0"; + operator=null; + input_text.setText(""); + } + }); + + input_text.setEditable(false);//禁止键盘录入 + + this.add(jp_north,BorderLayout.NORTH); + + } + +//添加中间的按钮 + public void addCenterButton() { + String btn_text ="123+456-789*0.=/"; + String regex="[\\+\\-*/=]"; + this.jp_center.setLayout(new GridLayout(4,4)); + for(int i=0;i<16;i++) { + String temp=btn_text.substring(i,i+1); + JButton btn=new JButton(); + btn.setText(temp); + btn.setFont(new Font("华文细黑",Font.BOLD,21)); + btn.setBackground(Color.WHITE); + if(temp.matches(regex)) { + btn.setForeground(new Color(225, 155, 0)); + } + btn.addActionListener(this); + jp_center.add(btn); + } + + this.add(jp_center,BorderLayout.CENTER); + } + + + +//运算实现 + private String str1=null; + private String str2=null; + private String operator="!"; + @Override + public void actionPerformed(ActionEvent e) { + String getStr=e.getActionCommand(); + if(".0123456789".indexOf(getStr)!=-1) { + if (getStr.equals(".")) { + if(input_text.getText().length()>0) //第一位不能是"." + this.input_text.setText(input_text.getText()+getStr); + }else { + this.input_text.setText(input_text.getText()+getStr); + } + + + }else if(getStr.matches("[\\+\\-*/]{1}")) { + operator=getStr; + str1=this.input_text.getText(); + this.input_text.setText(""); + }else if(getStr.equals("=")) { + Double a=Double.valueOf(str1); + str2=this.input_text.getText(); + Double b=Double.valueOf(str2); + Double result = null; + switch(operator) { + case "+": + result=a+b; + break; + case "-": + result=a-b; + break; + case "*": + result=a*b; + break; + case "/": + if(b==0) { + this.input_text.setText("除数不能为零"); + } + else{ + result=a/b; + } + break; + } + this.input_text.setText(result.toString()); + +//一次运算后临时变量清零 + str1="0"; + str2="0"; + operator=null; + } + + } + +} \ No newline at end of file diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29bfc0e5f50fdedcd21b504e77b529d48b6..3316af1eb06a074705069a09d3ff45d593e3484e 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,7 +1,10 @@ package java2022spring; + public class Test { - public static void main(String[] args) { - System.out.println("Hello world!"); + public static void main (String [] args) { + Calculator c=new Calculator(); + c.setVisible(true); } + }