2013-08-26 96 views
0

我一直在學習Java,並且遇到了這個程序的麻煩。假設只是兩個單選按鈕(男性/女性),然後顯示關於您選擇的消息對話框,但我不斷收到錯誤。這裏是我的代碼:Java初學者GUI單選按鈕

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

嗯,一個:源代碼在這裏是無關緊要的。所有您需要做的就是把

private class HandlerClass implements ItemListener 

,而不是

private HandlerClass implements ItemListener 
+0

哇,慢的錯誤。謝謝修復它 – ajk4550

+0

@ ajk4550:如果這個答案對您有幫助,那麼請按照我所做的那樣對它投票。如果它解決了你的問題,那麼也接受它。 –

+0

我沒有足夠的聲望投票,它告訴我等4分鐘才能接受,但只要我能接受,我會盡快 – ajk4550