Java beginners GUI Radio Buttons

4.7k views Asked by At

I have been working on learning Java and I am having trouble with this program. It is suppose to just be two radio buttons(male/female) then show a message dialog about which you chose but I keep getting an error. Here is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class gui2 extends JFrame {
   private JRadioButton male;
   private JRadioButton female;
   private ButtonGroup group;

   public gui2() {
      super("the Title");
      setLayout ( new FlowLayout());

      male = new JRadioButton("male", true);
      female = new JRadioButton("female", false);

      add(male);
      add(female);

      group = new ButtonGroup();
      group.add(male);
      group.add(female);

      HandlerClass handler = new HandlerClass();
      male.addItemListener(handler);
      female.addItemListener(handler);
   }
   private HandlerClass implements ItemListener {
      public void itemStateChanged(ItemEvent event) {
         JOptionPane.showMessageDialog(null, String.format("%s", ItemEvent.SELECTED));   
      }
   }
}

For those that recognize the apple class you'll know where i've been learning

import javax.swing.JFrame;

class apples {
   public static void main(String args[]) {

      gui2 go = new gui2();
      go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      go.setSize(300,200);
      go.setVisible(true);
   }
}

Here are the errors I am getting:

gui2.java:28: error: <identifier> expected
   private HandlerClass implements ItemListener {
                       ^
gui2.java:28: error: <identifier> expected
   private HandlerClass implements ItemListener {
                                               ^
gui2.java:33: error: class, interface, or enum expected
}
^
3 errors
1

There are 1 answers

3
Georgian On BEST ANSWER

Well, for one: the source code is irrelevant here. All you have to do is put

private class HandlerClass implements ItemListener

instead of

private HandlerClass implements ItemListener