button.addActionListener(new BHandle());
解决方案 »
- java中左移高位溢出不舍去么?还有没有节操啦
- Java Socket
- swing jcombobox 初始化的时候能用什么事件捕获?
- 在JAVA里这个怎么实现?比如我知道www.csdn.net,我怎么得到访问他时通过的是哪一个端口?
- 弱问:如何让 Applet程序用IE打开
- 在java裡怎麼lock段程序不能同時 被二個另外的程序同時調用?
- 怎样编一个java聊天室??
- 如何在Applet中做本地打印?(可以用Swing组件)
- jdk8安装后的版本?
- 为毛加个final就不报错了。swing
- 散分200讨论JAVA的GC机制!!!!!!!!!!!!!!!!!!!!!!!!高手请进
- Java制作动态报表
然而你用的很明显是ActionListener,这是对鼠标点击事件的一个简化接口,如果只是简单的点击事件,直接addActionListener就可以了
如果要实现对鼠标其他事件的监听,且你添加的是MouseListener,则必须实现MouseListener接口中的每一个方法
另一种较常用且简便的方法是注册一个继承自MouseAdapter的类,因为MouseAdapter实现了MouseListener接口的所有方法,所以你只需重写特定的方法来实现特定的鼠标操作就可以了.
import java.awt.event.*;public class bb
{
public static void main(String[] args)
{
Test t = new Test("testing");
t.setSize(200, 200);
t.setVisible(true);
}
}class Test extends Frame
{
private Button button;
public Test(String str)
{
super(str);
this.add(button(), BorderLayout.NORTH);
}
private Button button() {
Button button = new Button("well");
button.addActionListener(new BHandle()); // 增加按键相应 class BHandle implements ActionListener {
public void actionPerformed(ActionEvent e)
{
button.setBackground(Color.red);
setBackground(Color.cyan);
}
}
return button;
}// Button button()
}
D:\Program Files\JCreator Pro\MyProjects\Bath\Counter1\bb.java:28: cannot resolve symbol
symbol : class BHandle
location: class Test
button.addActionListener(new BHandle()); // 增加按键相应
^
D:\Program Files\JCreator Pro\MyProjects\Bath\Counter1\bb.java:33: local variable button is accessed from within inner class; needs to be declared final
button.setBackground(Color.red);
^
2 errorsProcess completed.
把class BHandle从private Button button{}拿出来,作为class bb 的 inner class. Button button = new Button("well"); 声明为class bb 的member
Button button = new Button("well");
button.addActionListener(new ActtionListener(){ public void actionPerformed(ActionEvent e)
{
button.setBackground(Color.red);
setBackground(Color.cyan);
}
});
class Test extends Frame implements Actionlistener
import java.awt.event.*;public class bb
{
public static void main(String[] args)
{
Test t = new Test("testing");
t.setSize(200, 200);
t.setVisible(true);
}
}class Test extends Frame
{
private Button button1=new BHandle(this); public Test(String str)
{
super(str);
this.add(button1, BorderLayout.NORTH);
} class BHandle extends Button implements ActionListener {
Frame frame;
BHandle(){
super("well");
addActionListener(this);// 增加按键相应
}
BHandle(Frame f){
super("well");
frame=f;
addActionListener(this);// 增加按键相应
} public void actionPerformed(ActionEvent e)
{
setBackground(Color.red);
frame.setBackground(Color.cyan);
}
}
}
public void actionPerformed(ActionEvent e){
……
}
});