开发者

problem with addactionlistener

I have this code:

 public void itemStateChanged(ItemEvent e) {
            if(e.getStateChange() == ItemEvent.SELECTED)
            {
                int x = comboBox.getSelectedIndex();
                if(x >=0 && x<=6)
                {
                    JButton button[] = new JButton[31];
                    for(int i = 0; i < 31; i++)
                    {
                        button[i] = new JButton(String.valueOf(i + 1));
                        button[i].addActionListener(this);
                        add(button[i]);


                    }

                }
 public void actionPerformed(ActionEvent e) {

}

But button[i].addactionlistener(this) gives me this error:

addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton
 cannot be 开发者_如何转开发applied to (<anonymous java.awt.event.ItemListener>)

How do I solve this?

 I think this refer to jcombobox


Have your class implement ActionListener.

Edit to add: Initially, I missed the phrase "anonymous" in the error message. That leads me to believe that this code resides inside of an anonymous class declaration. Try replacing

button[i].addActionListener(this)

with

button[i].addActionListener(mcalendar.this)


ah, you have an anonymous inner class for ItemListener.

try

button[i].addActionListener(mcalendar.this);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜