| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 570 人关注过本帖
标题:我的程序代码编译通不过
只看楼主 加入收藏
xuxianyue123
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-6-9
收藏
 问题点数:0 回复次数:9 
我的程序代码编译通不过
我是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");

}

}
搜索更多相关主题的帖子: 编译 代码 
2007-06-10 17:05
pity1115
Rank: 1
等 级:新手上路
威 望:2
帖 子:184
专家分:0
注 册:2006-9-15
收藏
得分:0 
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\");

}

}

2007-06-10 17:35
xuxianyue123
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-6-9
收藏
得分:0 

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

2007-06-10 18:14
zhufeifei
Rank: 1
等 级:新手上路
威 望:2
帖 子:402
专家分:0
注 册:2006-8-11
收藏
得分:0 
以下是引用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\");

}

}

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


在不断的拼搏与进取中,定能创造一片天地!
2007-06-10 19:57
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
以下是引用zhufeifei在2007-6-10 19:57:26的发言:

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

怎么可以?

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


可惜不是你,陪我到最后
2007-06-11 09:09
xuxianyue123
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-6-9
收藏
得分:0 

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

2007-06-11 10:14
zhufeifei
Rank: 1
等 级:新手上路
威 望:2
帖 子:402
专家分:0
注 册:2006-8-11
收藏
得分:0 

我弄错了,呵呵!


在不断的拼搏与进取中,定能创造一片天地!
2007-06-11 22:27
狂放不羁
Rank: 4
等 级:贵宾
威 望:12
帖 子:925
专家分:0
注 册:2007-1-24
收藏
得分:0 

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

2007-06-11 22:45
大嘴先生2
Rank: 1
等 级:新手上路
威 望:2
帖 子:815
专家分:0
注 册:2006-4-17
收藏
得分:0 

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");

}

}

骑白马的未必是王子,也可能是唐僧;有翅膀的未必是天使,也可能是鸟人。
2007-06-12 09:20
大嘴先生2
Rank: 1
等 级:新手上路
威 望:2
帖 子:815
专家分:0
注 册:2006-4-17
收藏
得分:0 
都完事了啊!

骑白马的未必是王子,也可能是唐僧;有翅膀的未必是天使,也可能是鸟人。
2007-06-12 09:25
快速回复:我的程序代码编译通不过
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012443 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved