diff --git a/src/java2020spring/Caculator b/src/java2020spring/Caculator new file mode 100644 index 0000000000000000000000000000000000000000..a7ed5806159c638f346a88ab50f1178f39184232 --- /dev/null +++ b/src/java2020spring/Caculator @@ -0,0 +1,221 @@ +package java2020spring; + +import java.awt.color.*; +import java.awt.*; +import javax.swing.*; +import java.awt.event.*; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Set; +public class Caculator extends JFrame{ + //标签 + JLabel show= new JLabel();//数值框框 + JPanel main= new JPanel();//主面板 + JPanel shuziPanel= new JPanel();//数字面板 + boolean start;//判断是否重新开始 + int count;//小数点计数 + double result;//接受结果 + String fuhao;//接受符号 + public static void main(String[] args) { + new Caculator(); + + } + public Caculator() { + init(); + } + +//布局 + private void init() { + //窗体大小 + this.setSize(350,200); + //显示窗体 + this.setVisible(true); + //窗体居中 + this.setLocationRelativeTo(null); + //设置点击退出则关闭程序 + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + //设置标题 + this.setTitle("Caculator"); + //显示框默认值为0 + show.setText("0"); + //默认开始 + start=true; + //默认小数点次数为0 + count=0; + //结果默认为0 + result=0; + //默认fuhao="=" + fuhao="="; + + //按钮设置布局 + shuziPanel.setLayout(new GridLayout(7,4)); + //准备监听对象 + Action1 action1=new Action1();//数字按钮 + Action2 action2=new Action2();//fuhao按钮 + Action3 action3=new Action3();//功能按钮 + //添加按钮 + this.addButton("TIME",action3); + this.addButton("Mess",action3); + this.addButton("Backspace",action3); + this.addButton("C",action3); + this.addButton("7",action1); + this.addButton("8",action1); + this.addButton("9",action1); + this.addButton("+",action2); + this.addButton("4",action1); + this.addButton("5",action1); + this.addButton("6",action1); + this.addButton("-",action2); + this.addButton("1",action1); + this.addButton("2",action1); + this.addButton("3",action1); + this.addButton("*",action2); + this.addButton(".",action1); + this.addButton("0",action1); + this.addButton("=",action2); + this.addButton("/",action2); + this.addButton("%",action2); + this.addButton("sqrt",action2); + this.addButton("M+",action3); + this.addButton("1/x",action2); + this.addButton("Square",action2); + this.addButton("Cube",action2); + this.addButton("CubeRoot",action2); + this.addButton("|X|",action2); + //主面板添加数字按键 + main.setLayout(new BorderLayout());; + main.add(show,"North");//数字面板显示在北方 + main.add(shuziPanel);//添加数字面板 + + //添加窗体 + this.add(main); + } + public void addButton(String shuzi,ActionListener a1 ) { + JButton button=new JButton(shuzi); + button.addActionListener(a1); + shuziPanel.add(button); + + } + + class Action1 implements ActionListener{ + + public void actionPerformed(ActionEvent e) { + String input=e.getActionCommand();//显示按钮的数字 + if(show.getText().equals("0")) { + start=true;} + if(start) { + count=0; + show.setText(""); + start=false; + } + if(input.equals(".")) { + if(count==0) { + show.setText("0"+show.getText()+input);//显示数值 + } + else { + show.setText(show.getText()+"");//显示数值 + } + count++; + } + if(!input.equals(".")) { + show.setText(show.getText()+input); + } + } + + + + } + //fuhao监听方法 + class Action2 implements ActionListener{ + public void actionPerformed(ActionEvent e) { + String input=e.getActionCommand(); + if(start) { + fuhao=input;}//开始新计算,接受符号 + else { + if(fuhao.equals("+")) { + result +=Double.parseDouble(show.getText()); + } + + else if(fuhao.equals("CubeRoot")) { + result=Math.pow(Double.parseDouble(show.getText()), 1.0/3); + } + else if(fuhao.equals("|X|")) { + if(Double.parseDouble(show.getText())<0) { + result=-Double.parseDouble(show.getText()); + } + else { + result=Double.parseDouble(show.getText()); + } + } + else if(fuhao.equals("Square")) { + result =Double.parseDouble(show.getText())*Double.parseDouble(show.getText()); + }else if(fuhao.equals("Cube")){ + result =Double.parseDouble(show.getText())*Double.parseDouble(show.getText())*Double.parseDouble(show.getText()); + } + else if(fuhao.equals("-")) { + result -=Double.parseDouble(show.getText()); + }else if(fuhao.equals("*")) { + result *=Double.parseDouble(show.getText()); + }else if(fuhao.equals("/")) { + result /=Double.parseDouble(show.getText()); + }else if(fuhao.equals("%")) { + result =Double.parseDouble(show.getText())/100; + }else if(fuhao.equals("sqrt")) { + result =Math.sqrt(Double.parseDouble(show.getText())); + + } + else if(input.equals("1/x")) { + if(Double.parseDouble(show.getText())==0) { + show.setText("0无倒数"); + } + else { + result=1/Double.parseDouble(show.getText()); + + } + } + else { + result =Double.parseDouble(show.getText()); + } + show.setText(result+""); + fuhao=input; + start=true; + + } + } + } + class Action3 implements ActionListener{ + public void actionPerformed(ActionEvent e) { + String input=e.getActionCommand(); + if(input.equals("TIME")) { + show.setText(new SimpleDateFormat("yyyy-MM-HH:mm:ss").format(new Date())); + start=true; + } + if(input.equals("C")) { + //清0 + show.setText("0");result=0; + } + if(input.equals("M+")) { + show.setText("暂无此功能"); + start=true; + } + + + + if(input.equals("Mess")) { + show.setText("20信管4周浩特"); + } + if(input.equals("Backspace")) { + //后退一步 + if(show.getText().length()>1) { + show.setText(show.getText().substring(0, show.getText().length()-1)); + }else { + show.setText("0"); + } + } + } + } + + + } + +