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

首頁 > 編程 > Java > 正文

深入java事件注冊的應(yīng)用分析

2019-11-26 16:05:01
字體:
供稿:網(wǎng)友
對上次的三個問題的個人理解:
1) 程序首先是從main函數(shù)開始執(zhí)行的,假設(shè)main 函數(shù)不是 static ,就要先實例化這個類,然后調(diào)用 main 方法,這似乎是不現(xiàn)實的. 其次 用 static 修飾的 main 方法是存儲在靜態(tài)的存貯區(qū)當中的,也就是說在創(chuàng)建一個類之后,main 函數(shù)就已經(jīng)存在了,去掉 static 修飾之后,編譯可以通過,但是不能執(zhí)行。
2)查 API之后才發(fā)現(xiàn)BufferedRead 對象的 readLine()方讀到的數(shù)據(jù)是,讀到有換行的地方為一行,直接用 readLine 判斷的時候已經(jīng)讀入一行了,在輸出數(shù)據(jù)時就會隔行輸出。
復(fù)制代碼 代碼如下:

FileReader file=new FileReader("C://123.txt");
            BufferedReader br1=new BufferedReader(file);
                       //判斷的時候已經(jīng)讀入一行
            while((br1.readLine())!=null)
            {   //輸出的是第二行的內(nèi)容
                System.out.println(br1.readLine());
            }

所以用一個臨時的字符串變量來存儲讀到的數(shù)據(jù),程序改改這樣就可以了:
復(fù)制代碼 代碼如下:

FileReader file=new FileReader("C://123.txt");
            BufferedReader br1=new BufferedReader(file);

            String cd;
            while((cd=br1.readLine())!=null)
            {
                System.out.println(cd);
            }

3)如果將客戶端、輸入流、輸出流的初始化全部放進 Send 按鈕的事件當中時,程序會達到我想要的效果,點擊連接之后就會有客戶端連接上去,但總覺得這樣會有其他的安全隱患,總有一天它會暴漏的。
今天要記錄在這里的是老師隨堂布置的一個小程序,實現(xiàn)一個計算器的雛形,里面只有加減運算,對其中的按鈕注冊有了一點新的認識,還是將代碼貼出來先。
復(fù)制代碼 代碼如下:

import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
public class ComboBoxTest extends JFrame{    
      private JButton done =new JButton(" Done ");
      private JButton clear=new JButton(" Clear ");     
      private JLabel  label = new JLabel("Please choose serverID:0(+)and1(-)");     

 public  ComboBoxTest(){    
     //添加一個組合框并設(shè)置兩個選項
      final JComboBox c = new JComboBox();
      int [] array = {0,1};
      c.addItem(array[0]);
      c.addItem(array[1]);
      final JTextField operand1=new JTextField(10);      //添加第一個操作數(shù)為輸入文本框,占8列
      final JLabel t=new JLabel("+");                     //初始化中間的操作符為“+”號
      final JTextField operand2=new JTextField(10);      //第二個操作符
      final JTextField result=new JTextField(4);         //結(jié)果的文本域 ,初始化占4列

      //給組合框c注冊一個事件,當組合框選項發(fā)生變化的時候,觸發(fā)的相應(yīng)事件
      c.addActionListener(new ActionListener() {     
          public void actionPerformed(ActionEvent e) {
              if(c.getSelectedIndex()==0)   //選項為“0”的時候 令中間的操作符顯示“+”號
                t.setText(" + ");          
               else  t.setText(" - ");       
         }
        });
      //給按鈕Done注冊一個事件,當中間的操作符不同時進行不同的操作
       done.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent e) {
                if(c.getSelectedIndex()==0)  
                {
                    //當中間的操作符為“+”號時,進行兩個操作數(shù)的加法 ,文本域的get()方法返回的是字符串,進行強制轉(zhuǎn)換
                     int a=Integer.parseInt(operand1.getText())+Integer.parseInt(operand2.getText());                    
                     result.setText("="+" "+a+" ");  //設(shè)置結(jié)果顯示相應(yīng)的結(jié)果
                   }         
              else {
                  //當中間的操作符為“-”號的時候,進行兩個操作數(shù)的減法
                int a=Integer.parseInt(operand1.getText())-Integer.parseInt(operand2.getText());                    
                result.setText("="+" "+a+" ");
              }   
            }             
          });
    // 給按鈕clear注冊一個事件,清空兩個操作數(shù)和結(jié)果的內(nèi)容
     clear.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {           
            operand1.setText("");    //清空操作數(shù)1
            operand2.setText("");    //清空操作數(shù)2
            result.setText("");      //清空結(jié)果框
            }              
          });   
      setLayout(new FlowLayout());
      add(label);                  
      add(c);
      add(operand1);
      add(t);
      add(operand2);
      add(result);
      add(done);
      add(clear);
      setSize(350,140); 
      setVisible(true);  
     }

   public static void main(String[] args) {
       new ComboBoxTest();
         }
}

上面的代碼中給選項框、“done”、"clear"按鈕注冊事件的時候所用的都是匿名類,這個類的創(chuàng)建就是為了給相應(yīng)的組件添加事件,還可以這樣寫,用里面的“clear”這個按鈕來做個例子。
實現(xiàn) ActionListener 抽象類當中的唯一的一個接口函數(shù),為此定義一個 ButtonListener 監(jiān)聽器的對象
復(fù)制代碼 代碼如下:

class ButtonListener implements ActionListener{
       public void actionPerformed(ActionEvent e){
            operand1.setText("");    //清空操作數(shù)1
            operand2.setText("");    //清空操作數(shù)2
            result.setText("");      //清空結(jié)果框            
       }
   }

類屬性當中需要定義一個 ButtonListener 的對象屬性:
復(fù)制代碼 代碼如下:

private ButtonListener clearaction = new ButtonListener();

最后一個步驟就是將這個按鈕監(jiān)聽器的事件對象注冊給按鈕:
復(fù)制代碼 代碼如下:

clear.addActionListener(clearaction);

個人總結(jié):
這一種注冊事件的方式大致過程是這樣的 ButtonListener =》 ActionListener => 注冊給按鈕,和匿名類相比,缺點是代碼量有點多,但假設(shè)你有N個打算具備這種
功能的按鈕并且事件實現(xiàn)的方法比較復(fù)雜時,就可以實現(xiàn)一個 ActionListener 的對象,同時定義N個 ButtonListener 監(jiān)聽器對象,將相同的事件實現(xiàn)注冊給按鈕就可以了,相比之下匿名類在這種情形下面會有很大的工作量,代碼量會激增。
還可以通過事件 e.getSource()方法將所有的事件處理放進一個函數(shù)當中,這樣似乎維護起來要更方便一點,在類的聲明當中要強調(diào)實現(xiàn)接口中的抽象函數(shù)。
復(fù)制代碼 代碼如下:

public class ComboBoxTest extends JFrame implements ActionListener

具體的實現(xiàn)過程如下:
復(fù)制代碼 代碼如下:

public void actionPerformed(ActionEvent e){
       if(e.getSource()==c){
              if(c.getSelectedIndex()==0)   //選項為“0”的時候 令中間的操作符顯示“+”號
                    t.setText(" + ");          
                    else  t.setText(" - ");    
       }

       if(e.getSource()==done){
            if(c.getSelectedIndex()==0)  
            {
                //當中間的操作符為“+”號時,進行兩個操作數(shù)的加法 ,文本域的get()方法返回的是字符串,進行強制轉(zhuǎn)換
                 int a=Integer.parseInt(operand1.getText())+Integer.parseInt(operand2.getText());                    
                 result.setText("="+" "+a+" ");  //設(shè)置結(jié)果顯示相應(yīng)的結(jié)果
               }         
          else {
              //當中間的操作符為“-”號的時候,進行兩個操作數(shù)的減法
            int a=Integer.parseInt(operand1.getText())-Integer.parseInt(operand2.getText());                    
         result.setText("="+" "+a+" ");
          }     
       }      
       if(e.getSource()==clear){
           operand1.setText("");    //清空操作數(shù)1
            operand2.setText("");    //清空操作數(shù)2
            result.setText("");      //清空結(jié)果框   
       }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 富平县| 新竹市| 蛟河市| 曲水县| 房山区| 白沙| 荔浦县| 高青县| 彭阳县| 蒙自县| 崇信县| 西乌珠穆沁旗| 蒙山县| 宣威市| 保亭| 荣昌县| 寻甸| 崇信县| 年辖:市辖区| 甘泉县| 冷水江市| 博白县| 健康| 明星| 陇南市| 广东省| 宣汉县| 曲阳县| 阳信县| 福泉市| 喀什市| 赤壁市| 聂拉木县| 襄垣县| 望都县| 洛川县| 文安县| 石棉县| 翁牛特旗| 广安市| 瓮安县|