关于事件驱动, 你没有给bt2注册监听呀,怎么可能event.getSource() == bt1呢?加上bt1.addActionListener(this); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 bt2没注册监听器把上面那句if (event.getSource() == bt2){ }改成if (event.getSource() == bt2){ System.exit(0);}就可以关闭了 import java.awt.*;import java.applet.*;import java.awt.event.*;import java.util.Vector;public class MyWork extends Applet { public void init(){ setLayout(new BorderLayout()); DrawControl dc = new DrawControl(); add("Center",dc); }}class DrawControl extends Panel implements ActionListener{ public DrawControl(){ setLayout(new FlowLayout()); //this.setForeground(Color.red); Label lb1 = new Label("第一季度"); add(lb1); TextField tf1 = new TextField("",6); add(tf1); Label lb2 = new Label("第二季度"); add(lb2); TextField tf2 = new TextField("",6); add(tf2); Label lb3 = new Label("第三季度"); add(lb3); TextField tf3 = new TextField("",6); add(tf3); Label lb4 = new Label("第四季度"); add(lb4); TextField tf4 = new TextField("",6); add(tf4); CheckboxGroup group = new CheckboxGroup(); Checkbox cb1 = new Checkbox("饼状图", group, true); add(cb1); Checkbox cb2 = new Checkbox("柱状图", group, false); add(cb2); Button bt1 = new Button("确定"); add(bt1); bt1.addActionListener(this); Button bt2 = new Button("关闭"); add(bt2); bt2.addActionListener(this); } public void actionPerformed(ActionEvent event){ if (event.getSource() == bt2){System.exit(0);} if (event.getSource() == bt1){ tf1.setText("ddd");} }}这是我的完整程序,可是依然不能运行,总报我的bt1,bt2无法解决。请大家再帮助我看看 这是我给你改了之后的完整代码,我没有调试,只是把事件监听部分给你改了,肯定没问题如果还是不行,就见检查一下别的地方,在给我发消息import java.awt.*;import java.applet.*;import java.awt.event.*;import java.util.Vector;public class MyWork extends Applet { public void init(){ setLayout(new BorderLayout()); DrawControl dc = new DrawControl(); add("Center",dc); }}class DrawControl extends Panel implements ActionListener{ public DrawControl(){ setLayout(new FlowLayout()); //this.setForeground(Color.red); Label lb1 = new Label("第一季度"); add(lb1); TextField tf1 = new TextField("",6); add(tf1); Label lb2 = new Label("第二季度"); add(lb2); TextField tf2 = new TextField("",6); add(tf2); Label lb3 = new Label("第三季度"); add(lb3); TextField tf3 = new TextField("",6); add(tf3); Label lb4 = new Label("第四季度"); add(lb4); TextField tf4 = new TextField("",6); add(tf4); CheckboxGroup group = new CheckboxGroup(); Checkbox cb1 = new Checkbox("饼状图", group, true); add(cb1); Checkbox cb2 = new Checkbox("柱状图", group, false); add(cb2); Button bt1 = new Button("确定"); bt1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { bt1_actionPerformed(e); } }); add(bt1); Button bt2 = new Button("关闭"); bt2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { bt2_actionPerformed(e); } }); add(bt2); void bt1_actionPerformed(ActionEvent e) { System.exit(0); } void bt2_actionPerformed(ActionEvent e) { tf1.setText("ddd"); }} nodreamer(我想抽烟) 首先感谢您对小弟的关心,可是仍然不行。下面的程序我的是通过的,不知道是不是和我实例的方法有关,导致事件处理出错???import java.awt.*;import java.applet.Applet;import java.awt.event.*;public class MyAction extends Applet implements ActionListener{ TextField text1; Button button1,button2; public void init() { text1 = new TextField(20); add(text1); button1 = new Button("Click"); add(button1); button1.addActionListener(this); button2 = new Button("come_on"); add(button2); button2.addActionListener(this); } public void actionPerformed(ActionEvent event) { String msg = new String ("Hello World"); if (event.getSource() == button1) { text1.setText(msg); } if (event.getSource() == button2) { text1.setText("come on"); } }} 你那个bt1,bt2和tf1为局部变量,当然通不过啦把他们提出来,如:import java.awt.*;import java.applet.*;import java.awt.event.*;import java.util.Vector;public class MyWork extends Applet { public void init(){ setLayout(new BorderLayout()); DrawControl dc = new DrawControl(); add("Center",dc); }}class DrawControl extends Panel implements ActionListener{ public DrawControl(){ setLayout(new FlowLayout()); //this.setForeground(Color.red); Label lb1 = new Label("µÚÒ»¼¾¶È"); add(lb1); tf1= new TextField("",6); add(tf1); Label lb2 = new Label("µÚ¶þ¼¾¶È"); add(lb2); TextField tf2 = new TextField("",6); add(tf2); Label lb3 = new Label("µÚÈý¼¾¶È"); add(lb3); TextField tf3 = new TextField("",6); add(tf3); Label lb4 = new Label("µÚËļ¾¶È"); add(lb4); TextField tf4 = new TextField("",6); add(tf4); CheckboxGroup group = new CheckboxGroup(); Checkbox cb1 = new Checkbox("±ý״ͼ", group, true); add(cb1); Checkbox cb2 = new Checkbox("Öù״ͼ", group, false); add(cb2); bt1 = new Button("È·¶¨"); add(bt1); bt1.addActionListener(this); bt1 = new Button("¹Ø±Õ"); add(bt2); bt2.addActionListener(this); } Button bt1; Button bt2; TextField tf1; public void actionPerformed(ActionEvent event){ if (event.getSource() == bt2){System.exit(0);} if (event.getSource() == bt1){ tf1.setText("ddd");} }} 搞定,谢谢Hodex(小何才露尖尖角) 同时也谢谢nodreamer(我想抽烟) 问题正如Hodex(小何才露尖尖角)所说,我应该把bt1和bt2定义为“全局”变量! swing中如何让jtextarea得到另一个线程中不断更新的值? 一个有难度的java树状结构题目,100分,请高手帮忙 谢谢 java.net.URLConnection问题 String问题 请教java学习中的几个问题 为什么子类构造器总是会调用父类构造器? 执行ant里task(cvschangelog)时出现的问题 一个非常妖的问题,不知大侠们碰到过没有? 请教大家,有关protected隐藏的一个小问题! java jdbc接口问题 100分请教:小弟KangTa问题:关于JTree树路径的问题!高手多帮忙! 如何使用JAVA获取用户从建盘上输入的数据
把上面那句if (event.getSource() == bt2){ }改成
if (event.getSource() == bt2){ System.exit(0);}就可以关闭了
import java.applet.*;
import java.awt.event.*;
import java.util.Vector;
public class MyWork extends Applet {
public void init(){
setLayout(new BorderLayout());
DrawControl dc = new DrawControl();
add("Center",dc);
}
}class DrawControl extends Panel implements ActionListener{
public DrawControl(){
setLayout(new FlowLayout());
//this.setForeground(Color.red);
Label lb1 = new Label("第一季度");
add(lb1);
TextField tf1 = new TextField("",6);
add(tf1);
Label lb2 = new Label("第二季度");
add(lb2);
TextField tf2 = new TextField("",6);
add(tf2);
Label lb3 = new Label("第三季度");
add(lb3);
TextField tf3 = new TextField("",6);
add(tf3);
Label lb4 = new Label("第四季度");
add(lb4);
TextField tf4 = new TextField("",6);
add(tf4);
CheckboxGroup group = new CheckboxGroup();
Checkbox cb1 = new Checkbox("饼状图", group, true);
add(cb1);
Checkbox cb2 = new Checkbox("柱状图", group, false);
add(cb2);
Button bt1 = new Button("确定");
add(bt1);
bt1.addActionListener(this);
Button bt2 = new Button("关闭");
add(bt2);
bt2.addActionListener(this); }
public void actionPerformed(ActionEvent event){
if (event.getSource() == bt2){System.exit(0);}
if (event.getSource() == bt1){ tf1.setText("ddd");}
}
}
这是我的完整程序,可是依然不能运行,总报我的bt1,bt2无法解决。请大家再帮助我看看
如果还是不行,就见检查一下别的地方,在给我发消息
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Vector;
public class MyWork extends Applet {
public void init(){
setLayout(new BorderLayout());
DrawControl dc = new DrawControl();
add("Center",dc);
}
}class DrawControl extends Panel implements ActionListener{
public DrawControl(){
setLayout(new FlowLayout());
//this.setForeground(Color.red);
Label lb1 = new Label("第一季度");
add(lb1);
TextField tf1 = new TextField("",6);
add(tf1);
Label lb2 = new Label("第二季度");
add(lb2);
TextField tf2 = new TextField("",6);
add(tf2);
Label lb3 = new Label("第三季度");
add(lb3);
TextField tf3 = new TextField("",6);
add(tf3);
Label lb4 = new Label("第四季度");
add(lb4);
TextField tf4 = new TextField("",6);
add(tf4);
CheckboxGroup group = new CheckboxGroup();
Checkbox cb1 = new Checkbox("饼状图", group, true);
add(cb1);
Checkbox cb2 = new Checkbox("柱状图", group, false);
add(cb2);
Button bt1 = new Button("确定");
bt1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
bt1_actionPerformed(e);
}
});
add(bt1);
Button bt2 = new Button("关闭");
bt2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
bt2_actionPerformed(e);
}
});
add(bt2);
void bt1_actionPerformed(ActionEvent e) {
System.exit(0);
}
void bt2_actionPerformed(ActionEvent e) {
tf1.setText("ddd");
}
}
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class MyAction extends Applet implements ActionListener
{
TextField text1;
Button button1,button2;
public void init()
{
text1 = new TextField(20);
add(text1);
button1 = new Button("Click");
add(button1);
button1.addActionListener(this);
button2 = new Button("come_on");
add(button2);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
String msg = new String ("Hello World");
if (event.getSource() == button1)
{
text1.setText(msg);
}
if (event.getSource() == button2)
{
text1.setText("come on");
}
}
}
把他们提出来,如:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Vector;
public class MyWork extends Applet {
public void init(){
setLayout(new BorderLayout());
DrawControl dc = new DrawControl();
add("Center",dc);
}
}class DrawControl extends Panel implements ActionListener{
public DrawControl(){
setLayout(new FlowLayout());
//this.setForeground(Color.red);
Label lb1 = new Label("µÚÒ»¼¾¶È");
add(lb1);
tf1= new TextField("",6);
add(tf1);
Label lb2 = new Label("µÚ¶þ¼¾¶È");
add(lb2);
TextField tf2 = new TextField("",6);
add(tf2);
Label lb3 = new Label("µÚÈý¼¾¶È");
add(lb3);
TextField tf3 = new TextField("",6);
add(tf3);
Label lb4 = new Label("µÚËļ¾¶È");
add(lb4);
TextField tf4 = new TextField("",6);
add(tf4);
CheckboxGroup group = new CheckboxGroup();
Checkbox cb1 = new Checkbox("±ý״ͼ", group, true);
add(cb1);
Checkbox cb2 = new Checkbox("Öù״ͼ", group, false);
add(cb2);
bt1 = new Button("È·¶¨");
add(bt1);
bt1.addActionListener(this);
bt1 = new Button("¹Ø±Õ");
add(bt2);
bt2.addActionListener(this); }
Button bt1;
Button bt2;
TextField tf1;
public void actionPerformed(ActionEvent event){
if (event.getSource() == bt2){System.exit(0);}
if (event.getSource() == bt1){ tf1.setText("ddd");}
}
}
问题正如Hodex(小何才露尖尖角)所说,我应该把bt1和bt2定义为“全局”变量!