溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2023-04-07 11:42:59 來源:億速云 閱讀:124 作者:iii 欄目:開發(fā)技術(shù)

這篇“Java怎么給按鈕添加監(jiān)視器”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Java怎么給按鈕添加監(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, "點(diǎn)擊了確定按鈕", "確定", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		
		cancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "點(diǎn)擊了取消按鈕", "取消", 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();
	}
}

運(yùn)行截圖:

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

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

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

第二種:實(shí)現(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);
	}
}

實(shí)現(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, "點(diǎn)擊了確定按鈕", "確定", JOptionPane.INFORMATION_MESSAGE);
	}
}

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

測試代碼

package dragon;

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

運(yùn)行截圖:

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

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

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

第三種:實(shí)現(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, "點(diǎn)擊了確定按鈕", "確定", JOptionPane.INFORMATION_MESSAGE);
		}
		if (e.getSource() == cancel) {
			JOptionPane.showMessageDialog(null, "點(diǎn)擊了取消按鈕", "取消", JOptionPane.CANCEL_OPTION);
		}
	}
	 
}

測試代碼

package dragon;

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

運(yùn)行截圖

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

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

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

第四種:Lambda 表達(dá)式

界面代碼

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, "點(diǎn)擊了確定按鈕", "確定", JOptionPane.INFORMATION_MESSAGE);
		});
		
		cancel.addActionListener((e)->{
			JOptionPane.showMessageDialog(null, "點(diǎn)擊了取消按鈕", "取消", JOptionPane.CANCEL_OPTION);
		});
		this.add(box);
	}
}

測試代碼

package dragon;

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

運(yùn)行截圖

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

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

說明:Lambda 表達(dá)式 是Java 8 的新特性,主要是簡化了代碼,這個可以理解為對第一種匿名對象方式的簡化,主要是去除了很多的模板代碼,從傳遞對象演進(jìn)到傳遞行為(因?yàn)槠渌嵌嘤嗟模?。這個我現(xiàn)在比較喜歡使用,使用這種方法,可以少些很多的代碼。當(dāng)然了,它也是有不足的,使用 Lambda 表達(dá)式的前提是這個接口必須是函數(shù)式接口(接口中只有一個抽象方法),一般這種接口都會使用一個注解進(jìn)行修飾:@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)該加上,如果有多個注解,必須進(jìn)行判斷才行)。
			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(); 這句話被我注釋了,因?yàn)樵?Java 9中,Class 對象的 newInstance()廢棄了,不再推薦使用了,但是這個ActionListener 是一個接口,接口是沒有構(gòu)造方法的,但是我使用反射的方式獲取到了,因?yàn)榻涌谝彩穷惖囊环N,可能是含有 私有的構(gòu)造方法。

運(yùn)行截圖:

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

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

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

以上就是關(guān)于“Java怎么給按鈕添加監(jiān)視器”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI