日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java实用教程——组件及事件处理——ActionEvent事件

發布時間:2023/12/4 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java实用教程——组件及事件处理——ActionEvent事件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

事件源:
文本框,按鈕,菜單項,密碼框,單選按鈕
注冊監視器:
能夠觸發ActionEvent事件的組件使用方法
addActionListener(ActionListener listener)
處理事件接口:
ActionListener接口中只有一個方法
public void actionPerformed(ActionEvent e)
事件觸發ActionEvent事件后,監視器調用接口中的方法actionPerformed(ActionEvent e)對發生的事件做出處理

ActionEvent類中的方法:
ActionEvent類有如下常用方法
public Object getSource() 可以獲取發生ActionEvent事件的事件源對象的引用

public String getActionCommand() 和該事件相關的一個“命令”字符串,對于文本框,當發生ActionEvent事件是,默認的“命令”字符串是文本框中的文本

主類

public class Example9_6 {public static void main(String args[]) {WindowActionEvent win=new WindowActionEvent();win.setBounds(100,100,310,260);win.setTitle("處理ActionEvent事件");}}

實現監視器的類

import java.awt.*; import java.awt.event.*; public class ReaderListen implements ActionListener{public void actionPerformed(ActionEvent e){String str = e.getActionCommand();System.out.println(str+":"+str.length());}} import java.awt.*; import javax.swing.*; import java.awt.event.*; public class WindowActionEvent extends JFrame {TextField text;ActionListener listener;public WindowActionEvent() {setLayout(new FlowLayout());init();setVisible(true);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}void init() {text = new TextField(10);listener = new ReaderListen();text.addActionListener(listener);this.add(text);} }


public class Example9_7 {public static void main(String args[]) {WindowActionEvent win=new WindowActionEvent();PoliceListen police = new PoliceListen();//創建監視器win.setMyCommandListener(police);//窗口組合監視器win.setBounds(100,100,310,260);win.setTitle("處理ActionEvent事件");}} import javax.swing.*; import java.awt.event.*;//實現這些事件處理必須要有這個包eventpublic interface MyCommandListener extends ActionListener{public void setTextField(JTextField text);public void setJTextArea(JTextArea area); } import javax.swing.*;import java.awt.event.*; public class PoliceListen implements MyCommandListener{JTextField textinput;JTextArea textshow;public void setTextField(JTextField text){textinput=text;}public void setJTextArea(JTextArea area){textshow=area;}public void actionPerformed(ActionEvent e){String str = textinput.getText();textshow.append(str+":"+str.length()+"\n");}} import java.awt.*; import javax.swing.*; import java.awt.event.*; public class WindowActionEvent extends JFrame {JTextField text;//文本區JTextArea textshow;//用于輸出的文本框JButton button;MyCommandListener listener;//監視器public WindowActionEvent() {init();setVisible(true);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}void init() {setLayout(new FlowLayout());//設置布局text = new JTextField(10);textshow = new JTextArea(9,30);button = new JButton("確定"); this.add(text);this.add(new JScrollPane(textshow));//設置為滾動窗格this.add(button);}void setMyCommandListener(MyCommandListener listener){this.listener=listener;listener.setTextField(text);listener.setJTextArea(textshow);text.addActionListener(listener);//text是事件源,listener是監視器button.addActionListener(listener);//button是事件源,listener是監視器} }

總結

以上是生活随笔為你收集整理的java实用教程——组件及事件处理——ActionEvent事件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。