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

詳解Java如何給按鈕添加監(jiān)視器

 更新時間:2023年04月07日 10:41:28   作者:CrazyDragon_King  
這篇文章主要介紹了詳解Java如何給按鈕添加監(jiān)視器,使用匿名對象、實現(xiàn)接口、實現(xiàn)類、Lambda表達式、注解等,需要的朋友可以參考下

給按鈕添加監(jiān)視器

最近看了 java 相關(guān)方面的內(nèi)容,正好總結(jié)一下,給按鈕添加監(jiān)視器的幾種方式,當做是加深對知識的理解吧。

直接上代碼吧!

第一種:匿名對象

界面代碼

package dragon;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class View3 extends JFrame{
	private JButton submit, cancel;
	public View3() {
		this.init();
		this.setLayout(new FlowLayout());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(300,300,250,100);
		this.setVisible(true);
	}
	
	private void init() {
		submit = new JButton("確定");
		cancel = new JButton("取消");
		Box box = Box.createHorizontalBox();
		box.add(submit);
		box.add(Box.createHorizontalStrut(20));
		box.add(cancel);
		
		submit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "點擊了確定按鈕", "確定", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		
		cancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "點擊了取消按鈕", "取消", JOptionPane.CANCEL_OPTION);
			}
		});
		this.add(box);
	}
}

測試代碼

package dragon;public class Test {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->public static void main(String[] args) {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->new View3();}}package dragon;

public class Test {
	public static void main(String[] args) {
		new View3();
	}
}

運行截圖:

在這里插入圖片描述

在這里插入圖片描述

說明:比較常見的一種方式,初學(xué)Java的朋友一般都會見到這種方法,但是同時這也是很老的一種方式,不能很好的體現(xiàn)Java代碼的變化。一般并不推薦使用這種方式,但是作為學(xué)習(xí)Java語法的例子,還是非常不錯的

第二種:實現(xiàn)類

界面代碼

package dragon;

import java.awt.FlowLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class View4 extends JFrame{
	private JButton submit, cancel;
	private SubmitListener submitListener;
	private CancelListener cancelListener;
	
	public View4() {
		this.init();
		this.setLayout(new FlowLayout());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(300,300,250,100);
		this.setVisible(true);
	}
	
	private void init() {
		submit = new JButton("確定");
		cancel = new JButton("取消");
		Box box = Box.createHorizontalBox();
		box.add(submit);
		box.add(Box.createHorizontalStrut(20));
		box.add(cancel);
		
		submitListener = new SubmitListener();
		cancelListener = new CancelListener();
		submit.addActionListener(submitListener);
		cancel.addActionListener(cancelListener);
		this.add(box);
	}
}

實現(xiàn) ActionListener 接口的類

package dragon;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

public class SubmitListener implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent e) {
		JOptionPane.showMessageDialog(null, "點擊了確定按鈕", "確定", JOptionPane.INFORMATION_MESSAGE);
	}
}

class CancelListener implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent e) {
		JOptionPane.showMessageDialog(null, "點擊了取消按鈕", "取消", JOptionPane.CANCEL_OPTION);
	}
	
}

測試代碼

package dragon;

public class Test {
	public static void main(String[] args) {
		new View4();
	}
}

運行截圖:

在這里插入圖片描述

在這里插入圖片描述

說明:使用繼承 ActionListener 接口的類,作為監(jiān)視器,是一種很好的方式。

第三種:實現(xiàn)接口

界面代碼

package dragon;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class View1 extends JFrame implements ActionListener{
	private JButton submit, cancel;
	public View1() {
		this.init();
		this.setLayout(new FlowLayout());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(300,300,250,100);
		this.setVisible(true);
	}
	
	private void init() {
		submit = new JButton("確定");
		cancel = new JButton("取消");
		Box box = Box.createHorizontalBox();
		box.add(submit);
		box.add(Box.createHorizontalStrut(20));
		box.add(cancel);
		
		
		submit.addActionListener(this);
		cancel.addActionListener(this);
		this.add(box);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == submit) {
			JOptionPane.showMessageDialog(null, "點擊了確定按鈕", "確定", JOptionPane.INFORMATION_MESSAGE);
		}
		if (e.getSource() == cancel) {
			JOptionPane.showMessageDialog(null, "點擊了取消按鈕", "取消", JOptionPane.CANCEL_OPTION);
		}
	}
	 
}

測試代碼

package dragon;

public class Test {
	public static void main(String[] args) {
		new View1();
	}
}

運行截圖

在這里插入圖片描述

在這里插入圖片描述

說明:這種方式和上面第二種差不多,但是我個人很喜歡這種,主要是少寫了幾個類。這也是一種很好的方式,主要是傳遞參數(shù)的方式,我以前對這個 submit.addActionListener(this); 很迷惑,哈哈。

第四種:Lambda 表達式

界面代碼

package dragon;

import java.awt.FlowLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class View extends JFrame{
	private JButton submit, cancel;
	public View() {
		this.init();
		this.setLayout(new FlowLayout());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(300,300,250,100);
		this.setVisible(true);
	}
	
	private void init() {
		submit = new JButton("確定");
		cancel = new JButton("取消");
		Box box = Box.createHorizontalBox();
		box.add(submit);
		box.add(Box.createHorizontalStrut(20));
		box.add(cancel);
		
		submit.addActionListener((e)->{
			JOptionPane.showMessageDialog(null, "點擊了確定按鈕", "確定", JOptionPane.INFORMATION_MESSAGE);
		});
		
		cancel.addActionListener((e)->{
			JOptionPane.showMessageDialog(null, "點擊了取消按鈕", "取消", JOptionPane.CANCEL_OPTION);
		});
		this.add(box);
	}
}

測試代碼

package dragon;

public class Test {
	public static void main(String[] args) {
		new View();
	}
}

運行截圖

在這里插入圖片描述

在這里插入圖片描述

說明:Lambda 表達式 是Java 8 的新特性,主要是簡化了代碼,這個可以理解為對第一種匿名對象方式的簡化,主要是去除了很多的模板代碼,從傳遞對象演進到傳遞行為(因為其它是多余的)。這個我現(xiàn)在比較喜歡使用,使用這種方法,可以少些很多的代碼。當然了,它也是有不足的,使用 Lambda 表達式的前提是這個接口必須是函數(shù)式接口(接口中只有一個抽象方法),一般這種接口都會使用一個注解進行修飾:@FunctionalInterface

第五種:注解

界面代碼

package dragon;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;


public class View5 extends JFrame{
	@ButtonListener(listener=SubmitListener.class)
	private JButton submit;
	
	@ButtonListener(listener=CancelListener.class)
	private JButton cancel;
	
	public View5() {
		this.init();
		this.setLayout(new FlowLayout());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(300,300,250,100);
		this.setVisible(true);
	}
	
	private void init() {
		submit = new JButton("確定");
		cancel = new JButton("取消");
		Box box = Box.createHorizontalBox();
		box.add(submit);
		box.add(Box.createHorizontalStrut(20));
		box.add(cancel);
		
		//處理注解的類對象
		ButtonProcess process;
		process = new ButtonProcess();
		try {
			process.addListener(this);
		} catch (Exception e) {
			//直接處理 Exception 異常
			e.printStackTrace();
		}
		
		this.add(box);
	}
}

注解處理類

package dragon;

import java.awt.event.ActionListener;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JButton;

public class ButtonProcess {
	public void addListener(Object ob) throws IllegalArgumentException, IllegalAccessException, NoSuchMethodException, SecurityException, InstantiationException, InvocationTargetException {
		Class<?> clazz = ob.getClass();
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			//訪問私有的成員變量,必須設(shè)置訪問權(quán)限
			field.setAccessible(true);
			Object object = field.get(ob);
			//獲取成員變量的注解
			ButtonListener buttonListener = field.getDeclaredAnnotation(ButtonListener.class);
			//如果注解對象不為空(有的成員變量沒有注解,所以該對象為 null),
			//并且 該成員變量所對應(yīng)的對象的類型是JButton 類型(這里只有一個注解,可以不需要這個判斷,
			//但是從代碼的健壯性角度來說,應(yīng)該加上,如果有多個注解,必須進行判斷才行)。
			if (buttonListener != null && object != null && object instanceof JButton) {
				//獲取注解的成員變量(必須經(jīng)過判斷之后才能,獲取成員變量,注解對象可能為 null)
				Class<? extends ActionListener> listener = buttonListener.listener();
				Constructor<?> con = listener.getDeclaredConstructor(new Class[] {});
				ActionListener actionListener = (ActionListener) con.newInstance(new Object[] {});
			//	ActionListener actionListener = listener.newInstance();
				//給按鈕對象添加監(jiān)視器
				((JButton)object).addActionListener(actionListener);
			}
		}
	}
}

測試代碼

package dragon;

public class Test {
	public static void main(String[] args) {
		new View5();
	}
}

注意:ActionListener actionListener = listener.newInstance(); 這句話被我注釋了,因為在 Java 9中,Class 對象的 newInstance()廢棄了,不再推薦使用了,但是這個ActionListener 是一個接口,接口是沒有構(gòu)造方法的,但是我使用反射的方式獲取到了,因為接口也是類的一種,可能是含有 私有的構(gòu)造方法。

運行截圖:

在這里插入圖片描述

在這里插入圖片描述

說明:這種實現(xiàn)方式,是這里最復(fù)雜的,但是不能說它是最差的(不過速度應(yīng)該是最慢的了,畢竟反射的速度是沒有直接創(chuàng)建對象快的。),上面代碼參考了瘋狂Java講義,但是加了一點我的注釋,但是這是一種新的方式,畢竟我們不能永遠只寫一些很容易就看得懂的代碼,也是需要學(xué)習(xí)新的知識,這個例子主要是學(xué)習(xí)如何使用注解的,可以看出來雖然很復(fù)雜,但是這也是很有趣的一種方式。

總結(jié)

使用上面五種方式實現(xiàn)了給按鈕添加監(jiān)視器,還是很有趣的。上面總結(jié)的這些例子,還是本身為了復(fù)習(xí)Java的知識,并且和新學(xué)習(xí)的只是做一個比較,這樣學(xué)習(xí)的效果會比較好,添加監(jiān)視器只是實現(xiàn)的一種方式。這里最重要的第五種方式,因為我這兩天在學(xué)習(xí)注解的使用,一開始看注解很懵逼,現(xiàn)在感覺有點理解了,說明只要付出努力,還是會有點效果的。一起好好努力吧。

到此這篇關(guān)于詳解Java如何給按鈕添加監(jiān)視器的文章就介紹到這了,更多相關(guān)Java按鈕添加監(jiān)視器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java進階必備之多線程編程

    Java進階必備之多線程編程

    今天帶大家來學(xué)習(xí)java多線程編程,文中有非常詳細的代碼示例及介紹,對正在學(xué)習(xí)java多線程的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • SpringMVC 攔截器不攔截靜態(tài)資源的三種處理方式方法

    SpringMVC 攔截器不攔截靜態(tài)資源的三種處理方式方法

    本篇文章主要介紹了SpringMVC 攔截器不攔截靜態(tài)資源的三種處理方式方法,詳細的介紹了三種方法,有興趣的可以了解一下。
    2017-01-01
  • SpringData @Query和@Modifying注解原理解析

    SpringData @Query和@Modifying注解原理解析

    這篇文章主要介紹了SpringData @Query和@Modifying注解原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • 淺談Java springboot日志管理

    淺談Java springboot日志管理

    這篇文章主要介紹了淺談Java springboot日志管理,文中有非常詳細的代碼示例,對正在學(xué)習(xí)Java的小伙伴們有很好的幫助喲,需要的朋友可以參考下
    2021-05-05
  • java 抽象類和接口的區(qū)別詳細解析

    java 抽象類和接口的區(qū)別詳細解析

    abstractclass和interface是Java語言中對于抽象類定義進行支持的兩種機制,正是由于這兩種機制的存在,才賦予了Java強大的面向?qū)ο竽芰?需要了解的朋友可以參考下
    2012-11-11
  • Java8?stream流的map()方法你會使用了嗎

    Java8?stream流的map()方法你會使用了嗎

    在日常的開發(fā)工作中經(jīng)常碰到要處理list中數(shù)據(jù)的問題。本文主要帶大家了解下Java8?stream流中map()方法的使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-12-12
  • java中應(yīng)用Stack進行算術(shù)運算的操作

    java中應(yīng)用Stack進行算術(shù)運算的操作

    這篇文章主要介紹了java中應(yīng)用Stack進行算術(shù)運算的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • SpringBoot集成kafka全面實戰(zhàn)記錄

    SpringBoot集成kafka全面實戰(zhàn)記錄

    在實際開發(fā)中,我們可能有這樣的需求,應(yīng)用A從TopicA獲取到消息,經(jīng)過處理后轉(zhuǎn)發(fā)到TopicB,再由應(yīng)用B監(jiān)聽處理消息,即一個應(yīng)用處理完成后將該消息轉(zhuǎn)發(fā)至其他應(yīng)用,完成消息的轉(zhuǎn)發(fā),這篇文章主要介紹了SpringBoot集成kafka全面實戰(zhàn),需要的朋友可以參考下
    2021-11-11
  • Eclipse下Javassist正確使用方法代碼解析

    Eclipse下Javassist正確使用方法代碼解析

    這篇文章主要介紹了Eclipse下Javassist正確使用方法代碼解析,javassist-3.15.0-ga.jar包是一款在java開發(fā)中十分重要的jar文件包,需要的朋友可以參考下,文中附下載鏈接。
    2017-12-12
  • Spring Security方法鑒權(quán)的實現(xiàn)

    Spring Security方法鑒權(quán)的實現(xiàn)

    在Spring Security中,主要有兩種鑒權(quán)方式,一個是基于web請求的鑒權(quán),一個是基于方法的鑒權(quán),本文就來介紹一下Spring Security方法鑒權(quán)的實現(xiàn),感興趣的可以了解一下
    2023-12-12

最新評論