詳解Java如何給按鈕添加監(jiān)視器
給按鈕添加監(jiān)視器
最近看了 java 相關(guān)方面的內(nèi)容,正好總結(jié)一下,給按鈕添加監(jiān)視器的幾種方式,當(dāng)做是加深對(duì)知識(shí)的理解吧。
直接上代碼吧!
第一種:匿名對(duì)象
界面代碼
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); } }
測(cè)試代碼
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)行截圖:
說(shuō)明:比較常見(jiàn)的一種方式,初學(xué)Java的朋友一般都會(huì)見(jiàn)到這種方法,但是同時(shí)這也是很老的一種方式,不能很好的體現(xiàn)Java代碼的變化。一般并不推薦使用這種方式,但是作為學(xué)習(xí)Java語(yǔ)法的例子,還是非常不錯(cuò)的
第二種:實(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); } }
測(cè)試代碼
package dragon; public class Test { public static void main(String[] args) { new View4(); } }
運(yùn)行截圖:
說(shuō)明:使用繼承 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); } } }
測(cè)試代碼
package dragon; public class Test { public static void main(String[] args) { new View1(); } }
運(yùn)行截圖
說(shuō)明:這種方式和上面第二種差不多,但是我個(gè)人很喜歡這種,主要是少寫(xiě)了幾個(gè)類。這也是一種很好的方式,主要是傳遞參數(shù)的方式,我以前對(duì)這個(gè) 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); } }
測(cè)試代碼
package dragon; public class Test { public static void main(String[] args) { new View(); } }
運(yùn)行截圖
說(shuō)明:Lambda 表達(dá)式 是Java 8 的新特性,主要是簡(jiǎn)化了代碼,這個(gè)可以理解為對(duì)第一種匿名對(duì)象方式的簡(jiǎn)化,主要是去除了很多的模板代碼,從傳遞對(duì)象演進(jìn)到傳遞行為(因?yàn)槠渌嵌嘤嗟模_@個(gè)我現(xiàn)在比較喜歡使用,使用這種方法,可以少些很多的代碼。當(dāng)然了,它也是有不足的,使用 Lambda 表達(dá)式的前提是這個(gè)接口必須是函數(shù)式接口(接口中只有一個(gè)抽象方法),一般這種接口都會(huì)使用一個(gè)注解進(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); //處理注解的類對(duì)象 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) { //訪問(wèn)私有的成員變量,必須設(shè)置訪問(wèn)權(quán)限 field.setAccessible(true); Object object = field.get(ob); //獲取成員變量的注解 ButtonListener buttonListener = field.getDeclaredAnnotation(ButtonListener.class); //如果注解對(duì)象不為空(有的成員變量沒(méi)有注解,所以該對(duì)象為 null), //并且 該成員變量所對(duì)應(yīng)的對(duì)象的類型是JButton 類型(這里只有一個(gè)注解,可以不需要這個(gè)判斷, //但是從代碼的健壯性角度來(lái)說(shuō),應(yīng)該加上,如果有多個(gè)注解,必須進(jìn)行判斷才行)。 if (buttonListener != null && object != null && object instanceof JButton) { //獲取注解的成員變量(必須經(jīng)過(guò)判斷之后才能,獲取成員變量,注解對(duì)象可能為 null) Class<? extends ActionListener> listener = buttonListener.listener(); Constructor<?> con = listener.getDeclaredConstructor(new Class[] {}); ActionListener actionListener = (ActionListener) con.newInstance(new Object[] {}); // ActionListener actionListener = listener.newInstance(); //給按鈕對(duì)象添加監(jiān)視器 ((JButton)object).addActionListener(actionListener); } } } }
測(cè)試代碼
package dragon; public class Test { public static void main(String[] args) { new View5(); } }
注意:ActionListener actionListener = listener.newInstance();
這句話被我注釋了,因?yàn)樵?Java 9中,Class
對(duì)象的 newInstance()
廢棄了,不再推薦使用了,但是這個(gè)ActionListener
是一個(gè)接口,接口是沒(méi)有構(gòu)造方法的,但是我使用反射的方式獲取到了,因?yàn)榻涌谝彩穷惖囊环N,可能是含有 私有的構(gòu)造方法。
運(yùn)行截圖:
說(shuō)明:這種實(shí)現(xiàn)方式,是這里最復(fù)雜的,但是不能說(shuō)它是最差的(不過(guò)速度應(yīng)該是最慢的了,畢竟反射的速度是沒(méi)有直接創(chuàng)建對(duì)象快的。),上面代碼參考了瘋狂Java講義,但是加了一點(diǎn)我的注釋,但是這是一種新的方式,畢竟我們不能永遠(yuǎn)只寫(xiě)一些很容易就看得懂的代碼,也是需要學(xué)習(xí)新的知識(shí),這個(gè)例子主要是學(xué)習(xí)如何使用注解的,可以看出來(lái)雖然很復(fù)雜,但是這也是很有趣的一種方式。
總結(jié)
使用上面五種方式實(shí)現(xiàn)了給按鈕添加監(jiān)視器,還是很有趣的。上面總結(jié)的這些例子,還是本身為了復(fù)習(xí)Java的知識(shí),并且和新學(xué)習(xí)的只是做一個(gè)比較,這樣學(xué)習(xí)的效果會(huì)比較好,添加監(jiān)視器只是實(shí)現(xiàn)的一種方式。這里最重要的第五種方式,因?yàn)槲疫@兩天在學(xué)習(xí)注解的使用,一開(kāi)始看注解很懵逼,現(xiàn)在感覺(jué)有點(diǎn)理解了,說(shuō)明只要付出努力,還是會(huì)有點(diǎn)效果的。一起好好努力吧。
到此這篇關(guān)于詳解Java如何給按鈕添加監(jiān)視器的文章就介紹到這了,更多相關(guān)Java按鈕添加監(jiān)視器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC 攔截器不攔截靜態(tài)資源的三種處理方式方法
本篇文章主要介紹了SpringMVC 攔截器不攔截靜態(tài)資源的三種處理方式方法,詳細(xì)的介紹了三種方法,有興趣的可以了解一下。2017-01-01SpringData @Query和@Modifying注解原理解析
這篇文章主要介紹了SpringData @Query和@Modifying注解原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Java8?stream流的map()方法你會(huì)使用了嗎
在日常的開(kāi)發(fā)工作中經(jīng)常碰到要處理list中數(shù)據(jù)的問(wèn)題。本文主要帶大家了解下Java8?stream流中map()方法的使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12java中應(yīng)用Stack進(jìn)行算術(shù)運(yùn)算的操作
這篇文章主要介紹了java中應(yīng)用Stack進(jìn)行算術(shù)運(yùn)算的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03SpringBoot集成kafka全面實(shí)戰(zhàn)記錄
在實(shí)際開(kāi)發(fā)中,我們可能有這樣的需求,應(yīng)用A從TopicA獲取到消息,經(jīng)過(guò)處理后轉(zhuǎn)發(fā)到TopicB,再由應(yīng)用B監(jiān)聽(tīng)處理消息,即一個(gè)應(yīng)用處理完成后將該消息轉(zhuǎn)發(fā)至其他應(yīng)用,完成消息的轉(zhuǎn)發(fā),這篇文章主要介紹了SpringBoot集成kafka全面實(shí)戰(zhàn),需要的朋友可以參考下2021-11-11Spring Security方法鑒權(quán)的實(shí)現(xiàn)
在Spring Security中,主要有兩種鑒權(quán)方式,一個(gè)是基于web請(qǐng)求的鑒權(quán),一個(gè)是基于方法的鑒權(quán),本文就來(lái)介紹一下Spring Security方法鑒權(quán)的實(shí)現(xiàn),感興趣的可以了解一下2023-12-12