当前位置: 代码迷 >> Java相关 >> 我的程序代码编译通不过
  详细解决方案

我的程序代码编译通不过

热度:260   发布时间:2007-06-10 17:05:31.0
我的程序代码编译通不过
我是java新手,好多都不懂,下面是我自己编的简单程序代码我找不出错误,但是编译通不过,提示监听那有错误,请帮忙更正一下。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Demo extends JFrame {

public Demo(String t) {
super(t);
Container contentPane=this.getContentPane();
contentPane.setLayout(new FlowLayout());
setBackground(Color.white);
}

JButton a1=new JButton("demo1");
JButton a2=new JButton("demo2");
JButton a3=new JButton("demo3");
JLabel a4=new JLabel("demo4");
a1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText("xu");}
});
a2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText("xian");}
});
a3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText("yue");}
});



/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Demo beat =new Demo("good");

}

}
搜索更多相关的解决方案: 编译  代码  

----------------解决方案--------------------------------------------------------
程序代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Demo extends JFrame {

public Demo(String t) {
super(t);
Container contentPane=this.getContentPane();
contentPane.setLayout(new FlowLayout());
setBackground(Color.white);
}

JButton a1=new JButton(\"demo1\");
JButton a2=new JButton(\"demo2\");
JButton a3=new JButton(\"demo3\");
JLabel a4=new JLabel(\"demo4\");
a1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText(\"xu\");}
});
a2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText(\"xian\");}
});
a3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText(\"yue\");}
});

//这几句应该放到一个方法里面,而不能直接在类里面这样写.


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Demo beat =new Demo(\"good\");

}

}

----------------解决方案--------------------------------------------------------

经过我多次修改成功了,相当感谢。


----------------解决方案--------------------------------------------------------
以下是引用pity1115在2007-6-10 17:35:11的发言:
程序代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Demo extends JFrame {

public Demo(String t) {
super(t);
Container contentPane=this.getContentPane();
contentPane.setLayout(new FlowLayout());
setBackground(Color.white);
}

JButton a1=new JButton(\"demo1\");
JButton a2=new JButton(\"demo2\");
JButton a3=new JButton(\"demo3\");
JLabel a4=new JLabel(\"demo4\");
a1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText(\"xu\");}
});
a2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText(\"xian\");}
});
a3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText(\"yue\");}
});

//这几句应该放到一个方法里面,而不能直接在类里面这样写.


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Demo beat =new Demo(\"good\");

}

}

可以这样,这是匿名类的用法!


----------------解决方案--------------------------------------------------------
以下是引用zhufeifei在2007-6-10 19:57:26的发言:

可以这样,这是匿名类的用法!

怎么可以?

在类里面只能声明变量,你要做别的事情,必须放到方法里面或者初始化块里面


----------------解决方案--------------------------------------------------------

好象在类里面真不能添加监听器,我试过了,放在方法里面就行了


----------------解决方案--------------------------------------------------------

我弄错了,呵呵!


----------------解决方案--------------------------------------------------------

把想做的事情要么放在static语句块,要么放在方法里,因为放在类里,你怎么通过对象来发送消息呢?呵呵


----------------解决方案--------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Demo extends JFrame //implements ActionListener 为什么不实现接口啊!
{
private static final long serialVersionUID = 1L;
public dfdfds(String t) {
super(t);
Container contentPane=this.getContentPane();
contentPane.setLayout(new FlowLayout());
setBackground(Color.white);
}

JButton a1=new JButton("demo1");
JButton a2=new JButton("demo2");
JButton a3=new JButton("demo3");
JLabel a4=new JLabel("demo4");
a1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText("xu");}
});
a2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText("xian");}
});
a3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){a4.setText("yue");}
});



public static void main(String[] args) {
dfdfds beat =new dfdfds("good");

}

}
----------------解决方案--------------------------------------------------------
都完事了啊!
----------------解决方案--------------------------------------------------------