首页编程java编程java监听器类是什么?在程序中(java,C#)监听器是啥玩意

java监听器类是什么?在程序中(java,C#)监听器是啥玩意

编程之家2023-10-1392次浏览

大家好,关于java监听器类是什么很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于在程序中(java,C#)监听器是啥玩意的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决您的问题,还望关注下本站哦,希望对各位有所帮助!

java监听器类是什么?在程序中(java,C#)监听器是啥玩意

关于java的监听器

1、public void addWindowListener(WindowListener l)添加指定的窗口侦听器,以从此窗口接收窗口事件。如果 l为 null,则不抛出任何异常,且不执行任何操作。

这个是API中的方法定义,此方法参数为接口WindowListener,任何实现该接口的类都可以作为参数。

2、public abstract class WindowAdapterimplements WindowListener, WindowStateListener, WindowFocusListener

java监听器类是什么?在程序中(java,C#)监听器是啥玩意

接收窗口事件的抽象适配器类。此类中的方法为空。此类存在的目的是方便创建侦听器对象。

扩展此类可创建 WindowEvent侦听器并为所需事件重写该方法。(如果要实现

WindowListener接口,则必须定义该接口内的所有方法。此抽象类将所有方法都定义为

java监听器类是什么?在程序中(java,C#)监听器是啥玩意

null,所以只需针对关心的事件定义方法。)

使用扩展的类可以创建侦听器对象,然后使用窗口的 addWindowListener

方法向该窗口注册侦听器。当通过打开、关闭、激活或停用、图标化或取消图标化而改变了窗口状态时,将调用该侦听器对象中的相关方法,并将

WindowEvent传递给该方法。

3、如果我想在代码中一次性使用某个类(抽象类或具体类)或接口,可以使用匿名类的方式,这样不需自己定义一个My***类,然后再使用,比较方便。用法就是直接在new WindowAdapter()后面加入类定义,在其中实现或覆盖方法就可以了。

匿名类不是返回值,而是相当于new String(“hello”)这种的扩展形式。我觉得匿名类的最多用处就是加监听器时。

附上WindowAdapter源代码:

publicabstractclassWindowAdapter

implementsWindowListener,WindowStateListener,WindowFocusListener

{

publicvoidwindowOpened(WindowEvente){}

publicvoidwindowClosing(WindowEvente){}

publicvoidwindowClosed(WindowEvente){}

publicvoidwindowIconified(WindowEvente){}

publicvoidwindowDeiconified(WindowEvente){}

publicvoidwindowActivated(WindowEvente){}

publicvoidwindowDeactivated(WindowEvente){}

publicvoidwindowStateChanged(WindowEvente){}

publicvoidwindowGainedFocus(WindowEvente){}

publicvoidwindowLostFocus(WindowEvente){}

}

在程序中(java,C#)监听器是啥玩意

监听器即listen,指的是被动监听,举例来说我有一个温度计,它一直监听着温度,如果温度发生了变化水银柱的高度就发生变化,这里温度计是一个监听器,温度变化是一个事件,当这个时间出发了监听器时,则得到水银柱高度变化这么个结果,你懂了么?

Java中的actionlistener是什么

actionlistener字面上理解就是动作监听器。

它是一个接口,在实现此接口的类中,你可以给需要关注其动作的组件(如Button)添加监听器(addActionListener(this);),之后在事件处理方法(public void actionPerformed(ActionEvent event){})中,对每个事件进行不同处理。

给你个例子吧,是我自己写的一个记事本:

import java.io.*;

import java.awt.event.*;

import javax.swing.*;

public class MainClass extends JFrame implements ActionListener{

int width= 500,height= 400;

JPanel panel;

JMenuBar bar;

JMenu fileMenu,editMenu,helpMenu;

JMenuItem打开O,新建N,保存S,另存A,剪切T,复制C,粘贴P,关于A;

JTextArea textArea= null;

File tempFile= null;

public MainClass(){//构造方法

setTitle("TextEdit");

setSize(width,height);

panel=(JPanel)getContentPane();

bar= new JMenuBar();

fileMenu= new JMenu("文件F");

fileMenu.setMnemonic('F');

editMenu= new JMenu("编辑E");

editMenu.setMnemonic('E');

helpMenu= new JMenu("帮助H");

helpMenu.setMnemonic('H');

打开O= new JMenuItem("打开O");

打开O.setMnemonic('O');

新建N= new JMenuItem("新建N");

新建N.setMnemonic('N');

保存S= new JMenuItem("保存S");

保存S.setMnemonic('S');

另存A= new JMenuItem("另存A");

另存A.setMnemonic('A');

剪切T= new JMenuItem("剪切C");

剪切T.setMnemonic('t');

复制C= new JMenuItem("复制C");

复制C.setMnemonic('C');

粘贴P= new JMenuItem("粘贴P");

粘贴P.setMnemonic('P');

关于A= new JMenuItem("关于A");

关于A.setMnemonic('A');

fileMenu.add(打开O);

fileMenu.add(新建N);

fileMenu.add(保存S);

fileMenu.add(另存A);

bar.add(fileMenu);

editMenu.add(剪切T);

editMenu.add(复制C);

editMenu.add(粘贴P);

bar.add(editMenu);

helpMenu.add(关于A);

bar.add(helpMenu);

textArea= new JTextArea();

panel.add("North",bar);

panel.add("Center", textArea);

打开O.addActionListener(this);

新建N.addActionListener(this);

保存S.addActionListener(this);

另存A.addActionListener(this);

剪切T.addActionListener(this);

复制C.addActionListener(this);

粘贴P.addActionListener(this);

关于A.addActionListener(this);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

public void actionPerformed(ActionEvent event){//处理事件

if(event.getSource()==打开O){//处理打开

JFileChooser jfc= new JFileChooser();

jfc.showOpenDialog(panel);

tempFile= jfc.getSelectedFile();

FileReader fis;

try{

fis= new FileReader(tempFile);

textArea.read(fis,null);

textArea.setEditable(true);

}catch(Exception ex){ex.printStackTrace();}

}

if(event.getSource()==新建N){//处理新建

textArea.setEditable(true);

textArea.setText(null);

}

if(event.getSource()==保存S){//处理保存

if(tempFile== null){

JFileChooser jfc= new JFileChooser();

jfc.showSaveDialog(panel);

tempFile= jfc.getSelectedFile();

try{

FileWriter fos= new FileWriter(tempFile);

textArea.write(fos);

}catch(Exception ex){ex.printStackTrace();}

}

else{

try{

FileWriter fos= new FileWriter(tempFile);

textArea.write(fos);

}catch(Exception ex){ex.printStackTrace();}

}

}

if(event.getSource()==另存A){//处理另存

JFileChooser jfc= new JFileChooser();

jfc.showSaveDialog(panel);

tempFile= jfc.getSelectedFile();

try{

FileWriter fos= new FileWriter(tempFile);

textArea.write(fos);

}catch(Exception ex){ex.printStackTrace();}

}

if(event.getSource()==剪切T){//处理剪切

textArea.cut();

}

if(event.getSource()==复制C){//处理复制

textArea.copy();

}

if(event.getSource()==粘贴P){//处理粘贴

textArea.paste();

}

if(event.getSource()==关于A){//处理关于

textArea.setText("Manifest-Version: 1.0\n"+

"Created-By: Libra_JL\n"+

"QQ: 254791521\n");

textArea.setEditable(false);

panel.validate();

validate();

}

}

public static void main(String []args){//主函数

new MainClass();

}

}

关于java监听器类是什么的内容到此结束,希望对大家有所帮助。

java运算符有什么特点?Java语言的特点主要有什么java开发做什么?java主要是做什么的