怎样触发choice中选中的某一项的事件
小妹谢谢了

解决方案 »

  1.   

    JRadioButton birdButton = new JRadioButton(birdString);
        birdButton.setMnemonic(KeyEvent.VK_B);
        birdButton.setActionCommand(birdString);
        birdButton.setSelected(true);    JRadioButton catButton = new JRadioButton(catString);
        catButton.setMnemonic(KeyEvent.VK_C);
        catButton.setActionCommand(catString);    JRadioButton dogButton = new JRadioButton(dogString);
        dogButton.setMnemonic(KeyEvent.VK_D);
        dogButton.setActionCommand(dogString);    JRadioButton rabbitButton = new JRadioButton(rabbitString);
        rabbitButton.setMnemonic(KeyEvent.VK_R);
        rabbitButton.setActionCommand(rabbitString);    JRadioButton pigButton = new JRadioButton(pigString);
        pigButton.setMnemonic(KeyEvent.VK_P);
        pigButton.setActionCommand(pigString);    //Group the radio buttons.
        ButtonGroup group = new ButtonGroup();
        group.add(birdButton);
        group.add(catButton);
        group.add(dogButton);
        group.add(rabbitButton);
        group.add(pigButton);    //Register a listener for the radio buttons.
        birdButton.addActionListener(this);
        catButton.addActionListener(this);
        dogButton.addActionListener(this);
        rabbitButton.addActionListener(this);
        pigButton.addActionListener(this);
    ...
    public void actionPerformed(ActionEvent e) {
        picture.setIcon(new ImageIcon("images/" 
                                      + e.getActionCommand() 
                                      + ".gif"));
    }
      

  2.   

    chinButton = new JCheckBox("Chin");
        chinButton.setMnemonic(KeyEvent.VK_C); 
        chinButton.setSelected(true);    glassesButton = new JCheckBox("Glasses");
        glassesButton.setMnemonic(KeyEvent.VK_G); 
        glassesButton.setSelected(true);    hairButton = new JCheckBox("Hair");
        hairButton.setMnemonic(KeyEvent.VK_H); 
        hairButton.setSelected(true);    teethButton = new JCheckBox("Teeth");
        teethButton.setMnemonic(KeyEvent.VK_T); 
        teethButton.setSelected(true);    //Register a listener for the check boxes.
        chinButton.addItemListener(this);
        glassesButton.addItemListener(this);
        hairButton.addItemListener(this);
        teethButton.addItemListener(this);
    ...
    public void itemStateChanged(ItemEvent e) {
        ...
        Object source = e.getItemSelectable();    if (source == chinButton) {
            //...make a note of it...
        } else if (source == glassesButton) {
            //...make a note of it...
        } else if (source == hairButton) {
            //...make a note of it...
        } else if (source == teethButton) {
            //...make a note of it...
        }    if (e.getStateChange() == ItemEvent.DESELECTED)
            //...make a note of it...
        ...
        updatePicture();
    }