diff --git a/src/java2022spring/jisuanji.java b/src/java2022spring/jisuanji.java new file mode 100644 index 0000000000000000000000000000000000000000..65ec33b747b2b6425814921c9ad08cbe2d194c7a --- /dev/null +++ b/src/java2022spring/jisuanji.java @@ -0,0 +1,262 @@ +package java2022spring; +import java.awt.*; +import java.awt.event.*; +import javax.swing.JFrame; +import javax.swing.JTextField; +class jisuanji extends JFrame implements ActionListener { + Panel p,pt; + Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bcheng,bchu,bjian,bjia,bdian,bdeng,bbai,bCE,bC,bX,bdao,bfang,bgeng,bzhf; + JTextField t; + char flag; + double a,b; + String s; + MenuBar mb; + Menu mbj,mck; + MenuItem mifz,mizt,mcbz; + MenuShortcut msCopy,msPaste; + public jisuanji(){ + p=new Panel(); + pt=new Panel(); + mb=new MenuBar(); + mbj=new Menu("编辑"); + mck=new Menu("查看"); + mcbz=new Menu("标准型"); + this.setMenuBar(mb); + mb.add(mck); + mck.add(mcbz); + msCopy=new MenuShortcut('C'); + msPaste=new MenuShortcut('V'); + mifz=new MenuItem("复制",msCopy); + mizt=new MenuItem("粘贴",msPaste); + this.setMenuBar(mb); + mb.add(mbj); + mbj.add(mifz); + mbj.addSeparator(); + mbj.add(mizt); + b1=new Button("1"); + b2=new Button("2"); + b3=new Button("3"); + b4=new Button("4"); + b5=new Button("5"); + b6=new Button("6"); + b7=new Button("7"); + b8=new Button("8"); + b9=new Button("9"); + bcheng=new Button("*"); + bchu=new Button("/"); + bjia=new Button("+"); + bdeng=new Button("="); + bjian=new Button("-"); + b0=new Button("0"); + bdian=new Button("."); + bbai=new Button("%"); + bCE=new Button("CE"); + bC=new Button("C"); + bX=new Button("←"); + bdao=new Button("1/X"); + bfang=new Button("X^2"); + bgeng=new Button("X^1/2"); + bzhf=new Button("+/-"); + t=new JTextField(20); + t.setEditable(false); + t.setHorizontalAlignment(JTextField.RIGHT); + b1.addActionListener(this); + b2.addActionListener(this); + b3.addActionListener(this); + b4.addActionListener(this); + b5.addActionListener(this); + b6.addActionListener(this); + b7.addActionListener(this); + b8.addActionListener(this); + b9.addActionListener(this); + b0.addActionListener(this); + bcheng.addActionListener(this); + bchu.addActionListener(this); + bjia.addActionListener(this); + bjian.addActionListener(this); + bdian.addActionListener(this); + bdeng.addActionListener(this); + bbai.addActionListener(this); + bCE.addActionListener(this); + bC.addActionListener(this); + bX.addActionListener(this); + bdao.addActionListener(this); + bfang.addActionListener(this); + bgeng.addActionListener(this); + bzhf.addActionListener(this); + mifz.addActionListener(this); + mizt.addActionListener(this); + this.addWindowListener(new WindowAdapter() + {public void windowClosing(WindowEvent e) + {System.exit(0);} + }); + Font fn =new Font("黑体",Font.BOLD,25); + t.setFont(fn); + this.setSize(600,700); + int w=Toolkit.getDefaultToolkit().getScreenSize().width; + int h=Toolkit.getDefaultToolkit().getScreenSize().height; + this.setLocation((w-500)/2,(h-500)/2); + this.add(p); + this.add(pt,"North"); + p.setLayout(new GridLayout(6,4,5,5)); + pt.add(t); + t.setEnabled(false); + p.add(bbai); + p.add(bCE); + p.add(bC); + p.add(bX); + p.add(bdao); + p.add(bfang); + p.add(bgeng); + p.add(bchu); + p.add(b7); + p.add(b8); + p.add(b9); + p.add(bcheng); + p.add(b4); + p.add(b5); + p.add(b6); + p.add(bjian); + p.add(b1); + p.add(b2); + p.add(b3); + p.add(bjia); + p.add(bzhf); + p.add(b0); + p.add(bdian); + p.add(bdeng); + this.setSize(350,400); + this.setTitle("计算器"); + this.setVisible(true); + } + @Override + public void actionPerformed(ActionEvent e) { + // TODO 自动生成的方法存根 + if(e.getSource()==b1) { + t.setText(t.getText()+"1"); + } + if(e.getSource()==b2) { + t.setText(t.getText()+"2"); + } + if(e.getSource()==b3) { + t.setText(t.getText()+"3"); + } + if(e.getSource()==b4) { + t.setText(t.getText()+"4"); + } + if(e.getSource()==b5) { + t.setText(t.getText()+"5"); + } + if(e.getSource()==b6) { + t.setText(t.getText()+"6"); + } + if(e.getSource()==b7) { + t.setText(t.getText()+"7"); + } + if(e.getSource()==b8) { + t.setText(t.getText()+"8"); + } + if(e.getSource()==b9) { + t.setText(t.getText()+"9"); + } + if(e.getSource()==b0) { + t.setText(t.getText()+"0"); + } + if(e.getSource()==bcheng) { + a=Double.parseDouble(t.getText()); + flag='*'; + t.setText(""); + } + if(e.getSource()==bchu) { + a=Double.parseDouble(t.getText()); + flag='/'; + t.setText(""); + } + if(e.getSource()==bjia) { + a=Double.parseDouble(t.getText()); + flag='+'; + t.setText(""); + } + if(e.getSource()==bjian) { + a=Double.parseDouble(t.getText()); + flag='-'; + t.setText(""); + } + if(e.getSource()==bdian) { + t.setText(t.getText()+"."); + } + if(e.getSource()==bC) { + t.setText(" "); + } + if(e.getSource()==bbai) { + String result=" "; + a=Double.parseDouble(t.getText()); + b=a/100; + result=String.valueOf(b); + t.setText(result); + } + if(e.getSource()==bCE) { + t.setText(t.toString()); + t.setText(""); + } + if(e.getSource()==bX) { + if(t.getText()==null) { + t.setText(""); + } + else { + t.setText(t.getText().substring(0,t.getText().length()-1)); + } + } + if(e.getSource()==bdao) { + String result=" "; + a=Double.parseDouble(t.getText()); + b=1/a; + result=String.valueOf(b); + t.setText(result); + } + if(e.getSource()==bfang) { + String result=" "; + a=Double.parseDouble(t.getText()); + b=a*a; + result=String.valueOf(b); + t.setText(result); + } + if(e.getSource()==bgeng) { + String result=" "; + a=Double.parseDouble(t.getText()); + b=Math.sqrt(a); + result=String.valueOf(b); + t.setText(result); + } + if(e.getSource()==bzhf) { + String result=" "; + a=Double.parseDouble(t.getText()); + if(a==0) { + a=0; + } + else { + a=-a; + } + result=String.valueOf(a); + t.setText(result); + } + if(e.getSource()==mifz) { + s=t.getText(); + } + if(e.getSource()==mizt) { + t.setText(s); + } + if(e.getSource()==bdeng) { + b=Double.parseDouble(t.getText()); + System.out.println(a+" "+b+" "+flag); + switch(flag) { + case '+':t.setText(String.valueOf(a+b));break; + case '-':t.setText(String.valueOf(a-b));break; + case '*':t.setText(String.valueOf(a*b));break; + case '/':t.setText(String.valueOf(a/b));break; + } + } + } +} + + diff --git a/src/java2022spring/jsj.java b/src/java2022spring/jsj.java new file mode 100644 index 0000000000000000000000000000000000000000..9a42caa00b7f1878e04b51b58c04c05afb892671 --- /dev/null +++ b/src/java2022spring/jsj.java @@ -0,0 +1,6 @@ +package java2022spring; +public class jsj { + public static void main(String[] args) { + jisuanji e=new jisuanji(); + } +}