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(); } }

    效果图:

  • 相关阅读:
    LYDSY模拟赛day3 序列
    LYDSY模拟赛day3 涂色游戏
    LYDSY模拟赛day3 平均数
    hdu1757 A Simple Math Problem
    清北国庆day1 (脑)残
    poj3070 Fibonacci
    uva10870 递推关系Recurrences
    湖南附中模拟day1 瞭望塔
    湖南附中模拟day1 收银员
    湖南附中模拟day1 金坷垃
  • 原文地址:https://www.cnblogs.com/AllenRandolph/p/7053650.html
Copyright © 2011-2022 走看看