以前我给一个按钮加监听器用的是匿名内部类的方式添加的,比如back.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
}
});我现在有很多按钮,添加监听器的内容是一样的,想象把new ActionListener()拿出来,
但是我ActionListener al=new ActionListener();是错误的,应为ActionListener是个接口谁能教我应该怎么做啊?
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
}
});我现在有很多按钮,添加监听器的内容是一样的,想象把new ActionListener()拿出来,
但是我ActionListener al=new ActionListener();是错误的,应为ActionListener是个接口谁能教我应该怎么做啊?
解决方案 »
- java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
- 在Lotus中 系统提示 反对使用java.util.Date中的方法
- 新手提问!! 请指点
- 关于解析网络xml的中文问题
- 如何理解 Swing的用户界面组件 中 Container c = this.getContentPane(); 只剩10分了, 不是扣
- 问题:用oci8连oracle,报java.lang.UnsatisfiedLinkError:do_open错误。
- 如何将JAVA程序从Jbuilder中导出为可执行文件?100分
- 各位老大,谁能介绍一下java?
- 如何在winNT中运行jdk?
- jbuilder 3.5 enterprise 的 serial 和 key
- Swing画图时遇到的奇怪现象
- 求一sql语句
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
}
}....
MyListener al=new MyListener();
back.addActionListener(al);
如果非要使用匿名内部类的话,那这样如何?
ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
}
};
back.addActionListener(al);
应该用ActionListener接口
实现抽象方法
public void actionPerformed(ActionEvent e)
{
if()
else if
else
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;public class C extends JFrame { private JButton b1, b2, b3; private Container context; public C() {
this.setTitle("测试");
this.setVisible(true);
context = this.getContentPane();
b1 = new JButton("按钮1");
b2 = new JButton("按钮2");
b3 = new JButton("按钮3");
context.setLayout(new BorderLayout());
context.add(b1, BorderLayout.NORTH);
context.add(b2, BorderLayout.CENTER);
context.add(b3, BorderLayout.SOUTH);
MyListener al=new MyListener(this);
b1.addActionListener(al);
b2.addActionListener(al);
b3.addActionListener(al); this.setResizable(false);
this.setBounds(200, 200, 300, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new C();
} //成员内部类,class小写
class MyListener implements ActionListener {
private JFrame frame;
public MyListener(JFrame frame){
this.frame=frame;
}
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
}}
按钮添加监听如下:
MyListner myListener=new MyListener();
JButton btnOK=new JButton("确定");
btnOK.addActionListner(myListener);
btnOK.addKeyListener(myListener);外部类代码如下:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;public class MyListener implements ActionListener, KeyListener { public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnOK){ }
} public void keyPressed(KeyEvent e) {
if(e.getSource()==btnOK){ }
} public void keyReleased(KeyEvent e) {
if(e.getSource()==btnOK){ }
} public void keyTyped(KeyEvent e) {
if(e.getSource()==btnOK){ }
}}
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
}
}....
MyListener al=new MyListener();
back.addActionListener(al);al可以改成this;