1、 /计算器 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends WindowAdapter implements ActionListener JFrame frame; JTextField show; JButton bc,ce,c,ab,jia,jian,cheng,chu,equ,point,sqrt,zf,bfh,ds; /退格,复位 ,清空,关于,加,减,乘,除,等,点, 2,加减,百分之,倒数 JButton b=new JButton1
2、0;/保存数字 JDialog about; final int length=30; int i=0,j=0,action=0,p=0; /记录数字键连续时间次数 double num=0,getValue;/记录计算结果的值 ,保存值 public Calculator() frame=new JFrame(“计算器 “); frame.addWindowListener(this); frame.setSize(360, 230); frame.setLocation(380,260); frame.setLayout(new FlowLayout(FlowLayout.CENTER);
3、 frame.setResizable(false); show=new JTextField(31); show.setText(“0“); show.setHorizontalAlignment(SwingConstants.RIGHT); show.setEnabled(false); frame.add(show); Panel dispTop=new Panel(); frame.add(dispTop); dispTop.setLayout(new GridLayout(1,4,3,3); bc=new JButton(“BACK“); bc.addActionListener(t
4、his); dispTop.add(bc); ce=new JButton(“CE“); ce.addActionListener(this); dispTop.add(ce); c=new JButton(“C“); c.addActionListener(this); dispTop.add(c); ab=new JButton(“ABOUT“); ab.addActionListener(this); dispTop.add(ab); /about弹出框 about=new JDialog(frame,“关于计算器 “,true); about.addWindowListener(thi
5、s); JLabel label=new JLabel(“http:/“,JLabel.CENTER); about.add(label); about.setSize(200,100); about.setLocation(500,300); /数据显示区 Panel dispMain=new Panel(); frame.add(dispMain); dispMain.setLayout(new GridLayout(1,2,10,10); /按钮左面板 Panel dispLeft=new Panel(); frame.add(dispLeft); dispLeft.setLayout(
6、new GridLayout(4,3,3,3);/四行三列 /按钮右面板 Panel dispRight=new Panel(); frame.add(dispRight); dispRight.setLayout(new GridLayout(4,2,3,3);/四行两列 for(int l=9;l=0;l-) bl=new JButton(String.valueOf(l); dispLeft.add(bl); bl.addActionListener(this); zf=new JButton(“+/-“); zf.addActionListener(this); dispLeft.ad
7、d(zf); point=new JButton(“.“); point.addActionListener(this); dispLeft.add(point); chu=new JButton(“/“); chu.addActionListener(this); dispRight.add(chu); sqrt=new JButton(“sqrt“); sqrt.addActionListener(this); dispRight.add(sqrt); cheng=new JButton(“*“); cheng.addActionListener(this); dispRight.add(
8、cheng); bfh=new JButton(“%“); bfh.addActionListener(this); dispRight.add(bfh); jian=new JButton(“-“); jian.addActionListener(this); dispRight.add(jian); ds=new JButton(“1/x“); ds.addActionListener(this); dispRight.add(ds); jia=new JButton(“+“); jia.addActionListener(this); dispRight.add(jia); equ=ne
9、w JButton(“=“); equ.addActionListener(this); dispRight.add(equ); frame.setVisible(true); /计算器抽象会报错 public void actionPerformed(ActionEvent e) getValue=Double.valueOf(show.getText().doubleValue(); for(int k=0;k1) show.setText(“); for(int t=0;tstr.length()-1;t+) char c=str.charAt(t); show.setText(show
10、.getText()+c); else show.setText(“0“); else if(e.getSource()=ab) about.setVisible(true); else if(e.getSource()=sqrt) num=Math.sqrt(getValue); show.setText(String.valueOf(num); j+;i=0;/ else if(e.getSource()=ds) num=1/getValue; show.setText(String.valueOf(num); j+;i=0;/ else if(e.getSource()=bfh) num
11、=getValue/100; show.setText(String.valueOf(num); j+;i=0; else if(e.getSource()=zf) String str=show.getText(); char c=str.charAt(0); if(c=-) show.setText(“); for(int t=1;tstr.length();t+) show.setText(show.getText()+str.charAt(t); else if(getValue!=0) show.setText(“-“+show.getText(); i=0; /windows关闭时调用此方法 public void windowClosing(WindowEvent e) if(e.getSource()=about) about.setVisible(false); else if(e.getSource()=frame) System.exit(0); public static void main(String args) Calculator c1=new Calculator();