《简易计算器java代码.pdf》由会员分享,可在线阅读,更多相关《简易计算器java代码.pdf(6页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、package lyw;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ch1 public static void main(String args)try UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName();catch(Exception e)JFrame frame=new CalculatorFrame();frame.show();class CalculatorPanel extends JPane
2、l implements ActionListener private JTextField display;private JButton btn;private double arg=0;private String op=;private boolean start=true;public CalculatorPanel()setLayout(new BorderLayout();display=new JTextField(0);display.setEditable(false);add(display,North);JPanel p=new JPanel();p.setLayout
3、(new GridLayout(4,4);String buttons=789/456*123-0.=+;for(int i=0;i buttons.length();i+)btn=new JButton(buttons.substring(i,i+1);p.add(btn);btn.addActionListener(this);add(p,Center);public void actionPerformed(ActionEvent evt)String s=evt.getActionCommand();if(0=s.charAt(0)&s.charAt(0)=9|s.equals(.)i
4、f(start)display.setText(s);else display.setText(display.getText()+s);start=false;else if(start)if(s.equals(-)display.setText(s);start=false;else op=s;else double x=Double.parseDouble(display.getText();calculate(x);op=s;start=true;public void calculate(double n)if(op.equals(+)arg+=n;else if(op.equals
5、(-)arg-=n;else if(op.equals(*)arg*=n;else if(op.equals(/)arg/=n;else if(op.equals(=)arg=n;display.setText(+arg);class CalculatorFrame extends JFrame public CalculatorFrame()setTitle(java2);setSize(220,180);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););
6、Container contentPane=getContentPane();contentPane.add(new CalculatorPanel();package lyw;import java.awt.*;import java.awt.event.*;publicclass jisuanji static Frame frm=new Frame(Panel class);static Panel pnl=new Panel(new GridLayout(3,3,4,4);static Label lab=new Label(0.,Label.RIGHT);static Label l
7、ab2=new Label();static MenuBar mb=new MenuBar();static Menu menu1=new Menu(查看(V));static Menu menu2=new Menu(编辑(E));static Menu menu3=new Menu(帮助(H));static MenuItem mi1=new MenuItem(标准型(T));static MenuItem mi2=new MenuItem(科学型(S));static MenuItem mi3=new MenuItem(信息员(P));publicstaticvoid main(Strin
8、g args)mb.add(menu1);mb.add(menu2);mb.add(menu3);menu1.add(mi1);menu1.add(mi2);menu1.add(mi3);menu2.add(mi1);/menu3.add(mi1);frm.setSize(200,150);frm.setMenuBar(mb);frm.setVisible(true);frm.setLayout(null);frm.setSize(200,150);/frm.setResizable(false);lab.setBounds(20,30,120,20);lab.setBackground(ne
9、w Color(240,220,190);pnl.setBounds(20,60,120,80);for(int i=1;i=9;i+)pnl.add(new Button(Integer.toString(i);frm.add(lab);frm.add(pnl);frm.setVisible(true);frm.addWindowListener(new WindowAdapter()publicvoidwindowClosing(WindowEvent e)System.exit(0););package lyw;import java.awt.event.*;import java.aw
10、t.*;import javax.swing.*;class jsj extends Frame implements ActionListener static Panel pn1=new Panel(new GridLayout(4,5,3,3);static Panel pn2=new Panel(new GridLayout(4,1,3,3);static Panel pn3=new Panel(new GridLayout(1,3,3,3);static Label lab1=new Label(0.,Label.RIGHT);static Label lab2=new Label(
11、);static MenuBar mb=new MenuBar();static Menu menu1=new Menu(查看(V));static Menu menu2=new Menu(编辑(E));static Menu menu3=new Menu(帮助(H));static MenuItem mi1=new MenuItem(标准型(T));static MenuItem mi2=new MenuItem(科学型(S));static MenuItem mi3=new MenuItem(信息员(P));publicstaticvoid main(String args)jsj frm
12、=new jsj();frm.setTitle(计算器 );frm.setLayout(null);frm.setSize(460,350);frm.setLocation(500,300);frm.setBackground(Color.white);frm.setMenuBar(mb);frm.setResizable(false);lab1.setBounds(30,70,400,20);lab1.setBackground(new Color(210,210,210);lab2.setBounds(30,100,60,42);lab2.setBackground(new Color(2
13、10,210,210);menu1.add(mi1);menu1.add(mi2);menu1.add(mi3);mb.add(menu1);mb.add(menu2);mb.add(menu3);pn1.setBounds(130,150,300,170);pn1.add(new JButton(7);pn1.add(new JButton(8);pn1.add(new JButton(9);pn1.add(new JButton(/);pn1.add(new JButton(sqrt);pn1.add(new JButton(4);pn1.add(new JButton(5);pn1.ad
14、d(new JButton(6);pn1.add(new JButton(*);pn1.add(new JButton(%);pn1.add(new JButton(1);pn1.add(new JButton(2);pn1.add(new JButton(3);pn1.add(new JButton(-);pn1.add(new JButton(1/x);pn1.add(new JButton(0);pn1.add(new JButton(+/-);pn1.add(new JButton(.);pn1.add(new JButton(+);pn1.add(new JButton(=);pn2
15、.setBounds(30,150,60,170);pn2.add(new JButton(MC);pn2.add(new JButton(MR);pn2.add(new JButton(MS);pn2.add(new JButton(M+);pn3.setBounds(130,100,300,42);pn3.add(new JButton(Baclspace);pn3.add(new JButton(CE);pn3.add(new JButton(C);frm.add(lab1);frm.add(lab2);frm.add(pn1);frm.add(pn2);frm.add(pn3);frm.setVisible(true);frm.addWindowListener(new WindowAdapter()publicvoidwindowClosing(WindowEvent e)System.exit(0););Overridepublicvoid actionPerformed(ActionEvent arg0)/TODO Auto-generated method stub
限制150内