Java添加事件监听的四种方法代码实例
Java添加事件的几种方式(转载了codebrother的文章,做了稍微的改动):
/**
*Java事件监听处理——自身类实现ActionListener接口,作为事件监听器
*
*@authorcodebrother
*/
classEventListener1extendsJFrameimplementsActionListener{
privateJButtonbtBlue,btDialog;
publicEventListener1(){
setTitle("JavaGUI事件监听处理");
setBounds(100,100,500,350);
setLayout(newFlowLayout());
btBlue=newJButton("蓝色");
btDialog=newJButton("弹窗");
//将按钮添加事件监听器
btBlue.addActionListener(this);
btDialog.addActionListener(this);
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//***************************事件处理***************************
@Override
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==btBlue){
Containerc=getContentPane();
c.setBackground(Color.BLUE);
}
elseif(e.getSource()==btDialog){
JDialogdialog=newJDialog();
dialog.setBounds(300,200,400,300);
dialog.setVisible(true);
}
}
}
/**
*Java事件监听处理——内部类处理
*
*@authorcodebrother
*/
classEventListener3extendsJFrame{
privateJButtonbtBlue,btDialog;
//构造方法
publicEventListener3(){
setTitle("JavaGUI事件监听处理");
setBounds(100,100,500,350);
setLayout(newFlowLayout());
btBlue=newJButton("蓝色");
btDialog=newJButton("弹窗");
//添加事件监听器对象(面向对象思想)
btBlue.addActionListener(newColorEventListener());
btDialog.addActionListener(newDialogEventListener());
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//内部类ColorEventListener,实现ActionListener接口
classColorEventListenerimplementsActionListener{
@Override
publicvoidactionPerformed(ActionEvente){
Containerc=getContentPane();
c.setBackground(Color.BLUE);
}
}
//内部类DialogEventListener,实现ActionListener接口
classDialogEventListenerimplementsActionListener{
@Override
publicvoidactionPerformed(ActionEvente){
JDialogdialog=newJDialog();
dialog.setBounds(300,200,400,300);
dialog.setVisible(true);
}
}
}
/**
*Java事件监听处理——匿名内部类处理
*
*@authorcodebrother
*/
classEventListener2extendsJFrame{
privateJButtonbtBlue,btDialog;
publicEventListener2(){
setTitle("JavaGUI事件监听处理");
setBounds(100,100,500,350);
setLayout(newFlowLayout());
btBlue=newJButton("蓝色");
btDialog=newJButton("弹窗");
//添加事件监听器(此处即为匿名类)
btBlue.addActionListener(newActionListener(){
//事件处理
@Override
publicvoidactionPerformed(ActionEvente){
Containerc=getContentPane();
c.setBackground(Color.BLUE);
}
});
//并添加事件监听器
btDialog.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
JDialogdialog=newJDialog();
dialog.setBounds(300,200,400,300);
dialog.setVisible(true);
}
});
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
/**
*Java事件监听处理——外部类处理
*
*@authorcodebrother
*/
classEventListener4extendsJFrame{
privateJButtonbtBlue,btDialog;
publicEventListener4(){
setTitle("JavaGUI事件监听处理");
setBounds(100,100,500,350);
setLayout(newFlowLayout());
btBlue=newJButton("蓝色");
btDialog=newJButton("弹窗");
//将按钮添加事件监听器
btBlue.addActionListener(newColorEventListener(this));
btDialog.addActionListener(newDialogEventListener());
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//外部类ColorEventListener,实现ActionListener接口
classColorEventListenerimplementsActionListener{
privateEventListener4el;
ColorEventListener(EventListener4el){
this.el=el;
}
@Override
publicvoidactionPerformed(ActionEvente){
Containerc=el.getContentPane();
c.setBackground(Color.BLUE);
}
}
//外部类DialogEventListener,实现ActionListener接口
classDialogEventListenerimplementsActionListener{
@Override
publicvoidactionPerformed(ActionEvente){
JDialogdialog=newJDialog();
dialog.setBounds(300,200,400,300);
dialog.setVisible(true);
}
}
publicclassActionListenerTest
{
publicstaticvoidmain(Stringargs[])
{
newEventListener2();
}
}