詳解Java如何給按鈕添加監(jiān)視器
給按鈕添加監(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)文章
SpringBoot項目打包成war包并部署在tomcat上運行的操作步驟
我們開發(fā) SpringBoot 項目有時我們會需要打包成 war 包,放入外置的 Tomcat 中進行運行,或者使用工具idea直接啟動,便于開發(fā)調(diào)試,本文給大家分享SpringBoot項目打包成war包并部署在tomcat上運行的操作步驟,感興趣的朋友一起看看吧2024-03-03
基于idea解決springweb項目的Java文件無法執(zhí)行問題
這篇文章給大家介紹了基于idea解決springweb項目的Java文件無法執(zhí)行問題,文中通過圖文結(jié)合的方式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02
Spring MVC中使用Controller如何進行重定向
這篇文章主要介紹了Spring MVC中使用Controller如何進行重定向操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
使用lombok@Data存在extends時需要注意的問題
在Java編程中,正確實現(xiàn)equals方法是保證對象比較一致性的關(guān)鍵,使用instanceof檢查類型可能導(dǎo)致違反對稱性原則,即當子類和父類都重寫equals時可能出現(xiàn)a.equals(b)不等于b.equals(a)的情況,Lombok的@EqualsAndHashCode注解可以通過callSuper=true參數(shù)2024-10-10

