123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileWriter;import java.io.IOException;import javax.swing.*;public class Calculator extends JFrame implements ActionListener{ //声明组件 JTextField t1, t2, t3; JButton add, sub, tim, div; JButton save; //属性,变量 double result=0; Calculator(){ init(); this.setTitle("Calculator"); this.setLayout(new FlowLayout()); this.setSize(500, 500); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init() { //新建组件 t1=new JTextField(10); t2=new JTextField(10); t3=new JTextField(20); add=new JButton("+"); sub=new JButton("-"); tim=new JButton("*"); div=new JButton("/"); save=new JButton("save"); //添加组件 add(t1); add(t2); add(t3); add(add); add(sub); add(tim); add(div); add(save); //注册监听 t1.addActionListener(this); t2.addActionListener(this); t3.addActionListener(this); sub.addActionListener(this); add.addActionListener(this); tim.addActionListener(this); div.addActionListener(this); save.addActionListener(this); //额外功能 t3.setEditable(false); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String first=t1.getText(); String second=t2.getText(); double n1 =Double.parseDouble(first); double n2=Double.parseDouble(second); if(e.getSource().equals(add)) { result=n1+n2; } else if(e.getSource().equals(sub)) { result=n1-n2; } else if(e.getSource().equals(tim)) { result=n1*n2; } else if(e.getSource().equals(div)) { result=n1/n2; } else if(e.getSource().equals(save)) { try { File myObj=new File("result.txt"); if(myObj.createNewFile()) System.out.println("Successfully create the file "+myObj.getName()); else System.out.println("The file already exists"); }catch(IOException e1) { System.out.println("ERROR"); e1.printStackTrace(); } try { FileWriter w=new FileWriter("result.txt"); w.write(result+"\n"); System.out.println("Sucessfully wrote to the file"); w.close(); }catch(IOException e1) { System.out.println("ERROR"); e1.printStackTrace(); } } t3.setText(result+"\n"); } public static void main(String args[]) { Calculator c=new Calculator(); }} 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class Calc extends JFrame implements ActionListener{ JButton b1,b2,b3,b4; JTextField t1,t2,t3; Calc(){ init(); this.setSize(300, 300); this.setTitle("计算"); this.setLayout(new FlowLayout()); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init() { b1=new JButton("加"); b2=new JButton("减"); b3=new JButton("乘"); b4=new JButton("除"); t1=new JTextField(10); t2=new JTextField(10); t3=new JTextField(20); add(b1); add(b2); add(b3); add(b4); add(t1); add(t2); add(t3); t1.addActionListener(this); t2.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); t3.setEditable(false); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub double n1; double n2; double result=0; if(e.getSource().equals(b1)) { try { n1=Double.parseDouble(t1.getText()); n2=Double.parseDouble(t2.getText()); result=n1+n2; t3.setText(String.valueOf(result)); }catch(NumberFormatException ee) { t3.setText("请输入数字"); } } else if(e.getSource().equals(b2)) { try { n1=Double.parseDouble(t1.getText()); n2=Double.parseDouble(t2.getText()); result=n1-n2; t3.setText(String.valueOf(result)); }catch(NumberFormatException ee) { t3.setText("请输入数字"); } } else if(e.getSource().equals(b3)) { try { n1=Double.parseDouble(t1.getText()); n2=Double.parseDouble(t2.getText()); result=n1*n2; t3.setText(String.valueOf(result)); }catch(NumberFormatException ee) { t3.setText("请输入数字"); } } else if(e.getSource().equals(b4)) { try { n1=Double.parseDouble(t1.getText()); n2=Double.parseDouble(t2.getText()); result=n1*n2; t3.setText(String.valueOf(result)); }catch(NumberFormatException ee) { t3.setText("请输入数字"); } } } public static void main(String args[]) { Calc c=new Calc(); }}