0

我一直在学习 Java,但我在使用这个程序时遇到了问题。假设只有两个单选按钮(男性/女性),然后显示您选择的消息对话框,但我不断收到错误消息。这是我的代码:

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));   
      }
   }
}

对于那些认识苹果类的人,你会知道我在哪里学习

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);
   }
}

以下是我得到的错误:

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
4

1 回答 1

4

好吧,首先:源代码在这里无关紧要。你所要做的就是把

private class HandlerClass implements ItemListener

代替

private HandlerClass implements ItemListener
于 2013-08-26T22:21:32.907 回答