import java.awt.*;
import java.awt.event.*;
public class Cen
{
private static Button b;
private static Button c;
public static void main(String args[])
{
Frame f=new Frame("Test");
b=new Button("B");
b.addActionListener(new ButtonHandler()); //好象这里报错.....
c=new Button("C");
c.addActionListener(new ButtonHandler());
f.setLayout(new FlowLayout());
f.add(b);
f.add(c);
f.setSize(200,100);
f.setVisible(true);
}
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("all"); //这个地方输出有问题
if(e.getSource().equals(b))
System.out.println("b");
if(e.getSource().equals(c))
System.out.println("c");
}
}}
import java.awt.event.*;
public class Cen
{
private static Button b;
private static Button c;
public static void main(String args[])
{
Frame f=new Frame("Test");
b=new Button("B");
b.addActionListener(new ButtonHandler()); //好象这里报错.....
c=new Button("C");
c.addActionListener(new ButtonHandler());
f.setLayout(new FlowLayout());
f.add(b);
f.add(c);
f.setSize(200,100);
f.setVisible(true);
}
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("all"); //这个地方输出有问题
if(e.getSource().equals(b))
System.out.println("b");
if(e.getSource().equals(c))
System.out.println("c");
}
}}
解决方案 »
- JOptionPane的问题
- 如何把hibernate利用hql查询获取的数据传入struts的logic iterate中?分不够就加!
- ######websphere中如何close掉数据库connection,而不会让其回收到websphere的连接池中 #####
- Java中的HashTable十个什么东西?
- 靠!如此简单的程序,却找不到错误,帮我。。。。
- getInstance()后一个还是多个实例问题
- 我的corba程序为什么在两台机器上无法运行
- 请问如何在Win2000Server注册类?请赐教。
- 我对JAVA的迷惑
- 拜托提供一下十六进制和十进制之间数据转换的代码
- 关于StringTokenizer的使用!!急!! 高分!
- 如何在JasperReports中实现上脚标
楼主【sure2003】截止到2008-07-14 15:52:08的历史汇总数据(不包括此帖):
发帖的总数量:236 发帖的总分数:10063 每贴平均分数:42
回帖的总数量:202 得分贴总数量:5 回帖的得分率:2%
结贴的总数量:232 结贴的总分数:9963
无满意结贴数:2 无满意结贴分:50
未结的帖子数:4 未结的总分数:100
结贴的百分比:98.31 % 结分的百分比:99.01 %
无满意结贴率:0.86 % 无满意结分率:0.50 %
值得尊敬
c=new Button("C");
c.addActionListener(new Cen().new ButtonHandler());你这个是内部类的问题。
像我上面那个样就没问题了
c=new Button("C");
c.addActionListener(cen.new ButtonHandler());内部类的问题
这样给你改了一下,避免了各种各样的数据交叉问题。import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class Test { public static void main(String args[]) {
new MyFrame("test");
}
}class MyFrame extends Frame { public MyFrame(String title) {
super(title);
b = new Button("B");
b.addActionListener(new ButtonHandler()); //好象这里报错.....
c = new Button("C");
c.addActionListener(new ButtonHandler());
setLayout(new FlowLayout());
add(b);
add(c);
setSize(200, 100);
setVisible(true);
addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private Button b, c; class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) {
System.out.println("all"); //这个地方输出有问题
if (e.getSource().equals(b)) {
System.out.println("b");
}
if (e.getSource().equals(c)) {
System.out.println("c");
}
}
}
}
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;@SuppressWarnings("serial")
public class MyFrame extends Frame { private Button b;
private Button c; public MyFrame() {
super("Test");
ButtonHandler bh = new ButtonHandler();
this.b = new Button("B");
this.b.addActionListener(bh);
this.c = new Button("C");
this.c.addActionListener(bh);
setLayout(new FlowLayout());
add(this.b);
add(this.c);
setSize(200, 100);
pack();
} private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) {
System.out.println("all");
if (e.getSource() == b) System.out.println("b");
if (e.getSource() == c) System.out.println("c");
}
} public static void main(String[] args) {
MyFrame mf = new MyFrame();
mf.setVisible(true);
}}
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class Cen implements ActionListener{
private static Button b;
private static Button c;
public void actionPerformed(ActionEvent e) {
System.out.println("all"); // 这个地方输出有问题
if (e.getSource().equals(b))
System.out.println("b");
if (e.getSource().equals(c))
System.out.println("c");
}
public static void main(String args[]) {
Frame f = new Frame("Test");
b = new Button("B");
b.addActionListener(new Cen()); // 好象这里报错.....
c = new Button("C");
c.addActionListener(new Cen()); f.setLayout(new FlowLayout());
f.add(b);
f.add(c); f.setSize(200, 100);
f.setVisible(true); }
}
写成匿名内部类就可以了