欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java基礎(chǔ)學習之Swing事件監(jiān)聽

 更新時間:2021年05月26日 08:55:18   作者:一腔詩意醉了酒  
今天學習java的Swing庫,創(chuàng)建桌面應用的時候,突然發(fā)現(xiàn)有些按鈕需要特定的功能響應,故來研究一番Swing的事件監(jiān)聽,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下

一、初始代碼架構(gòu)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Btn extends JFrame{
    public static void main(String []args){
        JFrame f = new JFrame("事件監(jiān)聽測試");
        f.setBounds(0,0,300,400);
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                f.setVisible(false);
                f.dispose();
                System.exit(0);
            }
        });
        Container page = f.getContentPane();
        page.setLayout(new FlowLayout());
        JButton btn = new JButton("打印");
        page.add(btn);
        f.setVisible(true);
    }
    

}

運行的結(jié)果

在這里插入圖片描述

二、需求分析

想要點擊按鈕的時候在終端打印一行信息(比如"按鈕被點擊")

產(chǎn)品爸爸,提了新需求,不能實現(xiàn)也要創(chuàng)造辦法實現(xiàn)的啦

2.1 寫監(jiān)聽器

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Btn{
    public static void main(String []args){
        JFrame f = new JFrame("事件監(jiān)聽測試");
        f.setBounds(0,0,300,400);
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                f.setVisible(false);
                f.dispose();
                System.exit(0);
            }
        });
        Container page = f.getContentPane();
        page.setLayout(new FlowLayout());
        JButton btn = new JButton("打印");

        /**
         * 事件監(jiān)聽的使用步驟:
         * 1. 創(chuàng)建監(jiān)聽聽
         * 2. 將監(jiān)聽器注冊到按鈕上
         * 
         */
        btnListener bl = new btnListener();
        btn.addActionListener(bl);
        page.add(btn);
        f.setVisible(true);
    }

    
    

}

class btnListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            System.out.println("按鈕被點擊了----");
        }
    }

點擊按鈕的結(jié)果

在這里插入圖片描述 

2.2 發(fā)現(xiàn)問題

中規(guī)中矩地實現(xiàn)監(jiān)聽器地話,發(fā)現(xiàn)要另外寫一個類實現(xiàn)ActionListener 接口地方法,使得代碼架構(gòu)顯得比較臃腫。

2.3 使用匿名內(nèi)部類優(yōu)化代碼

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Btn{
    public static void main(String []args){
        JFrame f = new JFrame("事件監(jiān)聽測試");
        f.setBounds(0,0,300,400);
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                f.setVisible(false);
                f.dispose();
                System.exit(0);
            }
        });
        Container page = f.getContentPane();
        page.setLayout(new FlowLayout());
        JButton btn = new JButton("打印");

        /**
         * 事件監(jiān)聽的使用步驟:
         * 1. 創(chuàng)建監(jiān)聽聽
         * 2. 將監(jiān)聽器注冊到按鈕上
         * 
         */
        ActionListener bl = new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                System.out.println("匿名內(nèi)部類優(yōu)化----");
            }
        };

        btn.addActionListener(bl);
        page.add(btn);
        f.setVisible(true);
    }

    
    

}

class btnListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            System.out.println("按鈕被點擊了----");
        }
    }

在這里插入圖片描述

2.4 優(yōu)化完之后發(fā)現(xiàn)還是不是很優(yōu)雅

因為每次監(jiān)聽都有重復代碼

@Override
 public void actionPerformed(ActionEvent e){
     System.out.println("匿名內(nèi)部類優(yōu)化----");
 } 

2.5 使用Lambda表達式再優(yōu)化

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Btn{
    public static void main(String []args){
        JFrame f = new JFrame("事件監(jiān)聽測試");
        f.setBounds(0,0,300,400);
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                f.setVisible(false);
                f.dispose();
                System.exit(0);
            }
        });
        Container page = f.getContentPane();
        page.setLayout(new FlowLayout());
        JButton btn = new JButton("打印");

        /**
         * 事件監(jiān)聽的使用步驟:
         * 1. 創(chuàng)建監(jiān)聽聽
         * 2. 將監(jiān)聽器注冊到按鈕上
         * 
         */
        // ActionListener bl = new ActionListener(){
        //     @Override
        //     public void actionPerformed(ActionEvent e){
        //         System.out.println("匿名內(nèi)部類優(yōu)化----");
        //     }
        // };

        // btn.addActionListener(bl);
        btn.addActionListener((e)->{
            System.out.println("使用Lambda表達式優(yōu)化");
        });
        page.add(btn);
        f.setVisible(true);
    }

    
    

}

class btnListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            System.out.println("按鈕被點擊了----");
        }
    }

結(jié)果

在這里插入圖片描述

2.6 最終的代碼

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Btn{
    public static void main(String []args){
        JFrame f = new JFrame("事件監(jiān)聽測試");
        f.setBounds(0,0,300,400);
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                f.setVisible(false);
                f.dispose();
                System.exit(0);
            }
        });
        Container page = f.getContentPane();
        page.setLayout(new FlowLayout());
        JButton btn = new JButton("打印");
        
        btn.addActionListener((e)->{
            System.out.println("使用Lambda表達式優(yōu)化");
        });
        page.add(btn);
        f.setVisible(true);
    }
}

三、ActionListener接口源碼

/*
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.awt.event;

import java.util.EventListener;

/**
 * The listener interface for receiving action events.
 * The class that is interested in processing an action event
 * implements this interface, and the object created with that
 * class is registered with a component, using the component's
 * {@code addActionListener} method. When the action event
 * occurs, that object's {@code actionPerformed} method is
 * invoked.
 *
 * @see ActionEvent
 * @see <a  rel="external nofollow" >How to Write an Action Listener</a>
 *
 * @author Carl Quinn
 * @since 1.1
 */
public interface ActionListener extends EventListener {

    /**
     * Invoked when an action occurs.
     * @param e the event to be processed
     */
    public void actionPerformed(ActionEvent e);

}

到此這篇關(guān)于Java基礎(chǔ)學習之Swing事件監(jiān)聽的文章就介紹到這了,更多相關(guān)Swing事件監(jiān)聽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • idea插件無法下載4的種解決方式

    idea插件無法下載4的種解決方式

    這篇文章主要介紹了idea插件無法下載4的種解決方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java源碼解析之ClassLoader

    Java源碼解析之ClassLoader

    在看系統(tǒng)啟動的流程中看到了ClassLoader使用,重新溫故下ClassLoader流程和原理,文中有非常詳細的代碼示例,對正在學習java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • shiro之INI配置詳解

    shiro之INI配置詳解

    這篇文章主要為大家詳細介紹了shiro之INI配置的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • swing中Tree與滾動條用法實例分析

    swing中Tree與滾動條用法實例分析

    這篇文章主要介紹了swing中Tree與滾動條用法,以實例形式分析了java基于swing實現(xiàn)圖形界面的使用技巧,需要的朋友可以參考下
    2015-09-09
  • Java實現(xiàn)的3des加密解密工具類示例

    Java實現(xiàn)的3des加密解密工具類示例

    這篇文章主要介紹了Java實現(xiàn)的3des加密解密工具類,結(jié)合完整實例形式分析了3des加密解密的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • java數(shù)據(jù)結(jié)構(gòu)與算法之插入算法實現(xiàn)數(shù)值排序示例

    java數(shù)據(jù)結(jié)構(gòu)與算法之插入算法實現(xiàn)數(shù)值排序示例

    這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之插入算法實現(xiàn)數(shù)值排序的方法,結(jié)合簡單實例形式分析了插入算法的節(jié)點操作與排序相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2016-08-08
  • 詳解Java的Spring框架中bean的定義以及生命周期

    詳解Java的Spring框架中bean的定義以及生命周期

    這篇文章主要介紹了Java的Spring框架中bean的定義以及生命周期,bean的實例化是Java web開發(fā)中的重要基礎(chǔ),需要的朋友可以參考下
    2015-12-12
  • HashMap線程不安全問題解析

    HashMap線程不安全問題解析

    這篇文章主要介紹了HashMap線程不安全問題解析,HashMap的線程不安全體現(xiàn)在會造成死循環(huán)、數(shù)據(jù)丟失、數(shù)據(jù)覆蓋等問題,其中死循環(huán)和數(shù)據(jù)丟失是在JDK1.7中出現(xiàn)的問題,在JDK1.8中已經(jīng)得到解決,但是1.8中仍會有數(shù)據(jù)覆蓋這樣的問題,需要的朋友可以參考下
    2023-11-11
  • Java之Jackson使用案例詳解

    Java之Jackson使用案例詳解

    這篇文章主要介紹了Java之Jackson使用案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • java  實現(xiàn)輸出隨機圖片實例代碼

    java 實現(xiàn)輸出隨機圖片實例代碼

    這篇文章主要介紹了java 實現(xiàn)輸出隨機圖片實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-06-06

最新評論