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 书。
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)
我在错误中跳过了很多行......它的列表很长。