--------------------------------------------------------------------------------
出现这以下错误
Exception in thread “main”java.lang.NoSuchMethodError:ButtonListener:method<init><>V not found
At GUI.<init><GUI.java:13>
At gui1.main<gui1.java:4>
哈哈,,,已经搞了好久还没解决,于是上来找大家帮忙了。
相关三个等程序代码如下:
import java.awt.*; //begin GUI.java
import javax.swing.*;
class GUI extends JFrame
{
JButton btn1=new JButton("Button1");
......
ButtonListener btnListener=new ButtonListener();
GUI()
{
Container c=getContentPane();
c.setLayout(null);
......
c.add(btn1);
btn1.addActionListener(btnListener);
btn1.setBounds(130,30,200,40);
setSize(380,200);
setTitle("GUI Demo with no Listeners");
setVisible(true);
setResizable(false);
}
}//end of GUI.java
import java.awt.*; //begin ButtonListener.java
import java.awt.event.*;
import javax.swing.*;
public class ButtonListener implements ActionListener
{
GUI x;
//下面定义一个构造函数,这样后面的 x.btn2.setText("I Love Java");才能通过
ButtonListener(GUI a) {x=a;}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Button1"))
{
System.out.println("Button1 has been clicked");
x.btn2.setText("I Love Java");
}
}
} //end of ButtonListener.java
//begin gui1.java
class gui1{
public static void main(String args[])
{
GUI x= new GUI();
}
}
//end of gui1.java