BlogJava 聯系 聚合 管理  

          Blog Stats

          News

           

          藍冰飛寒個人獨立博客地址,www.uphenan.com

          隨筆檔案

          文章檔案


          藍冰飛寒

          用心去做每一件事情

               雖然事件接口和事件源繁多復雜, 但是處理事件的基本原理卻很簡單:
                即想要接受某個事件的類實現該事件監聽器的接口,然后在事件源中注冊自己,這樣它就可以接收所要的事件,并通過監聽器接口中的方法進行處理。此文中使用了三個監聽器對象,一個是隨機顏色按鈕的監聽器ColorActionListener,還有另外兩個new出來的分別實現了顏色變暗和顏色變亮的監聽器對象。
               事件源分別是隨機顏色Button的ActionEvent對象,顏色變暗和顏色變亮的ActionEvent對象。
                當事件源對象發生ActionEvent事件的時候,會產生ActionEvent對象,此時編譯器會把這個事件對象傳個注冊在該事件源的接口,由相應接口執行操作。充分體現了用戶為主的操作體驗,根據用戶的動作來做出響應。這是事件驅動機制很好的地方,個人拙見。呵呵


          import java.awt.*;
          import java.awt.event.*;
          import javax.swing.*;

          public class ButtonFrame  extends SimpleFrame{
            public ButtonFrame(int width,int height){
              super(width,height);
              setTitle("這是個按鈕事件?。?);
              ColorPanel panel=new ColorPanel();
              Container contentPane=getContentPane();//返回此窗體的contentPane 對象
              contentPane.add(panel);
              
            }
          /*注釋用:getContentPane

          public Container getContentPane()

              返回此窗體的 contentPane 對象

              指定者:
                  接口 RootPaneContainer 中的 getContentPane

              返回:
                  contentPane 屬性*/
            //定義主函數,用來驅動ButtonFrame類
            public static void main(String[] args) {
              ButtonFrame frame=new ButtonFrame(400,300);
              frame.setVisible(true);
            }

          }

          class ColorPanel extends JPanel{
            public ColorPanel(){
              //創建組件
              colorButton =new JButton("隨機顏色");
              darkerButton=new JButton("顏色變暗");
              brighterButton=new JButton("顏色變亮");
              colorText=new JTextArea(10,30);
              // 添加組件
              add(new JScrollPane(colorText));
              add(colorButton);
              add(darkerButton);
              add(brighterButton);
              setBackground(backgroundColor);
              //注冊組件所監聽的事件
              colorButton.addActionListener(new ColorActionListener());
              brighterButton.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent event){
                  setBrighter();
                }
              });
              
            
            darkerButton.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent event){
                setDarker();
              }
            });
            
            }
            private void setBrighter(){
              backgroundColor=backgroundColor.brighter();
              changeColor();
            }
            private void setDarker(){
              backgroundColor=backgroundColor.darker();
              changeColor();
            }
            private class ColorActionListener implements ActionListener{
              public void actionPerformed(ActionEvent event){
                setColor();//設置面板的顏色
              }
              
            }
            private void changeColor(){
              int r=backgroundColor.getRed();
              int g=backgroundColor.getGreen();
              int b=backgroundColor.getBlue();
              colorText.append("顏色值:\tR="+r+"\t G="+g+"\t B="+b+"\n");
              this.setBackground(backgroundColor);
            }
            private void setColor(){
              //產生隨機顏色
              int r=(int)(Math.random()*255);
              int g=(int)(Math.random()*255);
              int b=(int)(Math.random()*255);
              colorText.append("顏色值:\tR="+r+"\t G="+g+"\t B="+b+"\n");
               backgroundColor=new Color(r,g,b);//非常的重要, 我這句話少寫了,弄了很長時間才弄好
              this.setBackground(backgroundColor);
            }
            private Color backgroundColor;
            private JButton colorButton;
            private JButton darkerButton;
            private JButton brighterButton;
            private JTextArea colorText;
          }

          Tags -
          文章來源:http://www.tt-shopping.com/kevinlau/read.php/113.htm
          posted on 2009-05-09 21:59 藍冰飛寒 閱讀(461) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 电白县| 邢台市| 介休市| 内乡县| 双鸭山市| 泸定县| 平乡县| 西丰县| 如皋市| 大冶市| 新和县| 历史| 陈巴尔虎旗| 临夏县| 皋兰县| 麻栗坡县| 莲花县| 咸丰县| 荆州市| 彰武县| 四会市| 蒙城县| 怀柔区| 宜宾县| 云和县| 抚宁县| 荃湾区| 清镇市| 黄石市| 柳林县| 甘孜县| 滕州市| 鲁甸县| 普陀区| 南漳县| 西宁市| 白沙| 会同县| 马关县| 改则县| 东辽县|