您好,欢迎访问三七文档
importjava.awt.Frame;importjava.awt.Button;importjava.awt.Label;importjava.awt.event.ActionListener;importjava.awt.event.ActionEvent;importjava.awt.FlowLayout;importjava.awt.TextField;importjava.awt.TextArea;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.Color;importjava.lang.Exception;publicclassCalculatorimplementsActionListener{privateFrameFm=newFrame();privateButtonquit=newButton(退出);privateButtonAdd=newButton(+);privateButtonSub=newButton(-);privateButtonMul=newButton(×);privateButtonDiv=newButton(÷);privateLabelAddend=newLabel(加数:);privateLabelAugend=newLabel(被加数:);privateLabelSum=newLabel(和:);privateTextFieldfieldAdd=newTextField();privateTextFieldfieldAug=newTextField();privateTextAreaareaSum=newTextArea();privateFontFt=newFont(宋体,Font.BOLD,18);publicCalculator(){Fm.setTitle(Alex制作.。);Fm.setBounds(200,100,400,300);Fm.setLayout(newFlowLayout(FlowLayout.LEFT,20,20));//×加数××××××××××××××××××××××××××××××××××××××××××××××××××××××××加数Fm.add(Addend);//标签--加数Addend.setFont(Ft);//字体类型Addend.setForeground(Color.blue);//设置字体颜色Addend.setPreferredSize(newDimension(100,30));//设置组件尺寸Fm.add(fieldAdd);//输入区--加数fieldAdd.setPreferredSize(newDimension(140,30));//设置组件尺寸fieldAdd.setFont(Ft);//字体类型fieldAdd.setForeground(Color.red);Fm.add(Add);Add.addActionListener(this);Fm.add(Sub);Sub.addActionListener(this);//×被加数××××××××××××××××××××××××××××××××××××××××××××××××××××被加数Fm.add(Augend);//标签--被加数Augend.setFont(Ft);Augend.setForeground(Color.blue);Augend.setPreferredSize(newDimension(100,30));//设置组件尺寸Fm.add(fieldAug);//输入区--被加数fieldAug.setPreferredSize(newDimension(140,30));//设置组件尺寸fieldAug.setFont(Ft);fieldAug.setForeground(Color.red);Fm.add(Mul);Mul.addActionListener(this);Fm.add(Div);Div.addActionListener(this);//×和××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××和Fm.add(Sum);//标签--和Sum.setFont(Ft);Sum.setForeground(Color.blue);Sum.setPreferredSize(newDimension(100,30));//设置组件尺寸Fm.add(areaSum);areaSum.setPreferredSize(newDimension(160,40));//设置组件尺寸areaSum.setFont(Ft);areaSum.setForeground(Color.red);//×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××退出Fm.add(quit);quit.addActionListener(this);Fm.setVisible(true);}publicvoidactionPerformed(ActionEvente){try{if(e.getSource()==quit)System.exit(0);elseif(e.getSource()==Add)areaSum.setText(Double.toString(Double.parseDouble(fieldAdd.getText())+Double.parseDouble(fieldAug.getText())));elseif(e.getSource()==Sub)areaSum.setText(Double.toString(Double.parseDouble(fieldAdd.getText())-Double.parseDouble(fieldAug.getText())));elseif(e.getSource()==Mul)areaSum.setText(Double.toString(Double.parseDouble(fieldAdd.getText())*Double.parseDouble(fieldAug.getText())));elseif(e.getSource()==Div)areaSum.setText(Double.toString(Double.parseDouble(fieldAdd.getText())/Double.parseDouble(fieldAug.getText())));}catch(ExceptionEx){areaSum.setText(出错了:+Ex);}}publicstaticvoidmain(Stringargs[]){Calculatorcall=newCalculator();}}
本文标题:实验-7-6
链接地址:https://www.777doc.com/doc-5647429 .html