Advertisement
rohitmehra

Untitled

Dec 17th, 2012
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. // Demonstrate JRadioButton
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. /*
  6. <applet code="JRadioButtonDemo" width=300 height=50>
  7. </applet>
  8. */
  9. public class JRadioButtonDemo extends JApplet
  10. implements ActionListener {
  11. JLabel jlab;
  12. public void init() {
  13. try {
  14. SwingUtilities.invokeAndWait(
  15. new Runnable() {
  16. public void run() {
  17. makeGUI();
  18. }
  19. }
  20. );
  21. } catch (Exception exc) {
  22. System.out.println("Can't create because of " + exc);
  23. }
  24. }
  25. private void makeGUI() {
  26. // Change to flow layout.
  27. setLayout(new FlowLayout());
  28. // Create radio buttons and add them to content pane.
  29. JRadioButton b1 = new JRadioButton("A");
  30. b1.addActionListener(this);
  31. add(b1);
  32. JRadioButton b2 = new JRadioButton("B");
  33. b2.addActionListener(this);
  34. add(b2);
  35. JRadioButton b3 = new JRadioButton("C");
  36. b3.addActionListener(this);
  37. add(b3);
  38. // Define a button group.
  39. ButtonGroup bg = new ButtonGroup();
  40. bg.add(b1);
  41. bg.add(b2);
  42. bg.add(b3);
  43. // Create a label and add it to the content pane.
  44. jlab = new JLabel("Select One");
  45. add(jlab);
  46. }
  47. // Handle button selection.
  48. public void actionPerformed(ActionEvent ae) {
  49. jlab.setText("You selected " + ae.getActionCommand());
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement