国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java事件響應方法匯總(容器類監聽、監聽器類、AbstractAction、反射)

2019-11-14 20:56:26
字體:
來源:轉載
供稿:網友
java事件響應方法匯總(容器類監聽、監聽器類、AbstractAction、反射)

Java圖形用戶界面中,處理事件時所必須的步驟是:1、創建接受響應的組件(控件)2、實現相關事件監聽接口3、注冊事件源的動作監聽器4、事件觸發時的事件處理相應的可以通過以下的集中方式來作出事件響應。

[java] view plaincopyPRint?
  1. <span style="font-size: 18px;">一、容器類監聽
  2. 效果:單擊窗體中的三個按鈕,實現相應的相應時間。
  3. </span><pre class="java" name="code">import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. //聲明 類時,添加“implements ActionListener”實現監聽的接口,如果要對多種監聽方式進行監聽,則用逗號間隔開
  7. // 比如“implements ActionListener,KeyListener”
  8. class ButtonListener extends JFrame implements ActionListener{
  9. JButton ok, cancel,exit; //創建接受響應的組建,就是三個按鈕
  10. public ButtonListener(String title){
  11. super(title);
  12. this.setLayout(new FlowLayout());
  13. ok = new JButton("確定");
  14. cancel = new JButton("返回");
  15. exit = new JButton("退出");
  16. //下面三個語句 為按鈕分別 注冊監聽器
  17. ok.addActionListener(this);
  18. cancel.addActionListener(this);
  19. exit.addActionListener(this);
  20. getContentPane().add(ok);
  21. getContentPane().add(cancel);
  22. getContentPane().add(exit);
  23. }
  24. //完成 事件觸發時的事件處理
  25. public void actionPerformed(ActionEvent e){
  26. if(e.getSource()==ok)
  27. System.out.println("確定");
  28. if(e.getSource()==cancel)
  29. System.out.println("返回");
  30. if(e.getSource()==exit)
  31. System.exit(0);;
  32. }
  33. public static void main(String args[]) {
  34. ButtonListener pd=new ButtonListener("ActionEvent Demo");
  35. pd.setSize(250,100);
  36. pd.setVisible(true);
  37. }
  38. }
  39. </pre><br>
  40. <br>
  41. <pre></pre>
  42. <p><span style="font-size: 18px;">二、監聽類實現</span><br>
  43. <br>
  44. <pre style="margin: 4px 0px; font-size: 18px; background-color: rgb(240, 240, 240);" class="java" name="code"><span style="font-size: 18px;">效果:單擊窗體中的三個按鈕,實現相應的相應時間。
  45. </span></pre><p></p>
  46. <div><span style="font-size: 18px;"><br>
  47. </span></div>
  48. <pre style="font-size: 18px;" class="html" name="code">import java.awt.*;
  49. import java.awt.event.*;
  50. import javax.swing.*;
  51. class ButtonListener1 extends JFrame { //這里沒有實現監聽
  52. JButton ok, cancel,exit;
  53. public ButtonListener1(String title){
  54. super(title);
  55. this.setLayout(new FlowLayout());
  56. ok = new JButton("確定");
  57. cancel = new JButton("返回");
  58. exit = new JButton("退出");
  59. ok.addActionListener(new MyListener());
  60. cancel.addActionListener(new MyListener());;
  61. exit.addActionListener(new MyListener());;
  62. getContentPane().add(ok);
  63. getContentPane().add(cancel);
  64. getContentPane().add(exit);
  65. }
  66. public static void main(String args[]) {
  67. ButtonListener pd=new ButtonListener("ActionEvent Demo");
  68. pd.setSize(250,100);
  69. pd.setVisible(true);
  70. }
  71. }
  72. //監聽動作事件
  73. class MyListener implements ActionListener{
  74. public void actionPerformed(ActionEvent e){
  75. if(e.getActionCommand()=="確定")
  76. System.out.println("確定");
  77. if(e.getActionCommand()=="返回")
  78. System.out.println("返回");
  79. if(e.getActionCommand()=="退出")
  80. System.exit(0);;
  81. }
  82. } </pre><br>
  83. <span style="font-size: 18px;">三、使用 AbstractAction類實現監聽</span><br>
  84. <span style="font-size: 18px;"> 這個方法,我也沒搞清,照著別人的例子做出來的<br>
  85. 效果:單擊菜單,作出響應</span><br>
  86. <pre style="font-size: 18px;" class="java" name="code">import java.awt.BorderLayout;
  87. import java.awt.event.ActionEvent;
  88. import javax.swing.AbstractAction;
  89. import javax.swing.Action;
  90. import javax.swing.JFrame;
  91. import javax.swing.JMenu;
  92. import javax.swing.JMenuBar;
  93. import javax.swing.JMenuItem;
  94. import javax.swing.JOptionPane;
  95. //此類繼承AbstractAction,必須實現actionPerformed()方法。
  96. class AbstractEvent extends AbstractAction{
  97. //private static final long serialVersionUID = 1L;
  98. AbstractEvent(){
  99. }
  100. public void actionPerformed(ActionEvent e){
  101. //彈出確認對話框
  102. if (e.getActionCommand()=="open"){
  103. JOptionPane.showMessageDialog(null, "打開");
  104. }else if (e.getActionCommand()=="close"){
  105. JOptionPane.showMessageDialog(null, "關閉");
  106. }else if (e.getActionCommand()=="run"){
  107. JOptionPane.showMessageDialog(null, "運行");
  108. }else if (e.getActionCommand()=="stop"){
  109. JOptionPane.showMessageDialog(null, "停止");
  110. }
  111. }
  112. }
  113. public class TestAbstractEvent {
  114. private static JMenuBar menubar;
  115. private static JFrame frame;
  116. //指定MenuEvent的具體處理程序是AbstractEvent類完成的。
  117. final Action MenuEvent=new AbstractEvent();
  118. public TestAbstractEvent(){
  119. frame=new JFrame("menu");
  120. frame.getContentPane().setLayout(new BorderLayout());
  121. menubar=new JMenuBar();
  122. JMenu menuFile=new JMenu("file");
  123. //實例化一個菜單項,并添加監聽openAction,
  124. JMenuItem menuItemopen=new JMenuItem("open");
  125. menuItemopen.addActionListener(MenuEvent);
  126. JMenuItem menuItemclose=new JMenuItem("close");
  127. menuItemclose.addActionListener(MenuEvent);
  128. menuFile.add(menuItemopen);
  129. menuFile.add(menuItemclose);
  130. JMenu menuTool=new JMenu("tool");
  131. JMenuItem menuItemrun=new JMenuItem("run");
  132. menuItemrun.addActionListener(MenuEvent);
  133. JMenuItem menuItemstop=new JMenuItem("stop");
  134. menuItemstop.addActionListener(MenuEvent);
  135. menuTool.add(menuItemrun);
  136. menuTool.add(menuItemstop);
  137. menubar.add(menuFile);
  138. menubar.add(menuTool);
  139. menubar.setVisible(true);
  140. frame.add(menubar,BorderLayout.NORTH);
  141. frame.setSize(400,200);
  142. frame.setVisible(true);
  143. }
  144. public static void main(String[] args){
  145. new TestAbstractEvent();
  146. }
  147. }</pre><br>
  148. <span style="font-size: 18px;">四、</span><span style="font-size: 18px;"> AbstractAction類 + 反射 的方法<br>
  149. <span style="font-size: 18px;">這個方法,我也沒搞清,照著別人的例子做出來的!<br>
  150. </span><br>
  151. 效果:單擊工具欄的三個按鈕,通過按鈕的名稱,反射得到 與按鈕名稱相同的類實現響應。<br>
  152. <br>
  153. </span><pre class="java" name="code">import java.awt.BorderLayout;
  154. import java.awt.event.ActionEvent;
  155. import javax.swing.*;
  156. class ViewAction extends AbstractAction{
  157. private String ActionName="";
  158. //private JFrame frame=null;
  159. private Action action=null;
  160. public ViewAction(){
  161. }
  162. public ViewAction(String ActionName){
  163. this.ActionName=ActionName;
  164. //this.frame=frame;
  165. }
  166. @Override
  167. public void actionPerformed(ActionEvent e) {
  168. Action action=getAction(this.ActionName);
  169. action.execute();
  170. }
  171. private Action getAction(String ActionName){
  172. try{
  173. if (this.action==null){
  174. Action action=(Action)Class.forName(ActionName).newInstance();
  175. this.action=action;
  176. }
  177. return this.action;
  178. }catch(Exception e){
  179. return null;
  180. }
  181. }
  182. }
  183. public class TestAE extends JFrame {
  184. public JToolBar bar=new JToolBar();
  185. String buttonName[]={"b1","b2","b3"};
  186. public TestAE(){
  187. super("事件");
  188. for (int i=0;i<buttonName.length;i++){
  189. ViewAction action=new ViewAction(buttonName[i]);
  190. JButton button=new JButton(buttonName[i]);
  191. button.addActionListener(action);
  192. bar.add(button);
  193. }
  194. this.getContentPane().add(bar,BorderLayout.NORTH);
  195. this.setSize(300, 200);
  196. this.setLocationRelativeTo(null);
  197. this.setVisible(true);
  198. }
  199. public static void main(String [] args){
  200. new TestAE();
  201. }
  202. }
  203. interface Action{
  204. void execute();
  205. }
  206. class b1 implements Action{
  207. public void execute(){
  208. JOptionPane.showMessageDialog(null, "單擊了 b1");
  209. }
  210. }
  211. class b2 implements Action{
  212. public void execute(){
  213. JOptionPane.showMessageDialog(null, "單擊了 b2");
  214. }
  215. }
  216. class b3 implements Action{
  217. public void execute(){
  218. JOptionPane.showMessageDialog(null, "單擊了 b3");
  219. }
  220. }</pre><br>
  221. <br>
  222. <p></p>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂州市| 安新县| 抚州市| 祁门县| 秭归县| 绵竹市| 寿光市| 曲沃县| 潍坊市| 伽师县| 盈江县| 福安市| 高唐县| 忻城县| 永川市| 浮山县| 鄂尔多斯市| 漯河市| 青铜峡市| 元朗区| 油尖旺区| 怀集县| 新宾| 德化县| 东乌| 普格县| 左云县| 凭祥市| 洪江市| 沁阳市| 河东区| 诸暨市| 建昌县| 日照市| 海盐县| 罗甸县| 长泰县| 云安县| 南投县| 万州区| 岳阳市|