import java.awt.event.*; import java.awt.*; import javax.swing.*; public class testNi extends Frame implements ActionListener { public Label first,second,result; public Button submit,clear; public TextField num1, num2; public Panel p,p1,p2,p3; CheckboxGroup c = new CheckboxGroup(); Checkbox r1,r2,r3,r4; public testNi(String title) { super(title); setLayout(new FlowLayout()); p = new Panel(); p1 = new Panel(); p2 = new Panel(); p3 = new Panel(); first = new Label("Enter 1st number"); second = new Label("Enter 2nd number"); num1 = new TextField(20); num2 = new TextField(20); submit = new Button("Submit"); clear = new Button("Clear"); result = new Label(" "); p3.add(r1=new Checkbox("addition",c,true)); p3.add( new Checkbox("subtraction",c,false)); p3.add( new Checkbox("multiplication",c,false)); p3.add( new Checkbox("division",c,false)); p.add(first); p.add(num1); p1.add(second); p1.add(num2); p2.add(submit); p2.add(clear); num1.addActionListener(this); num2.addActionListener(this); submit.addActionListener(this); clear.addActionListener(this); add(p3); add(p); add(p1); add(p2); add(result); } public void actionPerformed(ActionEvent e){ double ans=0.0; if(e.getSource() == submit){ if(r1.getState() == true){ ans = Double.parseDouble(num1.getText()) + Double.parseDouble(num2.getText()); result.setText("Answer: " + ans); } }else if(e.getSource() == submit){ if(r2.getState()==true){ ans = Double.parseDouble(num1.getText()) - Double.parseDouble(num2.getText()); result.setText("Answer: " + ans); } }else if(e.getSource() == submit){ if(r3.getState()==true){ ans = Double.parseDouble(num1.getText()) * Double.parseDouble(num2.getText()); result.setText("Answer: " + ans); } } else if(e.getSource() == submit){ if(r4.getState()==true){ ans = Double.parseDouble(num1.getText()) / Double.parseDouble(num2.getText()); result.setText("Answer: " + ans); } }else if(e.getSource() == clear){ num1.setText(""); num2.setText(""); result.setText(""); } } public static void main(String []args){ testNi f = new testNi("This is just a simple test!"); f.setSize(400,200); f.setVisible(true); } }
the problem here in this code is under in actionPerformed, I have 4 radio buttons. whatever button is selected that will be the operation to be used in my textFields. but the problem is only the addition is working. when im going to change the getState() to getSelectedCheckbox() an error will appear and it says cannot find symbol getSelectedCheckbox. what's wrong with my code?please do help..
thanks a lot in advance..