java如何在类构造函数中使用JPopupMenu actionlistener?
我制作了一个GUI,其中有很多按钮可以执行完全相同的功能,所以我决定制作一个特殊的JButton
类,它可以实现我想要的功能,其中包括一个JPopupMenu
。然而,我不理解的show
、getHeight
和getWidth
出现了一个错误:“方法getHeight()
对于类型new ActionListener(){}
是未定义的”,“方法getWidth()
对于类型new ActionListener(){}
是未定义的。”
注意:我从JPopupMenu
复制了代码;我不知道它是怎么工作的
public class MyButton extends JButton {
JPopupMenu menu = new JPopupMenu("Menu");
//create menu item
JMenuItem a = new JMenuItem("A");
JMenuItem b = new JMenuItem("B");
JMenuItem c = new JMenuItem("C");
JMenuItem d = new JMenuItem("D");
public MyButton() {
super();
menu.add(a);
menu.add(b);
menu.add(c);
menu.add(d);
this.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent ae) {
menu.show(this, this.getWidth() / 2, this.getHeight() / 2); //here is the error
}
});
}
}
共 (0) 个答案