加到按钮上的事件死活不执行
我做一个继承自JPanel的类,在这个类里有加了一个方法,代码如下:
public JButton getEMainBtn() {
if (emailBtn == null) {
// Create an action to add to the Context Menu
final MouseListener action = new MouseListener() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
// 双击
} else if (e.getClickCount() == 1) {
// 单击
}
}
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
emailBtn = new JButton();
emailBtn.setOpaque(false); // 设置组件透明
emailBtn.setBorder(null);
emailBtn.setIcon(SparkRes.getImageIcon(SparkRes.MAIL_16x16));
emailBtn.addMouseListener(action);
}
return emailBtn;
}
其实也很简单,这个方法就是返回一个按钮,这个方法是在继承自JPanel的那个类的构造函数里执行的。代码如下:
add(getEMainBtn(), new GridBagConstraints(index++, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
现在的问题是这个按钮是显示出来了,但当用户鼠标点击的时候按钮的事件却没有执行。
那位大侠遇到过这个问题的请指教指教。