2010-11-23 64 views
1

我有两个不同大小的jbuttons。我把更小的jbutton放在了更大尺寸的其他jbutton上。我还将MouseListener添加到对鼠标OnClick执行某些操作的较大大小的jbutton中。如何关注jbutton放在另一个jbutton上?

的问题是,我想鼠标的控制,从更大尺寸的JButton更改为更小尺寸的JButton当鼠标过来尺寸较小的JButton,目前没有发生。

,当我点击小尺寸的JButton不执行任何操作。

可有人请告诉我怎样才能解决这个问题。

代码片段:

CustomButtom button1 = new CustomButton(icon, null, null); 
CustomButton button2 = new CustomButton(icon1, iconRollover, iconPressed); 

button1.setBounds(new Rectangle(542,14,115,86)); 
button2.setBounds(new Rectangle(634,79,24,22)); 

button1.addMouseListener(new MouseListener(){ 
@Override 
public void mouseClicked(){ 
    performing some task 
} 

//rest of the methods are same. 

}); 
class CustomButton extends JButton() 
{ 
    public CustomButton(Icon icon, Icon rollover, Icon pressed) 
    { 
    super(icon); 
    setRolloverEnabled(false); 
    setRolloverIcon(rollover); 
    setPressedIcon(pressed); 
    setContentAreaFilled(false); 
    } 
} 

感谢 乔蒂

+3

乔蒂嗨,你能请张贴演示该问题的简单自包含的代码示例? – 2010-11-23 11:06:50

+0

嗨史蒂夫,我已经把代码片段。请提出建议。 – Jyoti 2010-11-23 11:35:21

回答

1

在你的示例代码,你从来就不是一个侦听器添加到Button2的,因此它永远不会得到一个鼠标事件。