import java.lang.*; import java.awt.*; import java.awt.event.*; public class thing { public static void main(String[] args) { new thing(); } private class window_watcher implements WindowListener { public void windowOpened(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowClosing(WindowEvent e) { System.out.println("Closing"); System.exit(0); } public window_watcher() {} } private class button_watcher implements ActionListener { public void actionPerformed(ActionEvent e) { Object o=e.getSource(); if (o==clear) { t1.setText("0"); t2.setText("0"); record.setText(""); } if (o==doit) { String text1=t1.getText(); String text2=t2.getText(); String op=""; int int1=0, int2=0, answer=0; boolean ok=true; try { int1=Integer.parseInt(text1); } catch (NumberFormatException ee) { t1.setText(text1+": BAD!"); ok=false; } try { int2=Integer.parseInt(text2); } catch (NumberFormatException ee) { t2.setText(text2+": BAD!"); ok=false; } if (ok) { Checkbox whichop=cbg.getSelectedCheckbox(); if (whichop==cbadd) { answer=int1+int2; op="+"; } else if (whichop==cbsub) { answer=int1-int2; op="-"; } else if (whichop==cbmul) { answer=int1*int2; op="x"; } else if (whichop==cbdiv) { if (int2==0) ok=false; else answer=int1/int2; op="/"; } if (ok) record.append("\n"+text1+op+text2+"="+answer); else record.append("\n"+text1+op+text2+"= BAD!"); } } } public button_watcher() {} } private TextField t1, t2; private CheckboxGroup cbg; private Checkbox cbadd, cbsub, cbmul, cbdiv; private Button doit, clear; private TextArea record; public thing() { Frame f=new Frame("thing"); f.addWindowListener(new window_watcher()); Panel p=new Panel(); p.setLayout(null); p.setSize(250,470); f.add(p); Label l1=new Label("First Operand"); l1.setBounds(20,20,100,20); p.add(l1); t1=new TextField("0"); t1.setBounds(20,45,200,20); p.add(t1); Label l2=new Label("Second Operand"); l2.setBounds(20,70,100,20); p.add(l2); t2=new TextField("0"); t2.setBounds(20,95,200,20); p.add(t2); Label l3=new Label("Operation"); l3.setBounds(20,120,100,20); p.add(l3); cbg=new CheckboxGroup(); cbadd=new Checkbox("Add",true,cbg); cbadd.setBounds(45,145,100,20); p.add(cbadd); cbsub=new Checkbox("Subtract",false,cbg); cbsub.setBounds(45,170,100,20); p.add(cbsub); cbmul=new Checkbox("Multiply",false,cbg); cbmul.setBounds(45,195,100,20); p.add(cbmul); cbdiv=new Checkbox("Divide",false,cbg); cbdiv.setBounds(45,220,100,20); p.add(cbdiv); button_watcher buttoneer=new button_watcher(); doit=new Button("Do It"); doit.setBounds(45,250,50,20); doit.addActionListener(buttoneer); p.add(doit); clear=new Button("Clear"); clear.setBounds(145,250,50,20); clear.addActionListener(buttoneer); p.add(clear); Label l4=new Label("Record"); l4.setBounds(20,280,100,20); p.add(l4); record=new TextArea("Hello!",30,100); record.setBounds(20,300,200,150); p.add(record); f.pack(); f.show(); } }