public class adsubprodquoRadButton extends JFrame implements ActionListener { private JButton answer, clr; private JRadioButton r1,r2,r3,r4; //private JCheckBox c1; private JTextField txt1,txt2; //private JLabel lbl1; public adsubprodquoRadButton() {//CONSTRUCTOR Container p = getContentPane(); JPanel jp = new JPanel(); //JPanel jp1 = new JPanel(); ButtonGroup bg = new ButtonGroup(); //ButtonGroup bg1 = new ButtonGroup(); p.add(jp);
bg.add(r1=new JRadioButton("Mutiplication")); bg.add(r2=new JRadioButton("Addtion")); bg.add(r3=new JRadioButton("Subtraction")); bg.add(r4=new JRadioButton("Division")); //bg1.add(c1= new JCheckBox ("Mushroom")); //jp1.add(c1);
//jp.add(lbl1 = new JLabel ("hello")); jp.add(r1); jp.add(r2); jp.add(r3); jp.add(r4); jp.add(txt1=new JTextField(5)); jp.add(txt2=new JTextField(5)); jp.add(answer=new JButton("Show Answer")); jp.add(clr=new JButton("Clear")); answer.addActionListener(this); clr.addActionListener(this); }//constructor public static void main(String[] args) { adsubprodquoRadButton js = new adsubprodquoRadButton(); js.setVisible(true); js.setSize(400,100); js.setTitle("Basic Aritmetic Operations - *, +, -, /"); js.setResizable(true); } public void actionPerformed(ActionEvent e) { double ans=0.0; JOptionPane j = new JOptionPane(); if(e.getSource() == answer) { if(r1.isSelected()) { ans=Double.parseDouble(txt2.getText()) * Double.parseDouble(txt1.getText()); j.showMessageDialog(null,"Answer: "+ ans); }else if(r2.isSelected()) { ans=Double.parseDouble(txt2.getText()) + Double.parseDouble(txt1.getText()); j.showMessageDialog(null,"Answer: "+ ans); }else if(r3.isSelected()) { ans=Double.parseDouble(txt2.getText()) - Double.parseDouble(txt1.getText());