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

          Blog Stats

          News

           

          藍(lán)冰飛寒個(gè)人獨(dú)立博客地址,www.uphenan.com

          隨筆檔案

          文章檔案


          藍(lán)冰飛寒

          用心去做每一件事情

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


          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("這是個(gè)按鈕事件啊!");
              ColorPanel panel=new ColorPanel();
              Container contentPane=getContentPane();//返回此窗體的contentPane 對(duì)象
              contentPane.add(panel);
              
            }
          /*注釋用:getContentPane

          public Container getContentPane()

              返回此窗體的 contentPane 對(duì)象

              指定者:
                  接口 RootPaneContainer 中的 getContentPane

              返回:
                  contentPane 屬性*/
            //定義主函數(shù),用來(lái)驅(qū)動(dòng)ButtonFrame類(lèi)
            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("隨機(jī)顏色");
              darkerButton=new JButton("顏色變暗");
              brighterButton=new JButton("顏色變亮");
              colorText=new JTextArea(10,30);
              // 添加組件
              add(new JScrollPane(colorText));
              add(colorButton);
              add(darkerButton);
              add(brighterButton);
              setBackground(backgroundColor);
              //注冊(cè)組件所監(jiān)聽(tīng)的事件
              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)生隨機(jī)顏色
              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);//非常的重要, 我這句話(huà)少寫(xiě)了,弄了很長(zhǎng)時(shí)間才弄好
              this.setBackground(backgroundColor);
            }
            private Color backgroundColor;
            private JButton colorButton;
            private JButton darkerButton;
            private JButton brighterButton;
            private JTextArea colorText;
          }

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

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 大厂| 南郑县| 古田县| 琼结县| 枣庄市| 江华| 鄂托克前旗| 阳高县| 吉林省| 观塘区| 永清县| 古交市| 江西省| 洛阳市| 台山市| 远安县| 芜湖市| 晋中市| 周至县| 巧家县| 津市市| 深圳市| 溧水县| 吉木萨尔县| 青川县| 绩溪县| 阿坝| 栾川县| 依兰县| 轮台县| 蒙城县| 天柱县| 和顺县| 安溪县| 瑞丽市| 综艺| 绥芬河市| 芦溪县| 丹江口市| 陆丰市| 肥城市|