2

This program has 2 radio buttons. 1: for a circle and 2: for outputting a square.

This program is basically designed to either output a cirlce or square based off a radio button group.

My issue is that I don't know how to implement an action listener onto the buttons in-order to output the shape. There are errors within initializing the radio buttons when I invoked the method fm. Apparently I think I need a main method.

Bear in mind this is java AWT.

Please comment if you want me to add more details or clarify.

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.applet.Applet;

    public class RadioButton extends Applet
implements ActionListener
    {
        int choice;

        Frame fm = new Frame ("RadioButton Group");
        Label la = new Label ("What shape do you want to draw?:");
        fm.setLayout (new GridLayout (0, 1));
        CheckboxGroup cg1 = new CheckboxGroup ();
        fm.add (la);

        fm.add (new Checkbox ("CIRCLE", cg1, true));
        fm.add (new Checkbox ("SQUARE", cg1, true));

        fm.setSize (250, 200);
        fm.setVisible (true);
        fm.addWindowListener (new WindowAdapter ()


        {

            public void paint (Graphics g)  // How can you 'update the drawing' or repaint it?
            {
                switch (choice) // Maybe for colors if all else fails you can add a switch 'within' a switch. Inefficient-yes but helps.
                {
                    case 1:
                        if (choice == 1)
                            g.fillOval (30, 40, 20, 20);

                    case 2:
                        if (choice == 2)
                            g.fillRect (20, 40, 20, 20);
                        break;
                }
            }
            public void actionPerformed (ActionEvent evt)
            {
                if (evt.getSource () == "CIRCLE")
                    choice = 1;
                else
                    choice = 2;
            }
            public void windowClosing (WindowEvent we)
            {
                System.exit (0);
            }
        }


        );
    }
    }
Fryon Alexandra
  • 139
  • 7
  • 19

1 Answers1

2

You can use an item Listener. This is a complete working example:

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;


import javax.swing.JFrame;


public class Project1 extends JFrame implements ItemListener {

    CheckboxGroup cg1 = new CheckboxGroup ();       //create group
    Checkbox c1 = new Checkbox("Circle",cg1,true);  //circle button
    Checkbox c2 = new Checkbox("Square",cg1,true);  //square button

    public Project1() {
        setLayout(new FlowLayout());        

        //add listeners
        c1.addItemListener(this);
        c2.addItemListener(this);

        //add to frame
        add(c1);
        add(c2);

        //set visible
        this.setVisible(true);
    }


    @Override
    public void itemStateChanged(ItemEvent e) 
    {
        //c1 = circle
        //c2 = square
        if(e.getSource() == c1)
            System.out.println("Circle");
        else
            System.out.println("Square");

    }

    public static void main(String[] args) {
        Project1 p = new Project1();
    }
}

Save this to a .java full and run it. Should give you a decent example of how to accomplish what your looking for.

Chris K
  • 1,581
  • 1
  • 10
  • 17