Note that there are some explanatory texts on larger screens.

plurals
  1. POJava- Adding Radio Buttons using AWT?
    text
    copied!<p>This program has 2 radio buttons. 1: for a circle and 2: for outputting a square.</p> <p>This program is basically designed to either output a cirlce or square based off a radio button group.</p> <p>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.</p> <p><em>Bear in mind this is java AWT.</em></p> <p><strong>Please comment if you want me to add more details or clarify.</strong></p> <pre><code> 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); } } ); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload