代码//1和代码//2应该是同样的效果,但是//2没问题,而//1则会提示在MyFrame中找不到方法gogogo的异常,求解释!
异常信息:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: No method called gogogo on class MyFrame with no arguments
代码:
class MyFrame extends javax.swing.JFrame{
public void gogogo(){
java.util.Date date = new java.util.Date();
JOptionPane.showConfirmDialog(null, date.toString());
}
javax.swing.JButton btn = new javax.swing.JButton("Set title by action");
public MyFrame(){
javax.swing.JPanel pnl = new javax.swing.JPanel();
//1
java.awt.event.ActionListener action =
java.beans.EventHandler.create(ActionListener.class, this, "gogogo");
btn.addActionListener(action);
//2
// btn.addActionListener(new java.awt.event.ActionListener(){
// public void actionPerformed(ActionEvent event){
// MyFrame.this.gogogo();
// }
// });
btn.setBounds(10, 10, 100, 50);
pnl.add(btn);
this.add(pnl);
java.awt.Rectangle r = new java.awt.Rectangle(10,10,300,300);
this.setBounds(r);
pack();
}
}