实验代码:
1 package 实验11; 2 3 import java.awt.*; 4 import javax.swing.*; 5 import java.awt.event.*; 6 7 8 public class Jisuanqi extends Frame implements ActionListener{ 9 public Jisuanqi() { 10 super("计算器"); 11 Frame ff=new Frame("framework test"); 12 ff.setSize(400,100); 13 ff.setLocation(300,240); 14 ff.setLayout(new FlowLayout()); 15 final TextField f1=new TextField("10",8); 16 ff.add(f1); 17 18 //this.add(new Label("+")); 19 Label l1=new Label("+"); 20 ff.add(l1); 21 //this.add(new TextField("20",8)); 22 TextField f2=new TextField("20",8); 23 ff.add(f2); 24 //this.add(new Button("=")); 25 Button b1=new Button("="); 26 ff.add(b1); 27 //this.add(new TextField(10)); 28 TextField f3=new TextField(10); 29 ff.add(f3); 30 ff.addWindowListener(new myclose()); 31 ff.setVisible(true); 32 b1.addActionListener(new ActionListener() 33 {public void actionPerformed(ActionEvent e) 34 {double c; 35 String s1=f1.getText(); 36 double a=Integer.parseInt(s1); 37 String s2=f2.getText(); 38 double b=Integer.parseInt(s2); 39 c=a+b; 40 String m=String.valueOf(c); 41 f3.setText(m); 42 43 } 44 45 private double Integer(String s) { 46 return 0; 47 } 48 }); 49 50 } 51 52 class myclose implements WindowListener{ 53 54 public void windowActivated(WindowEvent arg0) { 55 56 } 57 58 public void windowClosed(WindowEvent arg0) { 59 // TODO Auto-generated method stub 60 61 } 62 63 public void windowClosing(WindowEvent arg0) { 64 65 System.exit(0); 66 } 67 68 public void windowDeactivated(WindowEvent arg0) { 69 70 } 71 72 public void windowDeiconified(WindowEvent arg0) { 73 74 } 75 76 public void windowIconified(WindowEvent arg0) { 77 78 79 } 80 81 public void windowOpened(WindowEvent arg0) { 82 83 84 } 85 86 } 87 88 89 90 public static void main(String[] args) { 91 92 new Jisuanqi(); 93 } 94 95 96 public void actionPerformed(ActionEvent arg0) { 97 98 } 99 100 101 }
实验结果: