1 /* 2 * Shisoku.java --- 四則演算プログラム (GUI 版) 3 */ 4 5 import java.awt.*; // Frame 6 import java.awt.event.*; // WindowAdapter 等 7 8 public class Shisoku extends Frame implements ActionListener { 9 TextField tf_a, tf_b, wa, sa, seki, syou; 10 Button bt, bt2; 11 12 Shisoku() { 13 setSize(400, 400); 14 addWindowListener(new WindowAdapter() { 15 public void windowClosing(WindowEvent e) { 16 System.exit(0); 17 } 18 }); 19 20 setLayout(new GridLayout(7,2)); 21 add(new Label("a")); 22 tf_a = new TextField("0", 10); 23 add(tf_a); 24 add(new Label("b")); 25 tf_b = new TextField("0", 10); 26 add(tf_b); 27 add(new Label("a+b=")); 28 wa = new TextField("0", 10); add(wa); 29 add(new Label("a-b=")); 30 sa = new TextField("0", 10); add(sa); 31 add(new Label("a*b=")); 32 seki = new TextField("0", 10); add(seki); 33 add(new Label("a/b=")); 34 syou = new TextField("0", 10); add(syou); 35 bt = new Button("start"); 36 add(bt); 37 bt.addActionListener(this); 38 bt2 = new Button("exit"); 39 add(bt2); 40 bt2.addActionListener(this); 41 } 42 43 public void actionPerformed(ActionEvent e) { 44 if (e.getSource() == bt) { 45 double a = Double.valueOf(tf_a.getText().trim()).doubleValue(); 46 double b = Double.valueOf(tf_b.getText().trim()).doubleValue(); 47 wa.setText("" + (a + b)); 48 sa.setText("" + (a - b)); 49 seki.setText("" + (a * b)); 50 syou.setText("" + (a / b)); 51 } 52 else if (e.getSource() == bt2) { 53 System.exit(0); 54 } 55 } 56 57 public static void main(String args[]) { 58 Frame f = new Shisoku(); 59 f.show(); 60 } 61 }