BlogJava 聯(lián)系 聚合 管理  

          Blog Stats

          News

           

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

          隨筆檔案

          文章檔案


          藍冰飛寒

          用心去做每一件事情

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


          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 屬性*/
            //定義主函數(shù),用來驅(qū)動ButtonFrame類
            public static void main(String[] args) {
              ButtonFrame frame=new ButtonFrame(400,300);
              frame.setVisible(true);
            }

          }

          class ColorPanel extends JPanel{
            public ColorPanel(){
              //創(chuàng)建組件
              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);
              //注冊組件所監(jiān)聽的事件
              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();//設(shè)置面板的顏色
              }
              
            }
            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(){
              //產(chǎn)生隨機顏色
              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)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 海丰县| 广安市| 大邑县| 涞源县| 普兰县| 勐海县| 新巴尔虎左旗| 福清市| 孟连| 成安县| 项城市| 陆川县| 富平县| 顺平县| 泊头市| 山阴县| 玉龙| 思茅市| 秭归县| 南安市| 营口市| 广灵县| 普兰县| 崇仁县| 云和县| 临西县| 马龙县| 安阳市| 平潭县| 攀枝花市| 乐平市| 洪江市| 包头市| 泉州市| 天柱县| 西安市| 米泉市| 黔东| 清徐县| 辽宁省| 桦南县|