1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
/* Get Selected Radio Button Example This java example shows how to get selected radio button using Java AWT CheckboxGroup class. */ import java.applet.Applet; import java.awt.Checkbox; import java.awt.CheckboxGroup; import java.awt.Graphics; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; /* <applet code="GetSelectedRadioButtonExample" width=200 height=200> </applet> */ public class GetSelectedRadioButtonExample extends Applet implements ItemListener{ CheckboxGroup lngGrp = null; public void init(){ //create group lngGrp = new CheckboxGroup(); //create checkboxes and add to group Checkbox java = new Checkbox("Java", lngGrp, true); Checkbox cpp = new Checkbox("C++", lngGrp, false); Checkbox vb = new Checkbox("VB", lngGrp, false); //add radio buttons add(java); add(cpp); add(vb); //add listeners java.addItemListener(this); cpp.addItemListener(this); vb.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g){ /* * To get selected radio button, use * Checkbox getSelectedCheckbox() * method of CheckboxGroup class. */ Checkbox chk = lngGrp.getSelectedCheckbox(); g.drawString(chk.getLabel() + " is selected", 10 ,70); } } |
Example Output
y is that ?
it says
“main” java.lang.NoSuchMethodError:main ?
what should i do ?
is it possible to write the two components code(Check box, radio button) in the one itemStateChanged method..if yes, how to do? becuause I am getting problem.i.e. I wrote 2 components code in the one itemStateChange method..If I click first component, it is displaying first component, then If i click on second, it is displaying again first and second components ouput…i donno know y? can you help me out