zoukankan      html  css  js  c++  java
  • java笔记之图形类详解---事件监听器

     事件监听器代表负责处理事件的接口。Java 提供了各种事件监听器类,但我们将讨论更频繁使用的那些事件监听器类。一个事件监听器的每个方法有一个参数作为一个对象,该对象是 EventObject 类的子类。例如,鼠标事件监听器的方法将接受 MouseEvent 的实例,其中 MouseEvent 是 EventObject 派生的。

    EventListner 接口

    它是一个标记接口,每一个监听器接口必须扩展它。这个类定义在 java.util 包中。

    事件: 当发生了某个事件的时候,就会有相应处理方案。
    事件源 监听器 事件 处理方案

    以前我们在frame添加的元素点击是无任何响应的,需要添加监听。

    鼠标事件监听器

    键盘事件监听器

    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Demo05 {
        static int count=0;
        public static void main(String[] args) {
            JFrame frame =new JFrame("窗体");
            JPanel panel=new JPanel();
            JLabel nameLable=new JLabel("用户名");
            JTextField nameFiled =new JTextField(10);
            panel.add(nameLable);
            panel.add(nameFiled);
            frame.add(panel);
            
            nameFiled.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    System.out.println("按下的字符:"+e.getKeyChar());
                    System.out.println("获取键对应的数值:"+ e.getKeyCode());
                }
            });
            Demo01.initJframe(frame, 300, 400);
            
        }
    }

    事件监听器练习------记事本的保存实现

    import java.awt.BorderLayout;
    import java.awt.FileDialog;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JTextArea;
    
    public class Demo04 {
        JFrame frame =new JFrame("记事本");
        JMenuBar bar =new JMenuBar();
        JMenu file=new JMenu("文件");
        JMenu open=new JMenu("保存");
        
        JMenuItem saveOne=new JMenuItem("保存");
        JTextArea area =new JTextArea(20, 30);
       area.setLineWrap(true); //设置自动换行 
    public void initNotepad(){ file.add(saveOne); bar.add(file); saveOne.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { FileDialog fileDialog = new FileDialog(frame, "请选择保存的路径",FileDialog.SAVE); fileDialog.setVisible(true); //获取用户选择的路径与文件名 String path = fileDialog.getDirectory(); String fileName = fileDialog.getFile(); //创建一个输入对象 FileOutputStream fileOutputStream = new FileOutputStream(new File(path,fileName)); //获取文本域的内容,把内容写出 String content = area.getText(); fileOutputStream.write(content.getBytes()); //关闭资源 fileOutputStream.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); frame.add(bar,BorderLayout.NORTH);//设置自动换行 frame.add(area); Demo01.initJframe(frame, 300, 400); } public static void main(String[] args) { new Demo04().initNotepad(); } }

    效果图:

  • 相关阅读:
    RSA算法
    Windows-caffe配置
    python 下 excel,csv 文件的读写
    python 网络通讯 服务器端代码demo,能够同时处理多个客户端的连接请求
    python 下串口数据的读取,解析,和保存-
    XML字符串和JAVA对象之间的转化
    MySQL的join on和 where 的执行顺序和区别,以及各种连接说明
    全国各行政区行政编码
    计数器+打卡+习惯+目标APP推荐
    安卓计数器类APP推荐
  • 原文地址:https://www.cnblogs.com/AllenRandolph/p/7053650.html
Copyright © 2011-2022 走看看