EventHandler.create问题

epsilon-delta 2010-09-22 01:48:12
最近学习EventHandler.create方法,按照API的说明,试图通过这个方法创建一个ActionListener,实现将窗口背景色变为蓝色。
主要是继承JPanel新建了一个ButtonPanel类, 里面增加了个 loadBlue方法,并使用 EventHandler.create(ActionListener.class,
MyButtonPanel, "loadBlue")去创建ActionListener,但是实际点击对应按钮触发这个ActionListener时报错如下(有很长一串,我就列了最重要的第一行)
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: No method called loadBlue on class test.ButtonPanel with no arguments

比较不明白原因,我直接调用过MyButtonPanel.loadBlue(),是没有问题的

class ButtonFrame extends JFrame {
public ButtonFrame() {
.....

MyButtonPanel = new ButtonPanel();

makeButton("BLUE", EventHandler.create(ActionListener.class,
MyButtonPanel, "loadBlue"));

this.add(MyButtonPanel);


}

public void makeButton(String name, ActionListener listener) {
JButton cButton = new JButton(name);
cButton.addActionListener(listener);
MyButtonPanel.add(cButton);
}

private ButtonPanel MyButtonPanel;

}



class ButtonPanel extends JPanel {
public void loadBlue() {
super.setBackground(Color.blue);
}
}
...全文
83 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
epsilon-delta 2010-09-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 magong 的回复:]

引用楼主 bluelliant 的回复:
class ButtonPanel extends JPanel {

这个类若想被EventHandler调用,则必须是public的,因为所在包不同。改成

public class ButtonPanel extends JPanel {
[/Quote]

果然如此,谢谢啦
magong 2010-09-22
  • 打赏
  • 举报
回复
[Quote=引用楼主 bluelliant 的回复:]
class ButtonPanel extends JPanel {
[/Quote]
这个类若想被EventHandler调用,则必须是public的,因为所在包不同。改成

public class ButtonPanel extends JPanel {

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧