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;
}
当我点击黑色按钮时,它会抛出以下错误。我正在关注Core Java的书。
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)
我在错误中跳过很多行...这是一个很长的列表。
答案 0 :(得分:1)
为此,目标类必须为public
。反射由EventHandler
引用,并且仅限于能够使用public
类来查找目标方法。
public class Spanel {