import java.awt.*; // for layout managers import java.awt.event.*; // for ActionListener/ActionEvent import javax.swing.*; // JPanel, JFrame public class Ch16_4a // enhancement of Ch16_4 where we add to the calculator a pair of radio buttons to indicate int versus double computation { public static void main(String[] args) { JFrame f=new JFrame("calculator"); f.setSize(550,175); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); CalculatorPanel cp=new CalculatorPanel(); f.add(cp); f.setVisible(true); } public static class CalculatorPanel extends JPanel implements ActionListener // for JButtons { private JTextField f1, f2, f3; // f1 and f2 will be for input, f3 for output (could be a JLabel) private JButton add, sub, mul, div; // our JButtons to do the 4 arithmetic operations for the calculator private JRadioButton intButton, doubleButton; // JRadioButtons to indicate the type of operand to use (int vs double) public CalculatorPanel() { add=new JButton("Add"); // create the JButtons sub=new JButton("Subtract"); mul=new JButton("Multiply"); div=new JButton("Divide"); add.addActionListener(this); // add this as their ActionListeners sub.addActionListener(this); mul.addActionListener(this); div.addActionListener(this); intButton=new JRadioButton("int"); doubleButton=new JRadioButton("double"); ButtonGroup g1=new ButtonGroup(); g1.add(intButton); g1.add(doubleButton); intButton.setSelected(true); JLabel lab1=new JLabel("Number 1"); // create the 3 JLabels for each of the JTextFields JLabel lab2=new JLabel("Number 2"); JLabel lab3=new JLabel("Result"); f1=new JTextField("",10); // create the 3 JTextFields, all initially blank f2=new JTextField("",10); f3=new JTextField("",10); f3.setEditable(false); // f3 will be used for output only so is not editable (could be a JLabel) JPanel p1=new JPanel(); // create a JPanel for the JLabels and JTextFields p1.add(lab1); p1.add(f1); p1.add(lab2); p1.add(f2); p1.add(lab3); p1.add(f3); JPanel p2=new JPanel(); // create a JPanel for the 4 JButtons p2.add(add); p2.add(sub); p2.add(mul); p2.add(div); JPanel p4=new JPanel(); // add the JRadioButtons along the bottom of the GUI p4.add(intButton); p4.add(doubleButton); JPanel p3=new JPanel(new BorderLayout()); // add the two JPanels, one per row p3.add(p1,BorderLayout.NORTH); p3.add(p2,BorderLayout.CENTER); p3.add(p4,BorderLayout.SOUTH); add(p3); // add the last JPanel to this } public void actionPerformed(ActionEvent e) // upon a JButton press { if(intButton.isSelected()) // determine which radio button is selected and perform { // proper operation - int vs double int num1, num2, result; // note that we have to declare num1, num2, result in here so that they can be ints versus doubles (below in the else clause) num1=(int)Double.parseDouble(f1.getText()); num2=(int)Double.parseDouble(f2.getText()); if(e.getSource()==add) // perform the arithmetic operation based on the JButton pressed, storing result in result result=num1+num2; else if(e.getSource()==sub) result=num1-num2; else if(e.getSource()==mul) result=num1*num2; else result=num1/num2; f3.setText(""+result); // display the result } else { double num1, num2, result; // here we declare the variables as doubles num1=Double.parseDouble(f1.getText()); // get the two input numbers as doubles num2=Double.parseDouble(f2.getText()); if(e.getSource()==add) // perform the arithmetic operation based on the JButton pressed, storing result in result result=num1+num2; else if(e.getSource()==sub) result=num1-num2; else if(e.getSource()==mul) result=num1*num2; else result=num1/num2; f3.setText(""+result); // display the result } } } }