0
public class event {
public static void main(String[] args) {
Sframe frame = new Sframe();
frame.setSize(800,600);
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class Sframe extends javax.swing.JFrame {
public Sframe() {
Spanel panel = new Spanel();
getContentPane().add(panel);
panel.buttonadd();
//panel.buttonadd();
}
}
class Spanel extends javax.swing.JPanel {
public void buttonadd() {
javax.swing.JButton blackb = new JButton("Black");
add(blackb);
blackb.addActionListener((ActionListener)EventHandler
.create(ActionListener.class,this,"hello"));
}
public void hello() {
System.out.println("I love every girl");
}
static int i;
}
当我点击黑色按钮时,它会引发以下错误。我正在关注核心Java书。通过EventHandler创建的ActionListener不起作用
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: No method called hello on class Spanel with no arguments
at java.beans.EventHandler.invokeInternal(EventHandler.java:476)
at java.beans.EventHandler.access$000(EventHandler.java:278)
我在错误中跳过了很多行......它是一个长列表。
是的,它的工作......但任何理由......为什么这需要Spanel公开。 – 2013-03-06 12:50:16
这是由于使用'EventHandler'反射的一个要求。查看更新。 – Reimeus 2013-03-06 12:55:15